user2152265
user2152265

Reputation: 23

Mysql Insert Into Error

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

Upvotes: 0

Views: 94

Answers (5)

Fahim Parkar
Fahim Parkar

Reputation: 31637

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");

Upvotes: 0

HYDER ALI
HYDER ALI

Reputation: 86

there may be unique constraints for any of the column

Upvotes: 0

Sudip
Sudip

Reputation: 2051

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

Upvotes: 0

Rudi Visser
Rudi Visser

Reputation: 21979

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.

Upvotes: 4

c.pramod
c.pramod

Reputation: 606

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

Upvotes: 0

Related Questions