By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,041 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,041 IT Pros & Developers. It's quick & easy.

error in your SQL syntax

P: 18
hi....i am doing a php project.but lately, i have been in a dilemma bcoz i am receiving this kind of error after hitting the submit button.

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1


this is my coding.....

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* include db connection file */
  3. include("dbconn.php");
  4. if(isset($_POST['submit']))
  5. {
  6. /* capture values from HTML form */
  7. $icnumber = $_POST['icnumber'];
  8. $name = $_POST['name'];
  9. $position = $_POST['position'];
  10. $department = $_POST['department'];
  11. $email = $_POST['email'];
  12. $phoneno = $_POST['phoneno'];
  13. $username = $_POST['username'];
  14. $password = $_POST['password'];
  15.  
  16. $sql0 = "SELECT stafficnumber FROM staff WHERE stafficnumber= $icnumber";
  17. $query0 = mysql_query($sql0) or die ("Error: " . mysql_error());
  18. $row0 = mysql_num_rows($query0);
  19. if($row0 != 0)
  20. {
  21. echo "Record is existed";
  22. }
  23. else
  24. {
  25. /* execute SQL INSERT command */
  26. $sql2 = "INSERT INTO staff ( stafficnumber, staffname, staffposition, staffdepartment, staffemail, staffphoneno, staffusername, staffpassword )
  27. VALUES ('".$icnumber."','".$name."',','".$position."','".$department."','".$email."','".$phoneno."''".$username."','".$password."')";
  28. mysql_query($sql2) or die ("Error: ".mysql_error());
  29. /* display a message */
  30. echo "Data has been saved";
  31. }
  32. }// close if isset()
  33. /* close db connection */
  34. mysql_close($dbconn);
  35. ?>
  36.  
i have re-write this code but the error still appear after i hit the submit button. i'm very glad if anyone could help me plz....
Nov 29 '11 #1

✓ answered by zorgi

Generally that error means that something is wrong with your SQL syntax. I can see couple of things wrong with your $sql2 but there may be more.

You are missing comma here:

Expand|Select|Wrap|Line Numbers
  1. ".$phoneno."''".$username."
should be:

Expand|Select|Wrap|Line Numbers
  1. ".$phoneno."','".$username."
And you have problem with commas here too:

Expand|Select|Wrap|Line Numbers
  1. .$name."',','".$position.
It’s a good idea to echo your SQL statements for debugging.

Share this Question
Share on Google+
2 Replies


zorgi
Expert 100+
P: 431
Generally that error means that something is wrong with your SQL syntax. I can see couple of things wrong with your $sql2 but there may be more.

You are missing comma here:

Expand|Select|Wrap|Line Numbers
  1. ".$phoneno."''".$username."
should be:

Expand|Select|Wrap|Line Numbers
  1. ".$phoneno."','".$username."
And you have problem with commas here too:

Expand|Select|Wrap|Line Numbers
  1. .$name."',','".$position.
It’s a good idea to echo your SQL statements for debugging.
Nov 29 '11 #2

P: 18
thank you for helping me.....
Nov 30 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.