insert data into two separate table / 2 queries in same php file

Reply

Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

insert data into two separate table / 2 queries in same php file

 
0
  #1
Oct 11th, 2009
i can't get this from working. i want to execute these two queries ebcause i want to insert data to two separate table.

is it possible to do this with one query?

this is what i've got so far:
  1. $sql = "INSERT INTO login SET
  2. studNo = $studNo,
  3. userName = $user,
  4. password = $pass,
  5. dateRegistered = NOW()";
  6.  
  7. $sql2 = "INSERT INTO users SET
  8. studNo = $studNo,
  9. lastName = $lName,
  10. firstName = $fName,
  11. gender = $gender,
  12. bMonth = $month,
  13. bDay = $day,
  14. bYear = $year,
  15. address = $address,
  16. cellNo = $phone,
  17. email = $email";
  18.  
  19. if(mysql_num_rows(@mysql_query("SELECT * FROM classlist WHERE num = '$studNo'"))) {
  20. if(mysql_num_rows(@mysql_query("SELECT studNo FROM login WHERE studNo = '$studNo'"))) {
  21. $flag = true;
  22. $headMsg = "Registration Error";
  23. $msg = "You have already registered.";
  24. }
  25. else{
  26. @mysql_query($sql);
  27. @mysql_query($sq2);
  28. }
  29. }
  30. else {
  31. $flag = true;
  32. $headMsg = "Registration Unsuccessful";
  33. $msg = "Your student number doesn't exist in the database.";
  34. }
a joke.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 986
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 128
ardav's Avatar
ardav ardav is offline Offline
Posting Shark
 
-1
  #2
Oct 11th, 2009
I don't understand the purpose of your 'login' table. If it is to keep a login log - just use the id from the users table and a datetime. You'll need to make 2 sql queries on registering. Using mysql_insert_id() to get the id of the newly added user to the users table.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster
 
0
  #3
Oct 12th, 2009
Originally Posted by scias23 View Post
i can't get this from working. i want to execute these two queries ebcause i want to insert data to two separate table.

is it possible to do this with one query?

this is what i've got so far:
  1. $sql = "INSERT INTO login SET
  2. studNo = $studNo,
  3. userName = $user,
  4. password = $pass,
  5. dateRegistered = NOW()";
  6.  
  7. $sql2 = "INSERT INTO users SET
  8. studNo = $studNo,
  9. lastName = $lName,
  10. firstName = $fName,
  11. gender = $gender,
  12. bMonth = $month,
  13. bDay = $day,
  14. bYear = $year,
  15. address = $address,
  16. cellNo = $phone,
  17. email = $email";
  18.  
  19. if(mysql_num_rows(@mysql_query("SELECT * FROM classlist WHERE num = '$studNo'"))) {
  20. if(mysql_num_rows(@mysql_query("SELECT studNo FROM login WHERE studNo = '$studNo'"))) {
  21. $flag = true;
  22. $headMsg = "Registration Error";
  23. $msg = "You have already registered.";
  24. }
  25. else{
  26. @mysql_query($sql);
  27. @mysql_query($sq2);
  28. }
  29. }
  30. else {
  31. $flag = true;
  32. $headMsg = "Registration Unsuccessful";
  33. $msg = "Your student number doesn't exist in the database.";
  34. }
You can only insert into one table per SQL query.
http://dev.mysql.com/doc/refman/5.1/en/insert.html

However, there is nothing preventing you from doing two MySQL queries on the same PHP script.

It is hardly ever a good practice to force error suppression on function calls. ie: @mysql_query($sql);

If you remove the @ signs you'll see the errors that is preventing the SQL query from executing.

A better alternative is to set the error reporting level.

eg:

  1. ini_set('display_errors', '1');
  2. ini_set('error_reporting', E_ALL);
http://www.php.net/manual/en/functio...-reporting.php

or using:

  1. set_error_handler()

http://www.php.net/manual/en/functio...or-handler.php

When in development, you can choose to display errors. However, when in production mode, you can hide errors.

  1. ini_set('display_errors', '0');
  2. ini_set('error_reporting', E_ALL ^ E_NOTICE);
  3. ini_set('log_errors', '1');

This hides errors in the PHP script output, but they are still logged to the PHP error log.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster
 
0
  #4
Oct 12th, 2009
Here is a good reference to error handling in PHP: http://www.addedbytes.com/php/php-in...g-and-logging/
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster
 
0
  #5
Oct 12th, 2009
Originally Posted by digital-ether View Post
You can only insert into one table per SQL query.
i have two separate insert query:
  1. @mysql_query($sql);
  2. @mysql_query($sql2);

but the data don't appear on the tables. why?
a joke.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster
 
0
  #6
Oct 12th, 2009
Originally Posted by digital-ether View Post

If you remove the @ signs you'll see the errors that is preventing the SQL query from executing.
You won't know what is wrong, unless you view the errors.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 986
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 128
ardav's Avatar
ardav ardav is offline Offline
Posting Shark
 
0
  #7
Oct 12th, 2009
Ditto DE. However, I'm really confused as to why you've got all those loops just for inserting/checking for registration.

Have you confused table 'users' and 'classlist' in your code or are they separate entities? Is 'login' your 'registered users' table?
Could you supply info w.r.t the purpose of the tables. So far I see 3 tables:

users, classlist and login.

Your first post doesn't make it clear why you should have a separate 'login' and 'users' tables. Although, it may just be me being thick.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 12
Reputation: glycerine is an unknown quantity at this point 
Solved Threads: 1
glycerine glycerine is offline Offline
Newbie Poster
 
0
  #8
Oct 12th, 2009
Try this
  1. $sql = "INSERT INTO login (studNo, userName, password, dateRegistered) VALUES ($studNo, $user, $pass , NOW())";
  2.  
  3. $sql2 = "INSERT INTO users (studNo, lastName, firstName, gender, bMonth, bDay, bYear, address, cellNo, email) VALUES ($studNo, $lName, $fName, $gender, $month, $day, $year, $address, $phone, $email)";
  4.  
  5. if(mysql_num_rows(@mysql_query("SELECT * FROM classlist WHERE num = '$studNo'"))) {
  6. if(mysql_num_rows(@mysql_query("SELECT studNo FROM login WHERE studNo = '$studNo'"))) {
  7. $flag = true;
  8. $headMsg = "Registration Error";
  9. $msg = "You have already registered.";
  10. }
  11. else{
  12. @mysql_query($sql);
  13. @mysql_query($sq2);
  14. }
  15. }
  16. else {
  17. $flag = true;
  18. $headMsg = "Registration Unsuccessful";
  19. $msg = "Your student number doesn't exist in the database.";
  20. }
Last edited by glycerine; Oct 12th, 2009 at 7:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 26
Reputation: liamfriel is an unknown quantity at this point 
Solved Threads: 1
liamfriel's Avatar
liamfriel liamfriel is online now Online
Light Poster
 
0
  #9
Oct 13th, 2009
Remove error suppression (the @ in @mysql), then you might have more reason as to why the information is not going in.

Also you must make sure that the information going in the database it the correct data type i.e. only inserting numbers into INT field
They throw us away like yesterdays jam - Maurice Mossley

Please - rep if someone helps you, it can't be traded for stuff, but it's nice.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 26
Reputation: liamfriel is an unknown quantity at this point 
Solved Threads: 1
liamfriel's Avatar
liamfriel liamfriel is online now Online
Light Poster
 
0
  #10
Oct 13th, 2009
Remove error suppression (the @ in @mysql), then you might have more reason as to why the information is not going in.

Also you must make sure that the information going in the database it the correct data type i.e. only inserting numbers into INT field
They throw us away like yesterdays jam - Maurice Mossley

Please - rep if someone helps you, it can't be traded for stuff, but it's nice.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC