Problems with multiple queries

Thread Solved

Join Date: Jul 2009
Posts: 208
Reputation: CFROG is an unknown quantity at this point 
Solved Threads: 14
CFROG's Avatar
CFROG CFROG is offline Offline
Posting Whiz in Training

Problems with multiple queries

 
0
  #1
Jul 18th, 2009
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.
  1. $sql = "SELECT * FROM users WHERE username = '{$email}' " ;
  2.  
  3. $result = mysql_query($sql);
  4.  
  5. if ( mysql_num_rows ( $result ) > 0 )
  6. {
  7. // Username already exists
  8. echo 'That Email is already in use.' ;
  9. exit;
  10. }
  11.  
  12. else
  13. {
  14.  
  15. $sql = "INSERT INTO users (username, password, first_name, last_name) VALUE ('$email', '$pw', '$fname', '$lname')";
  16.  
  17. echo "Thank you for becoming a member ". $fname . "!";
  18. }
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 955
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 126
ardav's Avatar
ardav ardav is offline Offline
Posting Shark

Re: Problems with multiple queries

 
0
  #2
Jul 18th, 2009
Isn't it 'VALUES'? I must admit I very rarely use that syntax, I tend to use 'SET', so I may be wrong.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 208
Reputation: CFROG is an unknown quantity at this point 
Solved Threads: 14
CFROG's Avatar
CFROG CFROG is offline Offline
Posting Whiz in Training

Re: Problems with multiple queries

 
0
  #3
Jul 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 955
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 126
ardav's Avatar
ardav ardav is offline Offline
Posting Shark

Re: Problems with multiple queries

 
0
  #4
Jul 18th, 2009
How about:

  1. $sql = "INSERT INTO users (username, password, first_name, last_name) VALUES ('{$email}', '{$pw}', '{$fname}', '{$lname}')";

Although, I tend to do this:

  1. $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.
Last edited by ardav; Jul 18th, 2009 at 6:51 pm.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 208
Reputation: CFROG is an unknown quantity at this point 
Solved Threads: 14
CFROG's Avatar
CFROG CFROG is offline Offline
Posting Whiz in Training

Re: Problems with multiple queries

 
0
  #5
Jul 18th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 208
Reputation: CFROG is an unknown quantity at this point 
Solved Threads: 14
CFROG's Avatar
CFROG CFROG is offline Offline
Posting Whiz in Training

Re: Problems with multiple queries

 
0
  #6
Jul 18th, 2009
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.

Here is the form page:
  1. <form action="write.php" type="submit" method="post">
  2.  
  3. <tr>
  4. <td align="left"></td>
  5. <td align="left">First Name:</td>
  6. <td align="left"><input name="first_name" type="text" /></td>
  7. <td align="left"></td>
  8. </tr>
  9.  
  10. <tr>
  11. <td align="left"></td>
  12. <td align="left">Last Name:</td>
  13. <td align="left"><input name="last_name" type="text" /></td>
  14. <td align="left"></td>
  15. </tr>
  16.  
  17. <tr>
  18. <td align="left"></td>
  19. <td align="left">Email:</td>
  20. <td align="left"><input name="email" type="text" /></td>
  21. <td align="left"></td>
  22. </tr>
  23.  
  24. <tr>
  25. <td align="left"></td>
  26. <td align="left">Password:</td>
  27. <td align="left"><input name="password" type="password" /></td>
  28. <td align="left"></td>
  29. </tr>
  30.  
  31. <tr>
  32. <td colspan="4"><img src="images/spacer_20px_high_white.jpg" /></td>
  33. </tr>
  34.  
  35. <tr>
  36. <td colspan="4"><div align="center"><input name="create" type="submit" value="Create My Account !" /></div></td>
  37. </tr>

And this is the 'write.php' the form submits to:

  1. <?php
  2.  
  3. $fname = $_POST['first_name'];
  4. $fname = ucwords($fname);
  5. $lname = $_POST['last_name'];
  6. $lname = ucwords($lname);
  7. $email = $_POST['email'];
  8. $email = strtolower($email);
  9. $pw = $_POST['password'];
  10.  
  11. // I omitted my connection info
  12.  
  13. $sql = "INSERT INTO users SET
  14.  
  15. username = '{$email}',
  16. password = '{$pw}',
  17. first_name = '{$fname}',
  18. last_name = '{$lname}' ";
  19.  
  20. if (!$sql)
  21. {
  22. exit;
  23. }
  24. echo "Thank you for becoming a member ". $fname . "!";
  25.  
  26. // Close connection
  27. mysql_close($dbcnx);
  28. ?>
And of far as I can see everything is fine with the table. I have no problems with anything else writing to other tables in the database.

My table is set up as follows:

table name: users

fields in order:

id > int, auto increment, primary key
username > varchar, 255
password > varchar, 255
first_name > varchar, 255
last_name > varchar, 255
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

Re: Problems with multiple queries

 
1
  #7
Jul 19th, 2009
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.
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 208
Reputation: CFROG is an unknown quantity at this point 
Solved Threads: 14
CFROG's Avatar
CFROG CFROG is offline Offline
Posting Whiz in Training

Re: Problems with multiple queries

 
0
  #8
Jul 19th, 2009
I've changed the actual code a few times since what you've seen so if you could be more specific as to what line 16 is regarding what you see now?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 955
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 126
ardav's Avatar
ardav ardav is offline Offline
Posting Shark

Re: Problems with multiple queries

 
0
  #9
Jul 19th, 2009
Look at Humbug's answer again and insert the mysql_query. Should work then. Good spot Humbug.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 208
Reputation: CFROG is an unknown quantity at this point 
Solved Threads: 14
CFROG's Avatar
CFROG CFROG is offline Offline
Posting Whiz in Training

Re: Problems with multiple queries

 
0
  #10
Jul 19th, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC