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

error in sql statement

P: 31
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con = mysql_connect("localhost","root","");
  3. if (!$con)
  4.   {
  5.   die('Could not connect: ' . mysql_error());
  6.   }
  7. mysql_select_db("seed_bank", $con);
  8.  
  9. $sql = "insert into members(id,username,password) 
  10. values('$_POST[emailid]','$_POST[firstname]','$_POST[password]')";
  11. print $sql;
  12. $result=mysql_query($sql);
  13. $count=mysql_num_rows($result);
  14. if($count==1)
  15. {
  16. echo "Registration SUCCCESSFUL";
  17. }
  18.  
  19. if(!mysql_query($sql,$con))
  20. {
  21. die('Error:'.mysql_error());
  22. }
  23.  
  24. else {echo"Registration succcessful";}
  25. mysql_close($con);
  26. ?>
  27.  
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?
Jun 26 '12 #1

✓ answered by Rabbit

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

Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,347
It's because you run your query twice. Once on line 12 and again on line 19.
Jun 26 '12 #2

Post your reply

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