【发布时间】:2018-07-01 18:42:16
【问题描述】:
谁能告诉我为什么这不起作用
我正在尝试使用插入创建一个表并为其创建一个唯一 ID,然后我希望在另一个页面上更新该表,但选择具有唯一 ID 的表。
但由于某种原因,我得到的只是第一次插入工作,但更新不会..任何人都可以帮忙..
非常感谢
创建具有唯一 ID 的表
$listID=rand(10,100);
$db = new PDO("mysql:host=localhost;dbname=classifieds2", 'root', ''); // 1. set database with this instead of conect - or change conect to this
$query="INSERT INTO `listings` (`username`,`listID`) VALUES (?,?)";
$stat=$db->prepare($query);$stat=$db->prepare($query);
$stat->execute(array("$accountname","$listID"));
然后使用以下代码更新该表,但该代码不起作用
$db = new PDO("mysql:host=localhost;dbname=classifieds2", 'root', ''); // 1. set database with this instead of conect - or change conect to this
$query="UPDATE `listings` SET (`date`,`firstname`,`lastname`,`title`,`info`,`location`,`phone`,`postcode`,`town`,`city`,`image`,`image2`,`image3`,`image4`,`image5`,`price`,`catagory`,`cond`,`delivery`,`username`,`email`,`youtubevideo`,`paypal`,`facebook`,`twitter`,`feedbackscore`) WHERE listID=? VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$stat=$db->prepare($query);$stat=$db->prepare($query);
$stat->execute(array("$now","$firstname","$lastname","$sellingtitle","$sellinginfo","$town","$phone1","$postcode","$town","$city","$i0url","$i1url","$i2url","$i3url","$i4url","$price","$catagory","$cond","$delivery","$sellername","$email","$youtubeurl","$paypal","$facebook","$twitter","feedbackscore","$listID"));
listid通过 POST 发送到更新的接收页面,并且唯一 ID 从第一个查询插入到表中 - 但更新不起作用。任何人都可以找出原因吗?
【问题讨论】:
-
所有这些变量都用双引号括起来是怎么回事?没必要。
-
设置PDO抛出异常,它会告诉你什么失败了。
-
请记住,每个
?必须在execute的数组的同一位置有一个适当的对应值。您的$listID应该在该数组的前面,而不是末尾。也将它们减少到一个:$stat=$db->prepare($query);$stat=$db->prepare($query); -
不更新语法
update tablename set fieldname=?, field2=? where key=? -
我不确定值是否可以出现在 sql update 语句的 where 之后。有人吗?