Search the Community
Showing results for tags 'mysql_insert_id'.
-
I need to insert a new row in to a table, then grab the ID of taht row and update another table. What I have is something like this: $sql="INSERT INTO leads (field1, field2) VALUES('$value1','$value2')"; $query = mysql_query($sql); $lastID = mysql_insert_id(); $update = "UPDATE contacts SET leadID = $lastID WHERE contactID = $contactID"; $updateQuery = mysql_query($update); Everything works fine.. except that it inserts duplicate rows into the leads table. I have tried putting the update query into an if statement and it did the samething(this was just to try "something"). If I remove $lastID = mysql_insert_id(); it inserts just one row but obviously does not update the contacts table. So I am pretty sure it has to to with mysql_insert_id(). I need it to update the contacts table with the new id of the row inserted into the leads table. Any ideas would be greatly appreciated.
-
I've been trying to generate a database for a quiz game, where players can submit their questions to a database and send their friends the quiz that they have created. I have three problems i hope you can help: 1. I'm having trouble storing the id generated from my last query into a table after the page has changed using the header function, it always returns a 0. 2. I am also having trouble with the final page echoing both an error message about submission and a success message at the same time. 3. I also have a problem with the header link not being able to modify, although it does take you to that page. When the final submit button is pressed I get this: Please complete form before submission Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/Quiz/go.php:33) in /Applications/MAMP/htdocs/Quiz/go.php on line 52 Notice: Undefined variable: quizid in /Applications/MAMP/htdocs/Quiz/go.php on line 73 Quiz submitted sucessfully my code is like this: <?php //error reporting error_reporting(E_ALL); //connect to mysql $con = mysql_connect("localhost","dinita","*****"); if(!$con) { die('Could not connect: '.mysql_error()); } else{ // SELECT DATABASE mysql_select_db("quizCreation", $con); //Which form is it? if(isset($_POST['title']) &&!empty($_POST['title'])) { //accept POST data $name = $_POST['name']; $desc =$_POST['desc']; } //check if POST data is complete (all required fields are filled in) //if POST data is not complete return an error if(empty($name)) { echo "Please complete form before submission"; } else { //insert quiz title $sql=" INSERT INTO quizName (Name, Description) VALUES ('$name','$desc')"; mysql_query($sql, $con); //get ID for quiz title $quizid = mysql_insert_id(); if (empty($quizid)){ die("No Quiz id"); } } header("Location: http://localhost:8888/Quiz/quiz.creator.html"); } if (isset($_POST['creator'])&&!empty($_POST['creator'])) { //accept POST data $question = $_POST['question']; } //check if POST data is complete (all required fields are filled in) //if POST data is not complete return an error if(empty($question) && empty($ans1) && empty($ans2) && empty($ans3) && empty($ans4)) { echo "Please complete form before submission"; } else { //insert question $sql= "INSERT INTO questions (text, quiz_ID) VALUES ('$question', '$quizid')"; mysql_query($sql, $con); //get id of inserted question $id = mysql_insert_id(); //insert answers + question id for($i=1; $i<=4; $i++) { $correct = 0; if($_POST['radio'] == "radio".$i) { $correct = 1; } $answer = $_POST['answer'.$i]; $sql=" INSERT INTO answers (question_ID, answer, correct) VALUES ('$id','$answer','$correct')"; mysql_query($sql, $con); } } //show message to say insert has completed successfully echo "Quiz submitted sucessfully"; //close connection to mysql mysql_close($con); Thanks in advance for any help you can offer.