php – Mysqli准备语句插入不插入
发布时间:2020-12-13 17:47:56 所属栏目:PHP教程 来源:网络整理
导读:我已经连接到数据库了.当我回显所有变量时,它们可以工作,但它们不会插入到我的数据库表中.我的表名正确.这是代码: ?php$pid = '1'; $pname = 'name'; $poster_id = '2';$poster_name = 'name2'; $message = 'This is the message';$datetime = date("M d,Y"
|
我已经连接到数据库了.当我回显所有变量时,它们可以工作,但它们不会插入到我的数据库表中.我的表名正确.这是代码:
<?php
$pid = '1';
$pname = 'name';
$poster_id = '2';
$poster_name = 'name2';
$message = 'This is the message';
$datetime = date("M d,Y");
// insert into database
$ins = "INSERT INTO messages (profile_id,profile_name,poster_id,poster_name,message,countnum,postdate) VALUES (?,?,?)";
$stmt = $con->prepare($ins);
$num = 1;
$stmt->bind_param('isissis',$pid,$pname,$user_id,$user,$comment,$num,$datetime);
$stmt->execute();
?>
在此先感谢您的帮助. 解决方法
你有一些不匹配的变量.
$poster_id – $poster_name – $message 在你的绑定中与之对齐: $user_id,$comment 这应该现在有效: <?php
$pid = '1';
$pname = 'name';
$poster_id = '2';
$poster_name = 'name2';
$message = 'This is the message';
$datetime = date("M d,$poster_id,$poster_name,$message,$datetime);
$stmt->execute();
?>
但是,你应该替换$stmt-> execute(); with if(!$stmt-> execute()){trigger_error(“有一个错误……”.$con>错误,E_USER_WARNING);} 为了抓住错误. 还要在文件顶部添加error reporting,这有助于查找错误. <?php
error_reporting(E_ALL);
ini_set('display_errors',1);
// rest of your code
这将发出一个未定义的变量警告信号. 旁注:错误报告应该只在暂存中完成,而不是生产. 洞察力 正如Ghost所指出的那样: $datetime格式M d,Y也是可疑的,它可能搞砸Y-m-d H:i:s的格式为DATETIME列,如果确实如此. 因此你可能需要改变 $datetime = date("M d,Y");
至 $datetime = date("Y-m-d H:i:s");
要么 $datetime = date("Y-m-d");
取决于列类型的设置. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
