我正在尝试添加新记录,但错误显示"无法插入"。下面是我使用的代码(假设我已经在下面声明了这些变量)。
$sql6 = "SELECT * FROM records WHERE serialno='$serialno'";
//checking if the item is available in db
$check = $this->db->query($sql6) ;
$count_row = $check->num_rows;
//if the item is not in db then insert to the table
if ($count_row == 0){
$poster = "Admin";
$sql6 = "INSERT INTO records SET itemname='$itemname', itemdesc='$itemdesc', brand='$brand', serialno='$serialno', nostock='$nostock', price='$price', onsale='$onsale', $poster='$poster', thedate='date('Y-m-d')'";
$result = mysqli_query($this->db,$sql6) or die(mysqli_connect_errno()."Data cannot inserted");
return $result;
}
else { return false;}
我的数据库结构是
id (int, autoincrement)itemname (varchar), itemdesc (varchar), brand (varchar), serialno (varchar), nostock (int), price (varchar), onsale (tinytext), poster (tinytext), thedate (date)
我收到一个错误,说"无法插入"。 任何人都有想法或线索 我的代码有什么问题? 任何帮助将不胜感激。谢谢!
PS:我尝试通过回显$itemname,$itemdesc等显示所有帖子数据,例如($itemname,$itemdesc等),并且它确实正确显示,因此我的查询可能存在问题。
INSERT INTO records SET itemname='$itemname', itemdesc='$itemdesc', brand='$brand', serialno='$serialno', nostock='$nostock', price='$price', onsale='$onsale', $poster='$poster', thedate=''
将此布局更改为:
INSERT INTO records (itemname,itemdesc, brand, serialno,nostock, price,onsale,poster, thedate) VALUES ($itemname,$itemdesc ...the values... ".date('Y-m-d').", )
这是流程形状:
INSERT <tablename> (<columns1>,<columns2>) VALUES ($value1,$value2)
此代码会将一行新数据插入到指定的表中,并在成功时返回 true。
您可能还需要更改名为 $poster
的列。这可能是不正确的,列名不应该是列值 - 不是语法中断,而是错误的方法。
步骤 2
仍然不起作用 - 所以得到一些详细的反馈,并更换
die(mysqli_connect_errno()."Data cannot inserted");
跟
die($this->db->error." :: Data cannot be inserted");
这将从MySQL对象中为您提供更多有用的反馈。