View Full Version : Register script not working (php)

14 May 2006, 08:23 AM
for the past few days, ive been trying to make a login script for my website...

ive been trying to troubleshoot it. Whenever i try to creat a user it tells me "couldnt add data to table". I have no idea what to do, i tried manualy putting in the sql querry without php and it worked. Im not sure whats wrong. ill post the scripts too.

this is the register2.php script


$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$username = strip_tags($username);
$email = strip_tags($email);
$password = md5($password);
//form check
if ( !$username || !$password ) {
die( "You did not enter a username or password" );
//mysql login
$sqluser = "dbuser";
$sqlpass = "dbpw";
$db = "mydb";
$connect = mysql_connect( "localhost", $sqluser, $sqlpass );
if ( ! $connect ) {
die( "Could not connect to SQL server" );
mysql_select_db( $db, $connect )
or die ("Could not open database:" .mysql_error() );
//check if user exists
$selectstatement = "SELECT * FROM users WHERE username='$username'";

//insert data into db
$insertstatement = "INSERT INTO users ( username, password, email, ip ) VALUES (
'$username', '$password', '$email', '$ip' )";

$result = mysql_query( $selectstatement, $connect );
$numrows = mysql_num_rows( $result );
if ( $numrows > 0 ) {
print " The username you have chosen is in use. Please choose another one.";
else {
mysql_query( $insertstatement, $connect )
or die( "Couldn't add data to table" );
print "Registration successful! ";

this is the register.php script (forms only)

<head><title>Temporary registry</title></head>
<body align="center">
<form action="register2.php" method="post">
Username: <input type="text" name="username" size="20"><br>
Password: <input type="password" name="password" size="20"><br>
Email: <input type="text" name="email" size="30"><br>
<center><input type="submit" value="Submit"><input type="reset"></center>

14 May 2006, 01:43 PM
Are you sure you have the mySQL username, password and database name correct?
Also, consider changing the code to:

mysql_query( $insertstatement, $connect )
or die( "Couldn't add data to table<br/>".mysql_error());

to get a better idea of what's going wrong...