我正在尝试运行以下PDO查询,但它根本不更新记录。如果我通过PHPMyAdmin运行代码,它运行正常并更新记录。
$dbhost = 'localhost';
$dbuser = 'general';
$dbpass = 'removed';
$dbname = 'removed';
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$dbname, $dbuser, $dbpass);
}catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$query="UPDATE `soldier_info` SET `soldier_image`= '$soldier_image' WHERE `soldier_id`=" . $_POST['soldier_id'];
echo $query;
$q=$dbo->prepare($query);
$q->execute();
谁能告诉我我做错了什么?我没有看到任何错误消息回显或打印在页面上,当我回显$ query时显示:
UPDATE `soldier_info` SET `soldier_image`= 'http://www.bringingthemhome.co.uk/preview/images/soldiers/soldier1.jpg' WHERE `soldier_id`=4
如前所述,代码在PHPMyAdmin
中运行良好答案 0 :(得分:1)
试试这个:
<?php
$dbhost = 'localhost';
$dbuser = 'general';
$dbpass = 'removed';
$dbname = 'removed';
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$dbname, $dbuser, $dbpass);
$q = $dbo->prepare("UPDATE `soldier_info` SET `soldier_image`= :image WHERE `soldier_id`= :id");
$q->execute(array(":image" => $soldier_image, :id" => $_POST['soldier_id']));
}catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
确保$ soldier_image已定义。