2014-11-08 40 views
-2

我試圖更新行或記錄,我不斷收到一個錯誤:更新記錄與PDO一直收到錯誤

Parse error: syntax error, unexpected '$sql' (T_VARIABLE) in C:\wamp\www\Systems\update_process.php on line 11

我不知道如何去解決這個我試過很多東西。

<?php 

$db_host = "localhost"; 
$db_username = "root"; 
$db_pass = ""; 
$db_name = "systems_requests"; 
try{ 
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING) 

$sql = 'UPDATE requests SET lanId= :lanId, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compUser= :compUser, compDt= :comDt WHERE id= :id'; 
$stmt = $pdo->prepare($sql); 
$stmt->bindParam(':id', $_POST['lanId'], PDO::PARAM_INT);  
$stmt->bindParam(':lanId', $_POST['lanId'], PDO::PARAM_STR);  
$stmt->bindParam(':name', $_POST['$name'], PDO::PARAM_STR); 
$stmt->bindParam(':department', $_POST['department'], PDO::PARAM_STR); 
$stmt->bindParam(':manager', $_POST['manager'], PDO::PARAM_STR); 
$stmt->bindParam(':request', $_POST['request'], PDO::PARAM_STR);  
$stmt->bindParam(':request_description', $_POST['request_description'], PDO::PARAM_STR); 
$stmt->bindParam(':request_comments', $_POST['request_comments'], PDO::PARAM_STR); 
$stmt->bindParam(':status', $_POST['status'], PDO::PARAM_STR); 
$stmt->bindParam(':comments', $_POST['comments'], PDO::PARAM_STR); 
$stmt->bindParam(':compUser', $_POST['compUser'], PDO::PARAM_STR); 
$stmt->bindParam(':comDt', $_POST['comDt'], PDO::PARAM_INT); 
$stmt->execute(); 
header('Location:index.php'); 
}catch(PDOException $exception){ 
      echo "Error: " . $exception->getMessage(); 
    } 
?> 

訂正代碼,但仍然沒有工作獲取錯誤信息致命錯誤:調用一個成員函數準備()的非對象在C於:\瓦帕\ WWW \上線系統\ update_process.php 12

<?php 

$db_host = "localhost"; 
$db_username = "root"; 
$db_pass = ""; 
$db_name = "systems_requests"; 
try{ 
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

$db = 'UPDATE requests SET id=:id, lanId= :lanId, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compUser= :compUser, compDt= :comDt WHERE id= :id'; 
$stmt = $db->prepare($sql); 
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);  
$stmt->bindParam(':lanId', $_POST['lanId'], PDO::PARAM_STR);  
$stmt->bindParam(':name', $_POST['$name'], PDO::PARAM_STR); 
$stmt->bindParam(':department', $_POST['department'], PDO::PARAM_STR); 
$stmt->bindParam(':manager', $_POST['manager'], PDO::PARAM_STR); 
$stmt->bindParam(':request', $_POST['request'], PDO::PARAM_STR);  
$stmt->bindParam(':request_description', $_POST['request_description'], PDO::PARAM_STR); 
$stmt->bindParam(':request_comments', $_POST['request_comments'], PDO::PARAM_STR); 
$stmt->bindParam(':status', $_POST['status'], PDO::PARAM_STR); 
$stmt->bindParam(':comments', $_POST['comments'], PDO::PARAM_STR); 
$stmt->bindParam(':compUser', $_POST['compUser'], PDO::PARAM_STR); 
$stmt->bindParam(':comDt', $_POST['comDt'], PDO::PARAM_INT); 
$stmt->execute(); 
header('Location:index.php'); 
}catch(PDOException $exception){ 
      echo "Error: " . $exception->getMessage(); 
    } 




?> 

正確的代碼,現在和工作

<?php 
 
    
 
$db_host = "localhost"; 
 
$db_username = "root"; 
 
$db_pass = ""; 
 
$db_name = "systems_requests"; 
 
try{ 
 
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); 
 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 
 
    
 
$sql = 'UPDATE requests SET id=:id, lanId= :lanId, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compUser= :compUser, compDt= :comDt WHERE id= :id'; 
 
$stmt = $db->prepare($sql); 
 
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);  
 
$stmt->bindParam(':lanId', $_POST['lanId'], PDO::PARAM_STR);  
 
$stmt->bindParam(':name', $_POST['$name'], PDO::PARAM_STR); 
 
$stmt->bindParam(':department', $_POST['department'], PDO::PARAM_STR); 
 
$stmt->bindParam(':manager', $_POST['manager'], PDO::PARAM_STR); 
 
$stmt->bindParam(':request', $_POST['request'], PDO::PARAM_STR);  
 
$stmt->bindParam(':request_description', $_POST['request_description'], PDO::PARAM_STR); 
 
$stmt->bindParam(':request_comments', $_POST['request_comments'], PDO::PARAM_STR); 
 
$stmt->bindParam(':status', $_POST['status'], PDO::PARAM_STR); 
 
$stmt->bindParam(':comments', $_POST['comments'], PDO::PARAM_STR); 
 
$stmt->bindParam(':compUser', $_POST['compUser'], PDO::PARAM_STR); 
 
$stmt->bindParam(':comDt', $_POST['comDt'], PDO::PARAM_INT); 
 
$stmt->execute(); 
 

 
}catch(PDOException $exception){ 
 
      echo "Error: " . $exception->getMessage(); 
 
    } 
 

 
    
 

 

 
?>

+0

你錯過了',''在$ DB->的setAttribute(PDO :: ATTR_ERRMODE,PDO結束:: ERRMODE_WARNING)' – andrewsi 2014-11-08 13:26:53

+0

未定義的變量:pdo在C:\ wamp \ www \ Systems \ update_process.php上,並且在C:\ wamp \ www \ Systems \第12行的update_process.php現在我添加了這些錯誤; – Donny 2014-11-08 13:31:04

+0

第12行是$ stmt = $ pdo-> prepare($ sql); – Donny 2014-11-08 13:31:50

回答

2
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING) 

$sql = 'UPDATE requests SET lanId= :lanId, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compUser= :compUser, compDt= :comDt WHERE id= :id'; 
$stmt = $pdo->prepare($sql); 

的末尾沒有分號允許審查:

這條線:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING) 

觸發了錯誤,因爲PHP不能處理下一行。解決方案:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

,並在註釋:

Undefined variable: pdo in C:\wamp\www\Systems\update_process.php on line 12 and Call to a member function prepare()

,這是因爲您已經分配PDO類是在這一行$db

$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); 

因此,解決辦法:

$stmt = $db->prepare($sql); 

編輯:

Fatal error: Call to a member function prepare()

這是因爲你的SQL查詢將返回錯誤,這可能發生了很多很多原因。在任何查詢中輸入錯字。您正在嘗試調用不存在的表/列名稱,該表不存在。名單繼續。用於調試目的。我會建議改變你的代碼:

if (!stmt){ 
    print_r($db->errorInfo()); 
} 

最後,因爲它似乎你的try/catch不捕捉被拋出的所有異常。這會向您顯示您的錯誤,並將幫助您將代碼更新爲正確執行的查詢。


它似乎也已更新您的問題發佈新代碼。有碰撞與你的變量:

$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

$db = 'UPDATE requests SET id=:id, lanId= :lanId, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compUser= :compUser, compDt= :comDt WHERE id= :id'; 
$stmt = $db->prepare($sql); 

你在做什麼這裏設置變量$db是PDO類的訪問/構造,那麼三線下跌超過寫這個變量是一個字符串。因此$db中沒有數據庫連接。解決的辦法是在你的變量名的簡單變化:

$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

$sql= 'UPDATE requests SET id=:id, lanId= :lanId, name= :name, department= :department,manager= :manager,request= :request,request_description= :request_description, request_comments= :request_comments,status= :status,comments= :comments,compUser= :compUser, compDt= :comDt WHERE id= :id'; 
$stmt = $db->prepare($sql); 

回到原來

+0

'$ stmt = $ pdo-> prepare($ sql);' – 2014-11-08 13:49:48

+0

得到它的工作感謝Daryl的幫助和所有的輸入我現在明白我做錯了什麼,我開始瞭解pdo – Donny 2014-11-08 14:01:14

1

有在管線9

+1

您應該將有問題的行復制並粘貼到答案中,以便識別。 – Anthony 2014-11-08 13:48:07