Need help in inserting data to two tables

Reply

Join Date: Oct 2009
Posts: 11
Reputation: wayz1229 is an unknown quantity at this point 
Solved Threads: 0
wayz1229 wayz1229 is offline Offline
Newbie Poster

Need help in inserting data to two tables

 
0
  #1
Oct 22nd, 2009
I'm having problem with the script below.
The atitle getting the qid from the last question inserted in the table. I need to have atitle with the corresponding qid.

for example:
qtitle (qid=1)
- atitle (qid=1)
- atitle (qid=1)

qtitle (qid=2)
- atitle (qid=2)
- atitle (qid=2)

can anyone please help.. thanks in advance.


  1. <?php
  2. require_once('auth.php');
  3. require_once('config.php');
  4.  
  5. $date=date("F j, Y, g:i a");
  6. $stitle = $_POST['stitle'];
  7. $stitle = ucwords($stitle);
  8. $name = $_SESSION['SESS_NAME'];
  9. $member_id = $_SESSION['SESS_MEMBER_ID'];
  10.  
  11.  
  12. $query = "INSERT INTO survey (stitle, sdate, name, member_id) VALUES ('$stitle', '$date', '$name', '$member_id')";
  13. $result = mysql_query($query) or die("ERROR: $query.".mysql_error());
  14.  
  15. $sid = mysql_insert_id();
  16.  
  17. // reset variables
  18. unset($query);
  19. unset ($result);
  20.  
  21. foreach ($_POST['questions'] as $q) {
  22. if (trim($q) != '') {
  23. $qtitles[] = $q;
  24. }
  25. }
  26.  
  27. foreach ($qtitles as $qtitle) {
  28. $query = "INSERT INTO questions (sid, qtitle) VALUES ('$sid', '$qtitle')";
  29. $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
  30. }
  31.  
  32. $qid = mysql_insert_id();
  33.  
  34. // reset variables
  35. unset($query);
  36. unset ($result);
  37.  
  38. foreach ($_POST['options'] as $o) {
  39. if (trim($o) != '') {
  40. $atitles[] = $o;
  41. }
  42. }
  43.  
  44. foreach ($atitles as $atitle) {
  45. $query = "INSERT INTO answers (qid, sid, atitle, acount) VALUES ('$qid', '$sid', '$atitle', '0')";
  46. $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
  47. }
  48.  
  49. //Check whether the query was successful or not
  50. if($result) {
  51. header("location: surveypreview.php");
  52. exit();
  53. }else {
  54. echo '<script>alert("Query Failed. Please try again !!!");</script>';
  55. echo '<script>history.back(1);</script>';
  56. }
  57.  
  58. mysql_close();
  59. ?>
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 244
Reputation: samarudge is an unknown quantity at this point 
Solved Threads: 19
samarudge samarudge is offline Offline
Posting Whiz in Training
 
0
  #2
Oct 22nd, 2009
Not 100% sure what you are trying to do but mysql(i)_insert_id() can get the primary key from the last insert statement
http://php.net/mysql_insert_id
http://php.net/mysqli_insert_id (If using MYSQLI link)
My Blog, Life and everything that matters to me - SamRudge.co.uk

2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 244
Reputation: samarudge is an unknown quantity at this point 
Solved Threads: 19
samarudge samarudge is offline Offline
Posting Whiz in Training
 
0
  #3
Oct 22nd, 2009
Not 100% sure what you are trying to do but mysql(i)_insert_id() can get the primary key from the last insert statement
http://php.net/mysql_insert_id
http://php.net/mysqli_insert_id (If using MYSQLI link)
My Blog, Life and everything that matters to me - SamRudge.co.uk

2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 527
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 61
network18 network18 is offline Offline
Posting Pro
 
0
  #4
Oct 22nd, 2009
no its right, mysql_insert_id() is the function if you looking for the id of last insert.
Is that you looking?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 11
Reputation: wayz1229 is an unknown quantity at this point 
Solved Threads: 0
wayz1229 wayz1229 is offline Offline
Newbie Poster
 
0
  #5
Oct 25th, 2009
the above script insert the data to database. let say i hav 2 qtitle, and each qtitle has 2 atitle..
so in table1, there are total of 2 qtitle
and in table2, there are total of 4 atitle.

but the problem with the script is that, all the atitle in table2 is having the qid of last inserted qtitle.. thats the error in above script.. atitle should have the qid based on the qtitle..
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 78
Reputation: Graphix is an unknown quantity at this point 
Solved Threads: 17
Graphix's Avatar
Graphix Graphix is offline Offline
Junior Poster in Training
 
0
  #6
Oct 28th, 2009
Why would you use 2 tables for the exactly same purpose? Why don't you just make the following two table:

table "surveyresults":
qa_id INT(9) AUTO_INCREMENT NOT NULL,
question CHAR(255),
answer LONGTEXT,
sid INT(9) NOT NULL,

And use the following code:

  1. <?php
  2. require_once('auth.php');
  3. require_once('config.php');
  4. //
  5. // Retrieving which survey it is and who filled it in
  6. //
  7. $date=date("F j, Y, g:i a");
  8. $stitle = $_POST['stitle'];
  9. $stitle = ucwords($stitle);
  10. $name = $_SESSION['SESS_NAME'];
  11. $member_id = $_SESSION['SESS_MEMBER_ID'];
  12.  
  13. //
  14. // Saving in the database who filled in the survey, what survey and when
  15. //
  16.  
  17. $query = "INSERT INTO survey (stitle, sdate, name, member_id) VALUES ('$stitle', '$date', '$name', '$member_id')";
  18. $result = mysql_query($query) or die("ERROR: $query.".mysql_error());
  19.  
  20. $sid = mysql_insert_id();
  21.  
  22. // reset variables
  23. unset($query);
  24. unset ($result);
  25.  
  26. //
  27. // Retrieving the answers
  28. //
  29. foreach ($_POST['options'] as $o) {
  30. if (trim($o) != '') {
  31. $answers[] = $o;
  32. }
  33. }
  34. //
  35. // Retrieving the questions
  36. //
  37. foreach ($_POST['questions'] as $q) {
  38. if (trim($q) != '') {
  39. $questions[] = $q;
  40. }
  41. }
  42.  
  43. //
  44. // Counting the amount of questions and answers
  45. //
  46. $question_amount = count($questions);
  47. $answer_amount = count($answers);
  48.  
  49. //
  50. // Inserting the answers and questions
  51. //
  52.  
  53. for ($i=0; $i < $question_amount; $i++) {
  54. $answer = $answers[$i];
  55. $question = $questions[$i];
  56. $query = "INSERT INTO surveyresults (sid, question, answer) VALUES ('$sid', '$question','$answer')";
  57. $result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
  58. }
  59.  
  60. //Check whether the query was successful or not
  61. if($result) {
  62. header("location: surveypreview.php");
  63. exit();
  64. }else {
  65. echo '<script>alert("Query Failed. Please try again !!!");</script>';
  66. echo '<script>history.back(1);</script>';
  67. }
  68.  
  69. mysql_close();
  70. ?>
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