teenwolf Posted October 29, 2012 Share Posted October 29, 2012 Hey guys, basically I am trying to make an online php/MySQL quiz, where the questions and answers are stored on a database. I am having trouble getting my code to work and display the questions in radio button format, and dont know what is wrong or missing from what I have so far. Here is the code I have done so far Thanks <?php session_start(); require("function.php"); require("dblink.php"); $conn = "site"; $id = $_GET['id']; $sql = "SELECT * FROM 'questions' where id =".$id; $_SESSION['answer'] = $row['correct']; //Start with question ID generateheader("Question: ".$id=2); //Is there a vairable in the url if (isset($variable)) { //get question id from user and store $id = '2'; } else { //no question id submitted $id = '2'; //unset session data unset($_SESSION['questions']); unset($_SESSION['total']); } //If the questions array has been built session 'questions' if (isset($SESSION)) { //has the user selected a value (request) if (isset($REQUEST)) { //check for a correct answer, (look at stored session data later) if ($SESSION) { //correct answer $_SESSION['total'] = ''; } } else { //No value inputted from the user } } //Questions array is not been built else { //get a list of question id's $sql = "SELECT id FROM questions ORDER BY id ASC"; $results = mysql_query($sql,$dblink); $stringtemp = ""; $maxtemp = 1; while (mysql_fetch_array($results)) //More results { $stringtemp = $stringtemp." ".$row['id']; $maxtemp++; } //Create question id array pulled from database $_SESSION['questions'] = explode(" ",$stringtemp); //Max value of the array $_SESSION['max']=$maxtemp; } //Return a database result to pick data from $sql = "select * from questions where id =".$_SESSION['questions'][$id]; $result = mysql_query($sql,$dblink) ;; $row = mysql_fetch_array($results); //Store correct answer for next pageload $_SESSION['answer'] = $row['correct']; $nextid=$id+1; //Check to see if the current page is the final question if ($nextid == $_SESSION['max']) { //Final question } else { //Not the final question } generateheader("Question: ".$id); echo "<form action=\"".$next."\" method=\"post\" name=\"form\" > <p>".$row['qn']."</p> <input type=\"radio\" name=\"value\" value=\"1\">".$row['answer1']."<br> <input type=\"radio\" name=\"value\" value=\"2\">".$row['answer2']."<br> <input type=\"radio\" name=\"value\" value=\"3\">".$row['answer3']."<br> <input type=\"radio\" name=\"value\" value=\"4\">".$row['answer4']."<br> <input class=\"submit\" type=\"submit\" value=\"Submit\"/> </form> </body> </html> "; ?> Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 29, 2012 Share Posted October 29, 2012 Some quick comments, due to the number of issues: You are vulnerable for SQL injections via your $id variable. Use intval () around $_GET['id'] when setting it, to force it to be an integer (and not an unmodified string). $variable isn't being set anywhere (that I can see), thus it will always return false when checked with isset (). I presume $SESSION should be $_SESSION, or rather $_SESSION['questions']. The session superglobal will always be set after you've started a session, even if it's empty. Same with $REQUEST, should be $_REQUEST. You're not handling errors in your SQL queries, use something like this:$sql = 'SELECT $fields FROM $table $CONDITIONS'; if (!$res = mysql_query ($SQL)) { // Query errored out, show error. trigger_error (mysql_error ()."\nQuery: $SQL", E_USER_ERROR); } You should also be using the MySQLi library, or PDO, as the old MySQL library is outdated and is going to be deprecated soon. You're not saving the result from mysql_fetch_array () into the $row variable. No point in creating $stringtemp with the IDs separated by a space, only to explode it into an array later. Use $_SESSION['questions'][] = $row['id'] instead, inside the while loop. Your check for the final question isn't checking if the current page is the final question, but the next page. The $next variable isn't set, yet. Use htmlspecialchars () around content you're echoing out to HTML, which shouldn't contain HTML code. Especially user-submitted code! Except from this, and the unfinished code, it seems like you're basically on the right way. Some things could probably have been made a bit simpler, but that's something you can focus upon once you get the code fully working. It takes experience to figure that stuff out, after all. However, I think your problem stems from the lack of saving the row return from the mysql_fetch_array () call, where you build up the array of the question IDs. Also, please use the [code][/code] tags around your code, as it helps make both your post and your code a lot easier to read. Quote Link to comment Share on other sites More sharing options...
teenwolf Posted October 29, 2012 Author Share Posted October 29, 2012 Hey thanks so much for the reply!!! Whenever I try to do the things mentioned I just get syntax errors throughout my code, I must be putting in them wrong. are you able to paste the code with your changes to this thread? My heads about to implode with all this coding haha Quote Link to comment Share on other sites More sharing options...
White_Lily Posted October 29, 2012 Share Posted October 29, 2012 create a file in the main directory called "php.ini" in that file write: display_errors = on error_reporting = -1 save the file as "php.ini", upload to the server, and refresh your webpage. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 29, 2012 Share Posted October 29, 2012 (edited) First of all, you're most welcome. are you able to paste the code with your changes to this thread? Unfortunately, I'm not. That would be me doing your job, and mine, for free. However, if you post your code her, along with the error messages, then I'm sure we can help you figure it out. I do recommend that you do a search for the error messages first though, as I can almost guarantee you that they've all been answered countless times before. Searching will help save both you and us from wasting time. White_Lily: Better to edit the original php.ini. I don't think that method is supported on any hosts/web servers, or if it is that number is very small. It does not work with my local Apache install, at least. Plus the fact that he's seeing syntax errors tells me that he already has error reporting turned on. Edited October 29, 2012 by Christian F. Quote Link to comment Share on other sites More sharing options...
teenwolf Posted October 29, 2012 Author Share Posted October 29, 2012 (edited) So Ive made the changed listed I think all of them hopefully...with no syntax errors this time around When i preview the page I receive the error listed below relating the to the new SQL queries. Should I be able to see the data in my radio buttons kept in the database at the moment? because Im still not getting anything? would this be because Im not connecting or accessing the database properly yet? again thanks heaps for the help guys "Fatal error: Query was empty Query: in /Applications/XAMPP/xamppfiles/htdocs/SITE/index.php on line 63" session_start(); require("function.php"); require("dblink.php"); $conn = "site"; $id = ($_GET['id']); $sql = "SELECT * FROM 'questions' where id =".$id; $_SESSION['answer'] = $row['correct']; //Start with question ID generateheader("Question: ".$id=2); //Is there a vairable in the url if (isset($variable)) { //get question id from user and store $id = '2'; } else { //no question id submitted $id = '2'; //unset session data unset($_SESSION['questions']); unset($_SESSION['total']); } //If the questions array has been built session 'questions' if (isset($_SESSION['questions'])) { //has the user selected a value (request) if (isset($_REQUEST)) { //check for a correct answer, (look at stored session data later) if ($_SESSION) { //correct answer $_SESSION['total'] = ''; } } else { //No value inputted from the user } } //Questions array is not been built else { //get a list of question id's $sql = 'SELECT $fields FROM $table $CONDITIONS'; if (!$res = mysql_query ($SQL)) { // Query errored out, show error. trigger_error (mysql_error ()."\nQuery: $SQL", E_USER_ERROR); } $results = mysql_query($sql,$dblink); $stringtemp =""; $maxtemp = 1; while (mysql_fetch_array($results)) //More results { $stringtemp=$_SESSION['questions']=$row['id']; $maxtemp++; } //Create question id array pulled from database $_SESSION['questions']=explode(" ",$stringtemp); //Max value of the array $_SESSION['max']=$maxtemp; } //Return a database result to pick data from $sql = 'SELECT $fields FROM $table $CONDITIONS'; if (!$res = mysql_query ($SQL)) { // Query errored out, show error. trigger_error (mysql_error ()."\nQuery: $SQL", E_USER_ERROR); } $info = mysql_fetch_array($data); $result = mysql_query($sql,$dblink); $row = mysql_fetch_array($results); //Store correct answer for next pageload $_SESSION['answer'] = $row['correct']; $nextid=$id+1; //Check to see if the current page is the final question if ($nextid == $_SESSION['max']) { //Final question } else { //Not the final question } generateheader("Question: ".$id); echo " ".$row['qn']." ".$row['answer1']." ".$row['answer2']." ".$row['answer3']." ".$row['answer4']." "; ?> Edited October 29, 2012 by teenwolf Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 29, 2012 Share Posted October 29, 2012 $sql = 'SELECT $fields FROM $table $CONDITIONS'; if (!$res = mysql_query ($SQL)) { // Query errored out, show error. trigger_error (mysql_error ()."\nQuery: $SQL", E_USER_ERROR); Did you look at the lines? PHP is case sensitive. Quote Link to comment Share on other sites More sharing options...
teenwolf Posted October 29, 2012 Author Share Posted October 29, 2012 yeah I noticed that after I posted that comment...haha thanks though. Still cant get the info from the database to go into my radio buttons though, really quite frustrating Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 29, 2012 Share Posted October 29, 2012 Your code doesn't have anything that even attempts to create a radio set. Quote Link to comment Share on other sites More sharing options...
teenwolf Posted October 29, 2012 Author Share Posted October 29, 2012 is this not the radio set of buttons here at the end of my code? for some reasons it appears different in the code up a few posts. The radio buttons appear when I preview the page as well as the submit button, but none of the information from the database transfers over to them. generateheader("Question: ".$id); echo "<form action=\"".$next."\" method=\"post\" name=\"form\" > <p>".$row['qn']."</p> <input type=\"radio\" name=\"value\" value=\"1\">".$row['answer1']."<br> <input type=\"radio\" name=\"value\" value=\"2\">".$row['answer2']."<br> <input type=\"radio\" name=\"value\" value=\"3\">".$row['answer3']."<br> <input type=\"radio\" name=\"value\" value=\"4\">".$row['answer4']."<br> <input class=\"submit\" type=\"submit\" value=\"Submit\"/> </form> </body> </html> "; ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 29, 2012 Share Posted October 29, 2012 Use code tags dammit. In your other code that doesn't have this stuff you just posted, you have another problem. $result = mysql_query($sql,$dblink); $row = mysql_fetch_array($results); This is basic debugging, and you should be getting undefined variable errors. Quote Link to comment Share on other sites More sharing options...
teenwolf Posted October 29, 2012 Author Share Posted October 29, 2012 thanks ill take a look at that stuff now , sorry about the code tags, just when i used the code tags that last bit of code with the radio buttons didnt come up. Quote Link to comment Share on other sites More sharing options...
teenwolf Posted October 29, 2012 Author Share Posted October 29, 2012 THANKS TO EVERYONE WHO HAS HELPED!! Ive been able now to obtain the information in the database into my radio buttons! Now to continue working on the rest of it! Quote Link to comment Share on other sites More sharing options...
teenwolf Posted October 29, 2012 Author Share Posted October 29, 2012 hey guys... So I have been able to retrieve the information from the database into the radio buttons. How am I able to rotate through the 6 questions I have on my database without having to make a separate page for each question? So essentially when the user hits submit the page is refreshed with a new questions while the result is still stored in order to give the user a total score at the end of the quiz?? Thanks Quote Link to comment Share on other sites More sharing options...
MDCode Posted October 29, 2012 Share Posted October 29, 2012 (edited) Insert into the database the first how many or so questions. Then load something like page.php?set=2 or something for the next questions. For the final score, just select all of the answers out of the database and display the amount correct Edited October 29, 2012 by ExtremeGaming Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 29, 2012 Share Posted October 29, 2012 You just change the number in the id parameter, in your URL. Quote Link to comment Share on other sites More sharing options...
teenwolf Posted October 30, 2012 Author Share Posted October 30, 2012 hmm.. i cant seem to get this to work is this the URL you are talking about? whenever I change the id number to another numbers question it still just brings up the first question in my database //Start with question ID generateheader("Question: ".$id=2); //Is there a vairable in the url if (isset($variable)) { Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 I suspect the fault lies here : if (isset($variable)){ //get question id from user and store $id = '2'; } else { //no question id submitted $id = '2'; //unset session data unset($_SESSION['questions']); unset($_SESSION['total']); } I suspect the first $id= is supposed to be $id=$variable; It's just a hunch though... Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 30, 2012 Share Posted October 30, 2012 Use intval () to ensure that the ID is an integer, and not some SQL code. Also, I suspect that the variable should be retrieved from the URL, which means the $_GET superglobal. Thus the code becomes: if (isset ($_GET['variable'])) { $id = intval ($_GET['variable']); } else { $id = 2; unset ($_SESSION['questions']); unset ($_SESSION['total']); } Quote Link to comment Share on other sites More sharing options...
teenwolf Posted October 31, 2012 Author Share Posted October 31, 2012 Thanks for the help guys, that hasnt seemed to fix the problem though. Ill just have to keep playing around with different things and just hope I stumble across the right coding haha. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.