944,204 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 451
  • PHP RSS
Oct 22nd, 2009
0

Need help in inserting data to two tables

Expand Post »
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.


PHP Syntax (Toggle Plain Text)
  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. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wayz1229 is offline Offline
11 posts
since Oct 2009
Oct 22nd, 2009
0
Re: Need help in inserting data to two tables
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)
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008
Oct 22nd, 2009
0
Re: Need help in inserting data to two tables
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)
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008
Oct 22nd, 2009
0
Re: Need help in inserting data to two tables
no its right, mysql_insert_id() is the function if you looking for the id of last insert.
Is that you looking?
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Oct 25th, 2009
0
Re: Need help in inserting data to two tables
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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wayz1229 is offline Offline
11 posts
since Oct 2009
Oct 28th, 2009
0
Re: Need help in inserting data to two tables
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:

PHP Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 82
Solved Threads: 74
Posting Pro in Training
Graphix is offline Offline
401 posts
since Aug 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: Open Source PHP Financial Modules
Next Thread in PHP Forum Timeline: My head is going to explode if I can't fix this. PLEASE HELP





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


Follow us on Twitter


© 2011 DaniWeb® LLC