0

I've created a registration form and I'm adding the data with php but for some reason it will not let me add the data into the database. Can you see anything wrong with this code?

<?php
mysql_connect("localhost", "root", "password") or die("No Connection");;
mysql_select_db("music") or die("No Database");;

$username= mysql_real_escape_string($_REQUEST["username"]);
$password= mysql_real_escape_string($_REQUEST["password"]);
$email= mysql_real_escape_string($_REQUEST["email"]);
$hash = md5( rand(0,1000) );

mysql_query("INSERT INTO members (id, username, password, email, hash, active) VALUES('', '$username', '$password', '$email', '$hash', '')") or die("Can't Add");
if(mysql_affected_rows()>0){
echo "1";
}else{
echo "2";
}
?>

I Keep getting a Can't Add error indicating that there is a simple problem with the mysql_query row

Thank you

4
  • What is indicated problem? the error message u gets? Commented Mar 11, 2013 at 11:07
  • 1
    I think its in the id. insert query will not execute if primary keys are the same. Commented Mar 11, 2013 at 11:08
  • what are you using? wamp ? Commented Mar 11, 2013 at 11:08
  • Just remove the id and the the '' corresponding to the id. Commented Mar 11, 2013 at 11:09

5 Answers 5

4

Can't add is not an error, just a catch all statement you added at the end.

To see your actual problem, change your code at the end of the mysql_query line to include the actual error retrieved from mysql_error().

mysql_query("INSERT INTO members (username, password, email, hash, active) VALUES('$username', '$password', '$email', '$hash', '')")
    or die("Can't Add - " . mysql_error());

That will give you more details regarding the error, if you post that, I can update my answer with the reason why.

Note that I've also removed the insertion of id, it's not needed if your column is AUTO_INCREMENT, which it should be.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You - I ran the error code and found the problem - It was to do with the id column - It's not an auto_incriment but the id is made up of a unique code based on time + random numbers - Sorted now and the registration form works just fine - Thanks again
0

if id is your primary key then replane '' with null

Comments

0

Make id as auto-incremented primary key and remove it from insert query.

Comments

0

there may be unique constraints for any of the column

Comments

0

Either have

mysql_query("INSERT INTO members (id, username, password, email, hash, active) 
             VALUES(null, '$username', '$password', '$email', '$hash', '')") or die("Can't Add");
                    ^^^^

OR

mysql_query("INSERT INTO members (username, password, email, hash, active) 
             VALUES( '$username', '$password', '$email', '$hash', '')") or die("Can't Add");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.