Xampp Error

Discussion in 'PHP' started by sonu21, Jan 26, 2012.

  1. #1
    Hello Friends,I need little help with Xampp.I had updated Xampp but when I worked with PHP & Mysql with Xampp then it shows error.Like I had created 1 form which will insert data in Database but it shows :

    "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 'div, stan) VALUES ('sonu','1','A','12')' at line 1 "

    I don't know why this error are seen.I had already worked on Xampp & also create a same form insert into DB successfully but when I formatted my pc it is suddenly showing this error.

    <?php
    $con = mysql_connect("localhost","root","root123");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("main", $con);
    
    $sql="INSERT INTO infos (name, rollno, div, stan)
    VALUES ('$_POST[name]','$_POST[rollno]','$_POST[div]','$_POST[stan]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
    
    mysql_close($con)
    ?> 
    
    
    PHP:
    Please Help me....
     
    sonu21, Jan 26, 2012 IP
  2. trecords

    trecords Well-Known Member

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #2
    Your problem is here:
    $sql="INSERT INTO infos (name, rollno, div, stan)
    VALUES ('$_POST[name]','$_POST[rollno]','$_POST[div]','$_POST[stan]')";
    PHP:
    Replace this with this one and try again:

    $sql="INSERT INTO infos (name, rollno, div, stan)
    VALUES ('".$_POST["name"]."','".$_POST["rollno"]."','".$_POST["div"]".','".$_POST["stan"]."')";
    PHP:
     
    trecords, Jan 26, 2012 IP
  3. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #3
    Chances are you use reserved words in your table or field names, maybe div or name

    Try to change your table names OR use the ` (<- don't know its name)

    "INSERT INTO `infos` (`name`, `rollno`, `div`, `stan`)
    VALUES ($_POST['name'], $_POST['rollno'], $_POST['div'], $_POST['stan'])"

    PS: NEVER EVER use an input directly in a query. Google SQL injection to learn more about it.
     
    bogi, Jan 26, 2012 IP
  4. sonu21

    sonu21 Member

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4
    Thanks,but it shows error.

    "Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING"

    Thanks for help...
     
    sonu21, Jan 26, 2012 IP