-
Posts
24 -
Joined
-
Last visited
Everything posted by dinita
-
I'm a big fan of Big Brother UK and as a fun project I have built a Twitter hash tag counter to count the battle between the contestants this year. Using the API from Topsy.com I have built a counter that shows the amount of tweets daily, monthly and hourly. However, I wondered if it was possible for me to calculate the amount of tweets say between the time the page was loaded and the time a user clicks stop so I can see how the numbers have change during the show. Would it be possible to use the setTimeOut() function or is there a better way of doing this? heres my code: <?php error_reporting(0); $jsonurl = "http://otter.topsy.com/searchcount.json?q=%23teamrylan&type=tweet&apikey=FJWCK5YSZIBEUZX4HYJQAAAAABLTPFQ3DVIQAAAAAAAFQGYA"; $json = file_get_contents($jsonurl, 0, null, null); $json_output = json_decode($json, true); $json_output = array_values($json_output); foreach ($json_output as $key => $val) { $hourly = number_format($val["h"]); $daily = number_format($val["d"]); $weekly = number_format($val["w"]); $monthly = number_format($val["m"]); $alltime = number_format($val["a"]); } echo '<strong>How many times have people tweeted #TeamRylan</strong><br /><br />'; echo 'In the last Hour: ' . $hourly . '<br />'; echo 'In the last Day: ' . $daily . '<br />'; echo 'In the last Week: ' . $weekly . '<br />'; echo 'In the last Month: ' . $monthly . '<br />'; echo 'Ever: ' . $alltime . '<br />'; ?>
-
Trouble Displaying Data From A Mysql Database Into Flash
dinita replied to dinita's topic in MySQL Help
My code is below, but in short I created arrays to represent each of the dynamic text fields in my Flash project. Its probably not the smartest way to do this but I'm just a beginner. <?php error_reporting(-1); session_name("name"); session_start(); //functions function redirect_page($url) { if(!headers_sent()) { header("Location: ".$url); exit; } else { echo '<script type="text/javascript">'; echo 'window.location.href="'.$url.'";'; echo '</script>'; echo '<noscript>'; echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; echo '</noscript>'; exit; } } function getquestions($id) { $sql =mysql_query("select text FROM questions WHERE quiz_ID =$id "); $questions = array(); while($row = mysql_fetch_row($sql)) { $questions[] = $row[0]; } return $questions; } function getquesid($id) { $sql =mysql_query("select ID FROM questions WHERE quiz_ID =$id "); $questions = array(); while($row = mysql_fetch_row($sql)) { $questions[] = $row[0]; } return $questions; } function getanswers() { foreach ($quesid as $id) { $sql = mysql_query("select answer FROM answers WHERE question_ID= $id "); } $answers = array(); while($row =mysql_fetch_row($sql)) { $answers[] = $row[0]; } return $answers; } function getcorrect($id) { $sql = mysql_query("SELECT correct FROM answers WHERE question_ID= $id "); $correct =array(); while($row =mysql_fetch_assoc($sql)) { $correct[] =$row["correct"]; } return $correct; } //Connect to Database $con = mysql_connect(); if(!$con) { die('Could not connect: '.mysql_error()); } else { // SELECT DATABASE mysql_select_db("quizCreation", $con); } //GET NAME if(isset($_SESSION['quizid'])) { $id =$_SESSION['quizid']; } else { $id = 6; } //get quiz name $sql = mysql_query("SELECT Name FROM quizName WHERE ID= '$id'"); while ($row = mysql_fetch_array($sql)) { $name = $row['Name']; } //use quiz id to output array of question ID's and text $question = implode ("/",getquestions($id)); $quesid =getquesid($id); $answers = array(); // use question ID's to output 4 arrays foreach ($quesid as $id) { $sql = mysql_query("select answer FROM answers WHERE question_ID= $id "); while($row =mysql_fetch_row($sql)) { $answers[] = $row[0]; } } $one = array( $answers[0],$answers[4],$answers[8],$answers[12],$answers[16],$answers[20],$answers[24],$answers[28]); $two = array( $answers[1],$answers[5],$answers[9],$answers[13],$answers[17],$answers[21],$answers[25],$answers[29]); $three =array( $answers[2],$answers[6],$answers[10],$answers[14],$answers[18],$answers[22],$answers[26],$answers[30]); $four =array( $answers[3],$answers[7],$answers[11],$answers[15],$answers[19],$answers[23],$answers[27],$answers[31]); $answerone = implode ("/",$one); $answertwo = implode ("/", $two); $answerthree = implode ("/", $three); $answerfour =implode ("/", $four); // use question ID's to output array of correct answers $cor = array(); foreach ($quesid as $id) { $sql = mysql_query("select correct FROM answers WHERE question_ID= $id "); while($row =mysql_fetch_row($sql)) { $cor[] = $row[0]; } } $cor1 =array ($cor[0],$cor[4],$cor[8],$cor[12],$cor[16],$cor[20],$cor[24],$cor[28]); $cor2 =array ($cor[1],$cor[5],$cor[9],$cor[13],$cor[17],$cor[21],$cor[25],$cor[29]); $cor3 =array ($cor[2],$cor[6],$cor[10],$cor[14],$cor[18],$cor[22],$cor[26],$cor[30]); $cor4 =array ($cor[3],$cor[7],$cor[11],$cor[15],$cor[19],$cor[23],$cor[27],$cor[31]); $correctone = implode ("/", $cor1); $correcttwo = implode ("/", $cor2); $correctthree = implode("/", $cor3); $correctfour = implode("/", $cor4); //echo this information echo "name=". urlencode($name); echo "&questions=" . urlencode($question); echo "&answerone=". urlencode($answerone); echo "&answertwo=". urlencode($answertwo); echo "&answerthree=". urlencode($answerthree); echo "&answerfour=". urlencode($answerfour); echo "&correctone=".urlencode($correctone); echo "&correcttwo=".urlencode($correcttwo); echo "&correctthree=".urlencode($correctthree); echo "&correctfour=".urlencode($correctfour); mysql_close($con); -
Hi Neil, thanks for getting back to me I tried this and after clicking the link back to the home page via another php file which just prints the username and then redirects to the home page using the HTTP Header Response, this gives me an undefined variable error.
-
I have a question about passing a variable from php to html and back again Ideally i'd like to pass a customers name inputted into php file using a form out into the form submission page as a hidden form input and then use this value in a new php file when a button is pressed to send them back to the home page. the aim is to show the users name once they have completed the form. I've had a look around and many people suggest using code similar to this: value="<?php echo htmlspecialchars($name); ?>" but if i include this in my html form do I not have to save the html document as php? I hope someone can help, thanks in advance!
-
Variable Works When Echoed But Not When Returned
dinita replied to dinita's topic in PHP Coding Help
thanks for responding but this is solved! -
Please excuse my terrible ability to explain what I'm trying to do, my aim is to draw questions and answers from a database to be output in flash. My problem is: I have created an array listing all of the question ids for a particular quiz, I would like to create another array that lists all the enteries in the database that have those id's so far I have managed to create an array which shows the last ID's answers. what i want is to create 8 seperate arrays not just one? foreach ($quesid as $id) { $sql = mysql_query("select answer FROM answers WHERE question_ID= $id "); $answers = array(); while($row =mysql_fetch_row($sql)) { $answers[] = $row[0]; } } print_r ($answers); echo "questions=" . $question."&"; print_r ($quesid); here is what is output: Array ( [0] => Bulgaria [1] => Nicaragua [2] => Albania [3] => Romania ) questions=The ‘Sea Swallow’ is an alternative name for which bird?/In which sport would you see a ‘Western Roll’?/Who is better known as ‘Herbert Khaury’?/'Diet' is the parliament of which country?/What is the real first name of Coco Chanel?/'The Aztecs' were natives of which country?/What was invented by‘O.A. North’ in 1869?/King Zog was the ruler of which country?&Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 ) the first array is what i need but i need it 7 other times.
-
I'm trying to make it so that my code takes an id given at the beginning and uses this to return a set of arrays. Can anyone explain why my code might work (meaning echo the desired arrays ) like this: 1Array ( [0] => The ‘Sea Swallow’ is an alternative name for which bird? [1] => In which sport would you see a ‘Western Roll’? [2] => Who is better known as ‘Herbert Khaury’? [3] => 'Diet' is the parliament of which country? [4] => What is the real first name of Coco Chanel? [5] => 'The Aztecs' were natives of which country? [6] => What was invented by‘O.A. North’ in 1869? [7] => King Zog was the ruler of which country? ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 ) when I echo the variable id and not when I use the return function? when the return function is used I recieve no errors but nothing is printed either. Ideally I'd like the id variable to be returned as I don't really want to see it echoed. I know its probably something really simple but I just can't work it out, any help would be much appreciated, code below: <?php error_reporting(-1); function getquestions($id) { $sql =mysql_query("select text FROM questions WHERE quiz_ID =$id "); $questions = array(); while($row = mysql_fetch_row($sql)) { $questions[] = $row[0]; } return $questions; } function getquesid($id) { $sql =mysql_query("select ID FROM questions WHERE quiz_ID =$id "); $questions = array(); while($row = mysql_fetch_row($sql)) { $questions[] = $row[0]; } return $questions; } //Connect to Database $con = mysql_connect("localhost","dinita", "3nd3m0luk"); if(!$con) { die('Could not connect: '.mysql_error()); } else { // SELECT DATABASE mysql_select_db("quizCreation", $con); } //GET NAME $name ="my quiz"; //get quiz id $sql = mysql_query("SELECT ID FROM quizName WHERE Name= '$name'"); while ($row = mysql_fetch_array($sql)) { $id = $row['ID']; } echo $id; $quizid =$id; $question = getquestions($quizid); $quesid =getquesid($quizid); print_r ($question); print_r($quesid); //use quiz id to output array of question ID's and text // use question ID's to output 4 arrays mysql_close($con);
-
your right I was doing it wrong! I thought I needed the array to split into separate variables so the Actionscript could read these and display them into separate boxes by calling each variable. Instead I've used the array to echo a string split using backslashes and removed the backslashes in the Actionscript using the split function and then turned that string into an array. Thanks for being so patient with me!
-
quick question really.. trying to call an extract function where the keys of the array are numbers? a variable cannot start with a number, so how would I call my new variables?
-
Assigning Specific Array Keys To Seperate Variables
dinita replied to dinita's topic in PHP Coding Help
Thank you, Dan and everyone else for your input! -
Assigning Specific Array Keys To Seperate Variables
dinita replied to dinita's topic in PHP Coding Help
thanks for all your help, but changing the numbers from strings to a number makes no difference, I still receive the same Notices. I understand that its probably bad practice to use sequentially numbered variables but I haven't really been having much luck with arrays, I've only been learning php for 2 weeks, so please excuse my lack of knowledge. As I tried to explain I'm trying to understand how to call each part of the array, at the moment this is causing undefined notices. maybe it would help if I explained what I was trying to do, this file will be sending variables to dynamic text fields in flash that will display the questions and answers from my database. -
Assigning Specific Array Keys To Seperate Variables
dinita replied to dinita's topic in PHP Coding Help
ok, I understand so how would I go about assigning each of these values to different variables? or should I be calling more than one field in each record? -
Assigning Specific Array Keys To Seperate Variables
dinita replied to dinita's topic in PHP Coding Help
here is the full code //functions function get_id( $table) { $sql = mysql_query("select * FROM $table") ; while ($row =mysql_fetch_array($sql)) { return $row['ID']; } } function getquestions($id) { $sql =mysql_query("select text FROM questions WHERE quiz_ID =$id "); while($row = mysql_fetch_row($sql)) { return $row ; } } function getanswers() { $sql = mysql_query("select answer FROM answers WHERE question_ID= 1 "); while($row =mysql_fetch_row($sql)) { print_r (array_keys($row)); $ans1 = $row['0']; $ans2 = $row['1']; $ans3 = $row['2']; $ans4 = $row['3']; echo $ans1 . $ans2. $ans3. $ans4 ; } } //Connect to Database $con = mysql_connect("localhost","dinita","8888888"); if(!$con) { die('Could not connect: '.mysql_error()); } else { // SELECT DATABASE mysql_select_db("quizCreation", $con); ///Actual code $quizid = get_id("quizName", "ID"); $questionid = get_id("questions","quiz_ID"); if ($quizid == $questionid) { $question = getquestions("1"); echo $question[0]; getanswers(1); mysql_close($con); } } -
is it possible to store the keys of an array in to variables? currently my code looks like this: function getanswers() { $sql = mysql_query("select answer FROM answers WHERE question_ID= 1 "); while($row =mysql_fetch_row($sql)) { //print_r (array_keys($row)); $ans1 = $row['0']; $ans2 = $row; $ans3 = $row; $ans4 = $row; echo $ans1 . $ans2. $ans3. $ans4 ; } } and all I get are undefined offset notices what am i doing wrong? I'd appreciate any advice the output looks like this: The ‘Sea Swallow’ is an alternative name for which bird? Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29 Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30 Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31 Seagull Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29 Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30 Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31 Penguin Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29 Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30 Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31 Tern Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29 Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30 Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31 Cormorant Just in case it helps when I call the array keys they show this: Array ( [0] => 0 )Array ( [0] => 0 )Array ( [0] => 0 )Array ( [0] => 0 ) Thanks in advance to anyone who can help!
-
I've only been learning php for two weeks so please excuse my ignorance, I would like to store an array of data from a mysql table in a variable to be called as and when its needed in my script. i have written this function and used a vardump just to check wether the function was working: function getquestions($id) { $sql =mysql_query("select text FROM questions WHERE quiz_ID =$id "); while($row = mysql_fetch_array($sql)) { var_dump($row) ; } } ///Actual code $quizid = get_id("quizName", "ID"); $questionid = get_id("questions","quiz_ID"); if ($quizid == $questionid) { $question = getquestions("1"); } BUT my array lists all of my row values twice like this: array(2) { [0]=> string(68) "The ‘Sea Swallow’ is an alternative name for which bird?" ["text"]=> string(68) "The ‘Sea Swallow’ is an alternative name for which bird?" } array(2) { [0]=> string(58) "In which sport would you see a ‘Western Roll’?" ["text"]=> string(58) "In which sport would you see a ‘Western Roll’?" } array(2) { [0]=> string(53) "Who is better known as ‘Herbert Khaury’?" ["text"]=> string(53) "Who is better known as ‘Herbert Khaury’?" } array(2) { [0]=> string(42) "'Diet' is the parliament of which country?" ["text"]=> string(42) "'Diet' is the parliament of which country?" } array(2) { [0]=> string(43) "What is the real first name of Coco Chanel?" ["text"]=> string(43) "What is the real first name of Coco Chanel?" } array(2) { [0]=> string(43) "'The Aztecs' were natives of which country?" ["text"]=> string(43) "'The Aztecs' were natives of which country?" } array(2) { [0]=> string(54) "What was invented by‘O.A. North’ in 1869?" ["text"]=> string(54) "What was invented by‘O.A. North’ in 1869?" } array(2) { [0]=> string(40) "King Zog was the ruler of which country?" ["text"]=> string(40) "King Zog was the ruler of which country?" } I'd appreciate any explanation on how i stop this.
-
Just wondered if anyone could offer any advice on parsing information from a mySQL database into a Flash movie clip with 5 dynamic text fields. the flash movie clip loops eight times creating an id on ever loop, it will display questions for a quiz game. it has 5 text fields to hold the questions and its possible answers, I would like to draw the questions and answers from the database using php. The tables in my database are structured like this: Table structure for table `answers` -- CREATE TABLE `answers` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `ANSWER` varchar(80) NOT NULL, `CORRECT` tinyint(4) NOT NULL DEFAULT '0', `question_ID` int(11) NOT NULL, PRIMARY KEY (`ID`), UNIQUE KEY `ID` (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=33 ; -- -------------------------------------------------------- -- -- Table structure for table `questions` -- CREATE TABLE `questions` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `text` varchar(80) NOT NULL, `quiz_ID` int(11) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ; -- -------------------------------------------------------- -- -- Table structure for table `quizName` -- CREATE TABLE `quizName` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(45) NOT NULL, `Description` varchar(45) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; I planned to first start a connection to a database, then create a function that select's the data from the database using an ISSET to know what number of the loop we are on. so i guess my questions are: 1) is it possible to use the id number created every cycle of the loop in the flash actionscript in my php? 2) could I use this id to select the questions using my Question ID? hope someone can help.
-
Thanks you all for all your help! In the end I created a few functions to fix the problems i encountered- The first one inserts data into a database and returns the id like this: function insert_data($table_name, $data) { $id = false; if(!is_array($data)) { die('You must supply an array of data to be inserted'); } $con = mysql_connect("localhost", "dinita", "*********"); if(!$con) { die('could not connect: '.mysql_error()); } else { $sql = "INSERT INTO ".$table_name." (".implode(',', array_keys($data)).") VALUES ('".implode("','", $data)."')"; echo $sql; mysql_select_db("quizCreation", $con); mysql_query($sql, $con); $id = mysql_insert_id(); mysql_close($con); } return $id; } and another to redirect even when headers are sent using: function redirect_page($url = 'http://localhost:8888/Quiz/quiz.creator.html') { if(!headers_sent()) { header("Location: ".$url); exit; } else { echo '<script type="text/javascript">'; echo 'window.location.href="'.$url.'";'; echo '</script>'; echo '<noscript>'; echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; echo '</noscript>'; exit; } }
-
thanks Barand that definately helped fix one of my problems I now only have two problems: 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. I think the problem lies somewhere in this bit of the code: if (isset($_POST['creator'])&&!empty($_POST['creator'])) { //accept POST data $question = $_POST['question']; $quizid =$_SESSION['quizid']; } //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); 3. I also have a problem with the header link not being able to modify, i get the warning: Cannot modify header information - headers already sent
-
is there a way to store and id once you get a new connection? or even to change the page without starting a new connection? $_POST['title'] is the name of a hidden form field in my html, i'm using it as an ID for the different forms.
-
I have three tables each with a primary key like this: -- phpMyAdmin SQL Dump -- version 3.5.1 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Oct 04, 2012 at 12:37 PM -- Server version: 5.5.25 -- PHP Version: 5.4.4 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; -- -- Database: `quizCreation` -- -- -------------------------------------------------------- -- -- Table structure for table `answers` -- CREATE TABLE `answers` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `ANSWER` varchar(80) NOT NULL, `CORRECT` tinyint(4) NOT NULL DEFAULT '0', `question_ID` int(11) NOT NULL, PRIMARY KEY (`ID`), UNIQUE KEY `ID` (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; -- -------------------------------------------------------- -- -- Table structure for table `questions` -- CREATE TABLE `questions` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `text` varchar(80) NOT NULL, `quiz_ID` int(11) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; -- -------------------------------------------------------- -- -- Table structure for table `quizName` -- CREATE TABLE `quizName` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(45) NOT NULL, `Description` varchar(45) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-
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.
-
Populating My Database With Information From A Html Form
dinita replied to dinita's topic in MySQL Help
Thanks for all your help before I recieevd a blank page with no error message now after taking your advice atleast I get an error it shows: "Error: Cannot add or update a child row: a foreign key constraint fails (`quizCreator`.`answers`, CONSTRAINT `answers_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `questions` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE)" ... I think I have made a mistake in setting up the Foreign key but i'm not sure what it is. Thanks Again, I really appreciate all your advice! -
Hello I'm new to PHP and mySQL, I am trying to populate a database created using MySQL Workbench using information submitted via a form in my website. I have followed as many tutorials as i can and there appears to be an error in the php file, but i'm not sure what it is. my form is coded like this: <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <form id="quizCreator" name="quizCreator" method="post" action="insert.php"> <p> <label>Quiz Title </label> <br /> <input type="text" name="quizTitle" id="quizTitle" /> <p> <label>Describe your Quiz<br /> <textarea name="desc" id="desc" cols="45" rows="5"></textarea> <br /> </label> <p> <label>Question <br /> <input type="text" name="question" id="question" /> </label> <p> <label>Answers <br /> 1. <input type="text" name="answer1" id="answer1" /> </label> <label> <input type="radio" name="radio" id="radio1" value="1" /> </label> <p>2. <label> <input type="text" name="answer2" id="answer2" /> </label> <label> <input type="radio" name="radio" id="radio3" value="1" /> </label> <p>3. <label> <input type="text" name="answer3" id="answer3" /> </label> <label> <input type="radio" name="radio" id="radio3" value="1" /> </label> <p>4. <label> <input type="text" name="answer4" id="answer4" /> </label> <label> <input name="radio" type="radio" id="radio4" value="1" /> </label> <br /> <p> <input type="Submit" name="nextPage" id="nextPage" value="Submit" /> </form> </body> </html> and my php file is like this: <?php // CONNECT TO MYSQL SERVER $con = mysql_connect("localhost","dinita","**********"); if (!$con) { die('Could not connect: ' . mysql_error()); } // SELECT DATABASE mysql_select_db("quizCreator", $con); // CLEAN THE POST VALUES FROM THE FORM $title = clean($_POST['quizTitle']); $desc = clean($_POST['desc']); $question = $_POST['question']; $ans1 = clean($_POST['answer1']); $ans2 = clean($_POST['answer2']); $ans3 = clean($_POST['answer3']); $ans4 = clean($_POST['answer4']); $correct = clean($_POST['radio']); // CREATE SQL QUERY $sql="INSERT INTO quiz (quiz_name, quiz_desc) VALUES ('$title','$desc')"; $sql="INSERT INTO question (question_body) VALUES ('$question')"; $sql="INSERT INTO answer1 (answer) VALUES ('$ans1')"; $sql="INSERT INTO answer2 (answer) VALUES ('$ans2')"; $sql="INSERT INTO answer3 (answer) VALUES ('$ans3')"; $sql="INSERT INTO answer4 (answer) VALUES ('$ans4')"; $sql="INSERT INTO correct (correct_ans) VALUES ('$correct')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Quiz submitted sucessfully"; mysql_close($con); ?> I'm sorry if I havent explained my self well, Thanks for any help you can offer Dinita