2012-08-17 63 views
-2

UPDATE: 這個問題已經回答了 我有一個簡單的表格,用戶可以更新自己的MySQL數據庫。我使用mysql_連接到數據庫,但得知pdo是更好的方法,因爲mysql_已折舊。如何正確遷移從mysql_到PDO

包括以下是完整形式的老路上其次是完整形式的新途徑。 新的方式產生一個錯誤

老方法:

<?php 

$host = 'ip_address'; 
$user = 'user_name'; 
$password = 'password'; 

$link = mysql_connect($host, $user, $password); 

$selected = mysql_select_db('db_name', $link); 

if(!isset($_POST['text-input'])) 

?> 

<form method="post"> 
    %slice% 
    <input type="submit" value="Submit" /> 
</form> 
%[if !edit]% 

<?php 
%[repeat items]% 
$form_input%id=repeatIndex% = $_POST['element-%id=repeatIndex%'] ; 
%[endrepeat]% 

$query = 'INSERT INTO `table_name` (%[repeat items]%%[endif]%%html="Edit Me"%%[if !edit]%,%[endrepeat]%) VALUES (%[repeat items]%"' . $form_input%id=repeatIndex% . '",%[endrepeat]%);'; 
$query = preg_replace('/,\);/',');',$query); 
$query = preg_replace('/,\) /',')',$query); 
mysql_query($query); 

?> 
%[endif]% 

新途徑:

<?php 
db = new PDO('mysql:host=ip_address;dbname=db_name;', 'user_name', 'password'); 
?> 
    <form method="post"> 
     %slice% 
     <input type="submit" value="Submit" /> 
    </form> 
    %[if !edit]% 

<?php 
%[repeat items]% 
$form_input%id=repeatIndex% = $_POST['element-%id=repeatIndex%'] ; 

%[endrepeat]% 

$query = 'INSERT INTO `table_name` (%[repeat items]%%[endif]%%html="Edit Me"%%[if !edit]%,%[endrepeat]%) VALUES (%[repeat items]%"' . $form_input%id=repeatIndex% . '",%[endrepeat]%);'; 
$query = preg_replace('/,\);/',');',$query); 
$query = preg_replace('/,\) /',')',$query); 
mysql_query($query); 


?> 
%[endif]% 

的錯誤,即獲得罰球是這樣的:

Warning: mysql_query() [function.mysql-query]: Access denied for user 
'kuler'@'localhost' (using password: NO) in 
/home/path_to/index.php on line 125 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be 
established in /home/path_to/index.php on line 125 

我希望我提供了足夠的信息。

+6

你還在調用'的mysql_query()'與PDO。這兩個是不兼容的。使用'prepare()''execute()'或'query()'查看[PDO手冊](http://php.net/manual/en/book.pdo.php) – 2012-08-17 14:58:35

+0

請看看[這](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php),即使它是關於SQL注入,但它給你的基本您需要關於PDO的信息。 – Adi 2012-08-17 15:00:46

回答

1

的解決方案如下:

<?php 

//Connection vars 
$host = '%id=server%'; 
$user = '%id=username%'; 
$password = '%id=password%'; 
$databasename = '%id=database%'; 

//This block establishes the connection 
try{ 
    // 
    $connectiondetails = "mysql:host={$host};dbname={$databasename}"; 
    $db = new PDO($connectiondetails , $user , $password); 
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING); 
} 
catch (PDOException $e) { 
    print "Error!: " . $e->getMessage() . "<br/>"; 
    die(); 
} 

//Check if all the input fields of the form have been submitted 
if(
    %[repeat items]% 
    isset($_POST['element-%id=repeatIndex%'])&& 
    %[endrepeat]% 
    (true===true) 
){ 


    //The variables to read the output 
    %[repeat items]% 
    $form_input%id=repeatIndex% = $_POST["element-%id=repeatIndex%"] ; 
    %[endrepeat]% 

    //DB 
    global $db;  //must set this, otherwise can't access the db 
    $query = 'INSERT INTO `%id=table%` (%[endif]%%[repeat items]%%html="Edit Me"%%[if !edit]%,%[endif]%%[endrepeat]%%[if !edit]%)%[endif]% %[if !edit]%VALUES (%[repeat items]%"' . $form_input%id=repeatIndex% . '",%[endrepeat]%);'; 
    $query = preg_replace('/,\);/',');',$query); 
    $query = preg_replace('/,\) /',')',$query); 
    $db->query($query); 
    //[DB] 
} 

?>