-1

So, I am writing some php scripts for my website, and the INSERTing of data is not working. What I am wanting for now, and for future reference is a way to output the error as no syntax errors are already being outputted.

How would I echo out the error in my code?

echo "POSTed Data - ".$username.", ".$password."<br>";
$UserData_SQLInsert = "INSERT INTO UserData ( Username, Password ) VALUES ( ".$username.", ".$password." ) ";
if (mysql_query(UserData_SQLInsert)) {
  echo "INSERT INTO UserData - Succcessful.<br><br>";
}
else {
  echo "INSERT INTO UserData - Failed.<br><br>";
  mysql_query(UserData_SQLInsert) or die(mysql_error());
}
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Ryan
  • 1,096
  • 2
  • 16
  • 31
  • There might a problem with your query which doesn't show up as an error in the code. Could you show the code? – elgis Jun 28 '14 at 20:47
  • 1
    `mysql_query(UserData_SQLInsert)` is missing a `$`. All Variables need to start with a dollarsign in PHP. – Martin Tournoij Jun 28 '14 at 21:18
  • Also, you want to read: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – Martin Tournoij Jun 28 '14 at 21:19
  • Is your code a deliberate error and you want to output those errors? I'm not quite grasping something. I know where the errors are, just wondering on the first part of my question. – Funk Forty Niner Jun 29 '14 at 00:09

2 Answers2

0

You could have a few things going on: PHP errors, or MySQL errors, or just a non effective query (i.e. you didn't build the string properly or some such).

If it's PHP errors, you should have something in your PHP error log. You can also use ini_set('display_errors',true); to enable on-screen errors. This is usually helpful, but depending on the routing in your application, sometimes doesn't show everything. So check the PHP logs, too. Again depending on your framework/application, you could have applicaiton-specific errors logs catching the errors. Check with your vendor's documentation if this isn't a from-scratch project.

MySQL errors can be found using mysqli_error. Check if the query returns false and then use mysqli_error to spit out whatever the database said about it.

Also, as Gianluca suggested, you can use var_dump to output your query in its entirely, which you can paste into MySQL Workbench or similar to review it more thoroughly.

Matthew Poer
  • 1,682
  • 10
  • 17
  • My error is an SQL error, It is in this bit of code: echo "POSTed Data - ".$username.", ".$password."
    "; $UserData_SQLInsert = "INSERT INTO UserData ( Username, Password ) VALUES ( ".$username.", ".$password." ) "; if (mysql_query(UserData_SQLInsert)) { echo "INSERT INTO UserData - Succcessful.

    "; } else { echo "INSERT INTO UserData - Failed.

    "; mysql_query(UserData_SQLInsert) or die(mysql_error()); }
    – Ryan Jun 28 '14 at 21:04
  • I think that Carpetsmoker spotted the biggest issue already, `UserData_SQLInsert` should be `$UserData_SQLInsert` (note the currency symbol $). If that's a verbatim copy-paste of your code, you're effectively sending the text 'UserData_SQLInsert' as your query. – Matthew Poer Jun 29 '14 at 00:28
-1

use the var_dump for any kind of doubt

http://www.php.net/manual/en/function.var-dump.php

Gianluca
  • 19
  • 5