我正在尝试更新行或记录,但我一直收到错误:
解析错误:语法错误,第11行的C:\ wamp \ www \ Systems \ update_process.php中的意外'$ sql'(T_VARIABLE)
我不知道如何解决这个问题我做了很多尝试。
<?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();
}
?>
修改后的代码但仍无法正常工作获取错误消息致命错误:在第12行的C:\ wamp \ www \ Systems \ update_process.php中的非对象上调用成员函数prepare()
<?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 :(得分: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);
并在评论中:
未定义的变量:pdo在C:\ wamp \ www \ Systems \ update_process.php上 第12行并调用成员函数prepare()
这是因为您已在此行中将PDO类指定为$db
:
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
所以解决方案:
$stmt = $db->prepare($sql);
修改强>
致命错误:调用成员函数prepare()
这是因为您的SQL查询返回false,这可能由于很多原因而发生。任何查询中的错别字..您正在尝试调用不存在的表/列名称,该表不存在。名单还在继续。用于调试目的。我建议您将代码更改为:
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);
回到原来的
答案 1 :(得分:1)
第9行末尾没有分号