error in sql statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nazish zehra
    New Member
    • Dec 2011
    • 31

    error in sql statement

    I am making a from in php and inserting values in members table.Structure of members table is:
    id varchar(30) not null primary key
    username varchar(15)
    password varchar(15)

    when i insert values in table using $_POST[] it gives error
    Error:Duplicate entry 'john' for key 'PRIMARY' whereas there is no entry in my table as john.Code is:
    Code:
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db("seed_bank", $con);
    
    $sql = "insert into members(id,username,password) 
    values('$_POST[emailid]','$_POST[firstname]','$_POST[password]')";
    print $sql;
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
    if($count==1)
    {
    echo "Registration SUCCCESSFUL";
    }
    
    if(!mysql_query($sql,$con))
    {
    die('Error:'.mysql_error());
    }
    
    else {echo"Registration succcessful";}
    mysql_close($con);
    ?>
    It gives the error but at the same time also inserts values in the table.why it is giving error even when inserting values in the table?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It's because you run your query twice. Once on line 12 and again on line 19.

    Comment

    Working...