编写PDO更新语句的正确方法 [英] Correct way to write PDO update statements

查看:73
本文介绍了编写PDO更新语句的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是编写PDO更新语句的正确方法吗?我以在线上看到的教程为基础,但是当被AJAX触发时,它似乎根本没有做任何事情(即使AJAX请求在控制台中被成功记录,也没有对数据库进行更新):

Is this the correct way to write a PDO update statement? I based it off a tutorial I saw online but it doesn't seem to be doing anything at all when fired by AJAX (even though the AJAX request is being logged as successful in the console, no update is made to the database):

$duedate = $_POST["duedate"];
$status = $_POST["status"];
$id = $_POST["id"];

$sql = "UPDATE pm_schedule SET duedate=?, status=? WHERE id=?";
$q = $pdo->prepare($sql);
$q->execute(array($duedate,$status,$id));

推荐答案

是的,这是正确的,但是您需要告诉PDO在出错时引发错误.
因此,使您的连接代码如下:

Yes, it's correct, but you need to tell PDO to raise an error on error.
So, make your connection code like this:

$dsn = "mysql:host=$host;dbname=$db;charset=utf8";
$opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn, $user, $pass, $opt);

并确保您可以看到PHP错误.因此,您将知道发生了什么问题.

And make sure you can see PHP errors. So, you'll kn ow what's going wrong.

这篇关于编写PDO更新语句的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆