Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy. This forum is not for asking programming related questions.
hi all,
I will be honest i don't do much php so i am a bit rusty.. I am setting up a basic users database with mysyql.. there is a table called Users in the DB and three of the fields are unique: iD, userName, and email. setting these up in mySQL to be unique prevents duplicate users with the same username and/or email adress.. great.. and when i test my php, it doesn't write to the DB if one of these is not unique.. OK? AWESOME! but its also not throwing an error.. I'd like it to spit back which required field was not unique, so i can prompt the user to use a different email address or username..
// add user to database
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db("$DBName") or die("Unable to select
database $DBName");
$sqlquery = "INSERT INTO $table(userName,password,email)
VALUES('$userName', '$password', '$email')";
mysql_query($sqlquery);
mysql_close();
You are using the mysql_query($sqlquery); function without testing the value returned. You should use an if..else.. structure, because the function will return TRUE if it executes successfully, and FALSE if it does not. In the else portion you can present a message saying that the email or username are already in use.
// add user to database
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db("$DBName") or die("Unable to select
database $DBName");
$result = mysql_query("SELECT userName, email FROM $table WHERE userName = '$userName' OR email = '$email'", $connection);
if ($row = mysql_fetch_assoc($result)) {
if ($row['userName'] == $userName) {
echo " username already taken";
} elseif ($row['email'] == $email) {
echo " email already taken";
}
}else{
echo "user name and email are available";
$sqlquery = "INSERT INTO $table(userName,password,email)
VALUES('$userName', '$password', '$email')";
mysql_query($sqlquery);
}
mysql_close();
but then it returned the errors:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/eliddell/kingnozzle.com/regUser.php on line 25
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/eliddell/kingnozzle.com/regUser.php on line 26
user name and email are available
Last edited by eliddell on Fri Jul 15, 2011 11:54 am, edited 1 time in total.
You only show twenty lines of code and it's returning an error on line 25, so it's pretty hard to tell you what the problem is when a) you aren't showing all the code which b) makes it impossible for us to know which line of code is throwing the error.