By Iain Simpson


2013-06-11 20:44:12 8 Comments

I am using the following script to process a form to add info to my website. The problem I am having is when I submit the form nothing gets submitted to the database, and there are no errors. How can I add error reporting to my query?

<?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);
?>

3 comments

@Travesty3 2013-06-11 20:45:55

For development purposes, you can add or die(mysqli_error($conn)) before the semicolon in your mysqli_query($conn, 'SELECT...') line.

Make sure you remove this before you push to production, so you don't output information about your database to the public.

@Funk Forty Niner 2018-09-25 14:48:21

FYI: mysqli_error() requires a db connection as a parameter. The answer should be edited.

@Travesty3 2018-09-26 15:08:04

@FunkFortyNiner: Fixed. Thanks for pointing it out!

@Fabio 2013-06-11 20:45:42

Just simply add or die(mysqli_error($db)); at the end of your query, this will print the mysqli error.

 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));

As a side note I'd say you are at risk of mysql injection, check here How can I prevent SQL injection in PHP?. You should really use prepared statements to avoid any risk.

@Iain Simpson 2013-06-11 20:55:09

Thanks, this appears to be working as its dieing and the script is stopping as the redirect doesn't work if I remove the comment marks, but it also doesnt display any errors.

@Iain Simpson 2013-06-11 21:06:17

As far as I can see, I am going to double check in the morning once I have the login details handy for php my admin.

@Fabio 2013-06-11 21:08:25

@Iain alright check them, it seems like there is something wrong in connection

@mogosselin 2014-05-19 21:46:50

Please prefer "trigger_error()" instead of "die()" trigger_error("Query Failed! SQL: $sql - Error: ". mysqli_error($db), E_USER_ERROR);

@Jessica 2013-06-11 20:46:16

mysqli_error()

As in:

$sql = "Your SQL statement here";
$result = mysqli_query($sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);

Trigger error is better than die because you can use it for development AND production, it's the permanent solution.

@Travesty3 2013-06-11 20:48:59

+1, but I would elaborate on how to use it for both development and production (how the error wouldn't be shown in production).

@Your Common Sense 2014-06-19 14:15:12

@Travesty3 just use it. This code is all right for both development and production

@Jessica 2014-06-19 16:02:12

Not if you just let the error get printed to the screen it's not.

@KarlosFontana 2014-07-31 10:05:31

ok, so how can i have both "Congrats! Query .$sql."was successfull!" or die: "Sorry the query $sql failed with this error: $error";

@John Reid 2015-04-23 17:07:30

For those that may be wondering: trigger_error() is fine for both production and development because error reporting is usually turned off in production. trigger_error therefore won't be printed out.

@Your Common Sense 2017-10-13 08:46:59

@KarlosFontana you should never have anything like 'or die: "Sorry the query $sql failed with this error: $error"' in the first place. Please read about PHP error reporting basics

@Your Common Sense 2017-10-13 15:32:28

@JohnReid strictly speaking, error reporting should be set to max on for the production as well. it's displaying errors what ought to be switched off. People often mistake these two matters, that's why I took liberty to intervene.

@Funk Forty Niner 2018-09-25 14:48:03

@Jessica I sometime use the question as a reference for closing and need to point out that mysqli_error() requires a db connection as a parameter. The answer should be edited.

Related Questions

Sponsored Content

33 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

15 Answered Questions

[SOLVED] How to get a list of user accounts using the command line in MySQL?

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

36 Answered Questions

[SOLVED] How do I check if a string contains a specific word?

29 Answered Questions

[SOLVED] How to output MySQL query results in CSV format?

  • 2008-12-10 15:59:51
  • MCS
  • 1043604 View
  • 1041 Score
  • 29 Answer
  • Tags:   mysql csv quotes

30 Answered Questions

[SOLVED] How do you parse and process HTML/XML in PHP?

26 Answered Questions

[SOLVED] How do I get PHP errors to display?

11 Answered Questions

[SOLVED] How to fix "Headers already sent" error in PHP

  • 2011-11-06 17:41:22
  • Moses89
  • 1242821 View
  • 836 Score
  • 11 Answer
  • Tags:   php header

31 Answered Questions

[SOLVED] How do I get a YouTube video thumbnail from the YouTube API?

13 Answered Questions

[SOLVED] mysqli or PDO - what are the pros and cons?

Sponsored Content