一尘不染

如何显示我的MySQLi查询错误?[重复]

mysql

这个问题已经在这里有了答案

mysqli_fetch_assoc()期望参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误? (1个答案)

mysqli还是死了,它一定要死吗? (4个答案)

5年前关闭。

我正在使用以下脚本来处理将信息添加到我的网站的表单。我遇到的问题是,当我提交表单时,什么也没有提交到数据库,并且没有错误。如何将错误报告添加到查询中?

<?php
if (isset($_POST['itemdescription'])) {$itemdescription = $_POST['itemdescription'];}else {$itemdescription = '';}
if (isset($_POST['itemnumber'])) {$itemnumber = $_POST['itemnumber'];}else {$itemnumber = '';}
if (isset($_POST['sellerid'])) {$sellerid = $_POST['sellerid'];}else {$sellerid = '';}
if (isset($_POST['purchasedate'])) {$purchasedatepre = $_POST['purchasedate'];$date = DateTime::createFromFormat("D F d, Y", $purchasedatepre);$purchasedate = date('Y-m-d',strtotime($purchasedatepre));}else {$purchasedatepre = ''; $purchasedate = '';}
if (isset($_POST['otherinfo'])) {$otherinfo = $_POST['otherinfo'];}else {$otherinfo = '';}
if (isset($_POST['numberofitems'])) {$numberofitems = $_POST['numberofitems'];}else {$numberofitems = '';}
if (isset($_POST['numberofitemsused'])) {$numberofitemsused = $_POST['numberofitemsused'];}else {$numberofitemsused = '';}
if (isset($_POST['isitdelivered'])) {$isitdelivered = $_POST['isitdelivered'];}else {$isitdelivered = '';}
if (isset($_POST['price'])) {$price = $_POST['price'];}else {$price = '';}

$itemdescription = str_replace("'", "", "$itemdescription");
$itemnumber = str_replace("'", "", "$itemnumber");
$sellerid = str_replace("'", "", "$sellerid");
$otherinfo = str_replace("'", "", "$otherinfo");

include("connectmysqli.php");

mysqli_query($db,"INSERT INTO stockdetails (`itemdescription`,`itemnumber`,`sellerid`,`purchasedate`,`otherinfo`,`numberofitems`,`isitdelivered`,`price`) VALUES ('$itemdescription','$itemnumber','$sellerid','$purchasedate','$otherinfo','$numberofitems','$numberofitemsused','$isitdelivered','$price')");

// header('Location: stockmanager.php?&key='.$key);
?>

阅读 230

收藏
2020-05-17

共1个答案

一尘不染

只需or die(mysqli_error($db));在查询末尾添加即可,这将显示mysqli错误。

 mysqli_query($db,"INSERT INTO stockdetails (`itemdescription`,`itemnumber`,`sellerid`,`purchasedate`,`otherinfo`,`numberofitems`,`isitdelivered`,`price`) VALUES ('$itemdescription','$itemnumber','$sellerid','$purchasedate','$otherinfo','$numberofitems','$numberofitemsused','$isitdelivered','$price')") or die(mysqli_error($db));

附带说明一下,我会说您有患上的风险mysql injection,请在此处查看如何防止PHP中的SQL注入?。您应该真正使用准备好的语句来避免任何风险。

2020-05-17