如何显示MySQLi查询的错误?我使用以下脚本来处理表单以向我的网站添加信息。我遇到的问题是,当我提交表单时,没有任何内容被提交到数据库,并且没有错误。如何向查询添加错误报告?<?phpif (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);?>
3 回答
收到一只叮咚
TA贡献1821条经验 获得超4个赞
只需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注入?。您应该使用准备好的语句来避免任何风险。
狐的传说
TA贡献1804条经验 获得超3个赞
mysqli_error()
如:
$sql = "Your SQL statement here";$result = mysqli_query($sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);
触发错误比die更好,因为你可以将它用于开发和生产,它是永久的解决方案。
largeQ
TA贡献2039条经验 获得超7个赞
出于开发目的,您可以在行中or die(mysqli_error($conn))
的分号前添加mysqli_query($conn, 'SELECT...')
。
确保在推送到生产之前删除它,因此不要向公众输出有关数据库的信息。
添加回答
举报
0/150
提交
取消