I'm working on a script that handles a member sign up. Upon submission of new member info the script queries the users table to check for duplicate user name (in this case an email) and return an error if duplicate is found. If not I want it to just submit the original new member info. The check for duplicates executes okay, but INSERT new info does not.
$sql = "SELECT * FROM users WHERE username = '{$email}' " ;
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
// Username already exists
echo'That Email is already in use.' ;
exit;
}
else
{
$sql = "INSERT INTO users (username, password, first_name, last_name) VALUE ('$email', '$pw', '$fname', '$lname')";
echo"Thank you for becoming a member ". $fname . "!";
}
I thought that maybe assigning a different variable to the INSERT query would work but I had no luck. Also tried to free $results and that didn't work either.
I use 'VALUE' all the time and it works, but for the sake of my own sanity I changed it to 'VALUES' to no avail. I used to use 'SET' as well until I ran into problems where a query would not work with 'SET' but did with 'VALUES'. Never thought it would make a diff.
$sql = "INSERT INTO users SET username='{$email}', password='{$pw}', first_name='{$fname}', last_name='{$lname}'";
The way I do it ensures that I don't forget a value of put the values in the wrong order. I'm just not organized enough! However it's a bit more difficult to do batch inserts.
Unfortunately neither option worked. I'm new to php but I have handled multiple INSERT to multiple tables and the same with SELECT all in one shot. This is the first I've attempted to go from a SELECT right into an INSERT. Could this be the problem?
I must be overlooking something somewhere. I broke the script down to it's simplest form and put it on a page all by itself and it still won't write to the table.
Holy Moley, you'll hate your self for this You forgot a crucial line: mysql_query($sql); which should go on line 16. As your code stands, it doesn't execute the new SQL command, just sets the $sql variable.
Regarding the VALUE syntax, VALUE and VALUES are interchangeable.
Wow, you are totally right, I do hate myself for that one. How on earth I could spend so much time and still be overlooking the same darned thing is beyond me. Alas, the joys of being a newb! Thanks Humbug!
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.