3

我已经验证了下面的所有变量,现在正尝试使用准备好的语句将它们插入到我的 mysql 数据库中,但是即使我没有收到任何系统错误,数据也没有插入。$insert_stmt->affected_rows>0 返回 false,从而显示我的自定义警告消息:

$insert_stmt = $db->prepare("INSERT INTO users (id,timestamp,externalid,password,authentication,email) VALUES(NULL,NOW(),?,?,?,?)");
$insert_stmt->bind_param("ssss",$userid,$hash,$authenticate,$email);
$insert_stmt->execute();
if ($insert_stmt->affected_rows>0)

我对 PHP(和社区)有点陌生,不知道为什么什么都不会插入,而我的自定义警告消息总是显示?同样,不会产生系统错误。谢谢!

4

3 回答 3

3

调用时可能会出错execute()。 如果语句成功并且失败execute()将返回。所以检查返回值。如果是,请致电获取有关该问题的信息。TRUEFALSEFALSEmysqli_stmt_error()

于 2012-10-14T05:17:00.993 回答
1

I figured out the issue. I was testing with an externalid that was already in the database, and my check for preventing duplicate external ids was not working correctly, so my code was reaching the insertion line and trying to put the externalid again into MySQL when it already existed in the database. I had set the externalid as a unique field, so it wouldn't allow it to be entered. MySQL though didn't return any noticeable errors when I had set E_ALL | E_STRICT. Good to know in the future.

I didn't realize I was testing with a duplicate entry, so sorry to the community for adding that nugget of info.

于 2012-10-14T19:05:49.437 回答
1

请执行以下操作:

1)看这个链接:

2)连接到数据库后添加:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

3) 在您的准备、绑定和执行语句周围添加这个 try/catch 块:

try { .. } and catch (Exception $e) { echo $e->getMessage(); }

4) 报告您收到的错误消息(如果有)

确保检查您的呼叫的错误状态。特别是$insert_stmt->execute()

于 2012-10-14T05:17:00.900 回答