error in your SQL syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ammyra91
    New Member
    • Nov 2011
    • 18

    error in your SQL syntax

    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.....

    Code:
    <?php
    /* include db connection file */
    include("dbconn.php");
    if(isset($_POST['submit']))
    {
    /* capture values from HTML form */
    $icnumber = $_POST['icnumber'];
    $name = $_POST['name'];
    $position = $_POST['position'];
    $department = $_POST['department'];
    $email = $_POST['email'];
    $phoneno = $_POST['phoneno'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $sql0 = "SELECT stafficnumber FROM staff WHERE stafficnumber= $icnumber";
    $query0 = mysql_query($sql0) or die ("Error: " . mysql_error());
    $row0 = mysql_num_rows($query0);
    if($row0 != 0)
    {
    echo "Record is existed";
    }
    else
    {
    /* execute SQL INSERT command */
    $sql2 = "INSERT INTO staff ( stafficnumber, staffname, staffposition, staffdepartment, staffemail, staffphoneno, staffusername, staffpassword )
    VALUES ('".$icnumber."','".$name."',','".$position."','".$department."','".$email."','".$phoneno."''".$username."','".$password."')";
    mysql_query($sql2) or die ("Error: ".mysql_error());
    /* display a message */
    echo "Data has been saved";
    }
    }// close if isset()
    /* close db connection */
    mysql_close($dbconn);
    ?>
    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....
    Last edited by Meetee; Nov 29 '11, 08:47 AM. Reason: code tags added
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    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:

    Code:
    ".$phoneno."''".$username."
    should be:

    Code:
    ".$phoneno."','".$username."
    And you have problem with commas here too:

    Code:
    .$name."',','".$position.
    It’s a good idea to echo your SQL statements for debugging.

    Comment

    • ammyra91
      New Member
      • Nov 2011
      • 18

      #3
      thank you for helping me.....

      Comment

      Working...