| | |
Need help in inserting data to two tables
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
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.
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)
<?php require_once('auth.php'); require_once('config.php'); $date=date("F j, Y, g:i a"); $stitle = $_POST['stitle']; $stitle = ucwords($stitle); $name = $_SESSION['SESS_NAME']; $member_id = $_SESSION['SESS_MEMBER_ID']; $query = "INSERT INTO survey (stitle, sdate, name, member_id) VALUES ('$stitle', '$date', '$name', '$member_id')"; $result = mysql_query($query) or die("ERROR: $query.".mysql_error()); $sid = mysql_insert_id(); // reset variables unset($query); unset ($result); foreach ($_POST['questions'] as $q) { if (trim($q) != '') { $qtitles[] = $q; } } foreach ($qtitles as $qtitle) { $query = "INSERT INTO questions (sid, qtitle) VALUES ('$sid', '$qtitle')"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); } $qid = mysql_insert_id(); // reset variables unset($query); unset ($result); foreach ($_POST['options'] as $o) { if (trim($o) != '') { $atitles[] = $o; } } foreach ($atitles as $atitle) { $query = "INSERT INTO answers (qid, sid, atitle, acount) VALUES ('$qid', '$sid', '$atitle', '0')"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); } //Check whether the query was successful or not if($result) { header("location: surveypreview.php"); exit(); }else { echo '<script>alert("Query Failed. Please try again !!!");</script>'; echo '<script>history.back(1);</script>'; } mysql_close(); ?>
•
•
Join Date: May 2008
Posts: 244
Reputation:
Solved Threads: 19
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)
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 =)
2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
•
•
Join Date: May 2008
Posts: 244
Reputation:
Solved Threads: 19
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)
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 =)
2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
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..
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..
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:
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)
<?php require_once('auth.php'); require_once('config.php'); // // Retrieving which survey it is and who filled it in // $date=date("F j, Y, g:i a"); $stitle = $_POST['stitle']; $stitle = ucwords($stitle); $name = $_SESSION['SESS_NAME']; $member_id = $_SESSION['SESS_MEMBER_ID']; // // Saving in the database who filled in the survey, what survey and when // $query = "INSERT INTO survey (stitle, sdate, name, member_id) VALUES ('$stitle', '$date', '$name', '$member_id')"; $result = mysql_query($query) or die("ERROR: $query.".mysql_error()); $sid = mysql_insert_id(); // reset variables unset($query); unset ($result); // // Retrieving the answers // foreach ($_POST['options'] as $o) { if (trim($o) != '') { $answers[] = $o; } } // // Retrieving the questions // foreach ($_POST['questions'] as $q) { if (trim($q) != '') { $questions[] = $q; } } // // Counting the amount of questions and answers // $question_amount = count($questions); $answer_amount = count($answers); // // Inserting the answers and questions // for ($i=0; $i < $question_amount; $i++) { $answer = $answers[$i]; $question = $questions[$i]; $query = "INSERT INTO surveyresults (sid, question, answer) VALUES ('$sid', '$question','$answer')"; $result = mysql_query($query) or die("ERROR: $query. ".mysql_error()); } //Check whether the query was successful or not if($result) { header("location: surveypreview.php"); exit(); }else { echo '<script>alert("Query Failed. Please try again !!!");</script>'; echo '<script>history.back(1);</script>'; } mysql_close(); ?>
![]() |
Similar Threads
- The operation has timed out error while inserting data through web service (IT Professionals' Lounge)
- Join Two data tables (C#)
- Copy data from 2 tables in 2 databases (MS SQL)
- Insert data into two tables using php and mysql (MySQL)
- Adding dynamic rows and inserting the data in database (JSP)
- problem in inserting data into the database (PHP)
- Inserting data to a mysql table problems :( (PHP)
- Inserting datas from two tables to third table with a command. is it possible (MySQL)
Other Threads in the PHP Forum
- Previous Thread: Open Source PHP Financial Modules
- Next Thread: My head is going to explode if I can't fix this. PLEASE HELP
| Thread Tools | Search this Thread |
.htaccess access alexa apache api array beginner binary broken cakephp checkbox class cms code convert cron curl database date directory display dropdown dynamic echo email encode error fairness file files folder form forms function functions google hack href htaccess html image include indentedsubcategory insert ip javascript joomla limit link login loop mail mail() menu methods mlm multiple multipletables mysql network newsletters oop passwords paypal pdf php problem provider query radio random recursion redirect remote script search secure securephp server sessions simple sms source space sql syntax system table tutorial update upload url user validator variable variables video voteup web youtube





