944,039 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1574
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 11th, 2009
0

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

Expand 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:
php Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Oct 11th, 2009
-1
Re: insert data into two separate table / 2 queries in same php file
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.
Sponsor
Featured Poster
Reputation Points: 1061
Solved Threads: 951
Disgraced Poster
ardav is offline Offline
6,708 posts
since Oct 2006
Oct 12th, 2009
0
Re: insert data into two separate table / 2 queries in same php file
Click to Expand / Collapse  Quote originally posted by scias23 ...
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:
php Syntax (Toggle Plain Text)
  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:

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

or using:

PHP Syntax (Toggle Plain Text)
  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.

PHP Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Oct 12th, 2009
0
Re: insert data into two separate table / 2 queries in same php file
Here is a good reference to error handling in PHP: http://www.addedbytes.com/php/php-in...g-and-logging/
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Oct 12th, 2009
0
Re: insert data into two separate table / 2 queries in same php file
You can only insert into one table per SQL query.
i have two separate insert query:
php Syntax (Toggle Plain Text)
  1. @mysql_query($sql);
  2. @mysql_query($sql2);

but the data don't appear on the tables. why?
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Oct 12th, 2009
0
Re: insert data into two separate table / 2 queries in same php file

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Oct 12th, 2009
0
Re: insert data into two separate table / 2 queries in same php file
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.
Sponsor
Featured Poster
Reputation Points: 1061
Solved Threads: 951
Disgraced Poster
ardav is offline Offline
6,708 posts
since Oct 2006
Oct 12th, 2009
0
Re: insert data into two separate table / 2 queries in same php file
Try this
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
glycerine is offline Offline
17 posts
since Feb 2009
Oct 13th, 2009
0
Re: insert data into two separate table / 2 queries in same php file
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
Reputation Points: 13
Solved Threads: 13
Junior Poster
liamfriel is offline Offline
101 posts
since Oct 2009
Oct 13th, 2009
0
Re: insert data into two separate table / 2 queries in same php file
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
Reputation Points: 13
Solved Threads: 13
Junior Poster
liamfriel is offline Offline
101 posts
since Oct 2009

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.
Message:
Previous Thread in PHP Forum Timeline: How to force web page to open in internet explorer
Next Thread in PHP Forum Timeline: MSqyl error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC