yakoup46 Posted April 18, 2010 Share Posted April 18, 2010 I am not really sure what is going on, but for some reason I am getting a "Query is Empty" error. This code was suggested to me in another post so I am not completly farmilar with it. I have looked it over but cannot find why I would be getting the error. function checkAnswer($button, $inputbox, $session, $table, $answer) { if(isset($_POST[$button]) || $_POST[$inputbox]) { $sql="INSERT INTO $table VALUES ('$_POST[$inputbox]')"; if($_POST[$inputbox] != $answer) { echo "Sorry Wrong!"; session_start(); if(isset($_SESSION[$session])) { $_SESSION[$session] = $_SESSION[$session] + 1; // Could use $_SESSION[$session]++; } else { $_SESSION[$session] = 1; } } else { echo "Correct!"; } echo "<br />Number of incorrect answers = ". $_SESSION[$session]; } } checkAnswer("buttonsumofodds", "inputbox1", "views1", "Sumofodds", "280"); checkAnswer("buttonsumofchain", "inputbox3", "views3", "Sumofchain", "820"); checkAnswer("buttonsumofsquares", "inputbox2", "views2", "Sumofsquares", "2870"); checkAnswer("buttonsumofprimes", "inputbox5", "views5", "Sumofprimes", "1060"); checkAnswer("buttonsumofdifference", "inputbox4", "views4", "Diffofsums", "-144"); echo "<br /><a href='http://luta.comuv.com/Easy/easy.html'>Go Back</a>"; if(!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
-Karl- Posted April 18, 2010 Share Posted April 18, 2010 Where's $table defined? Quote Link to comment Share on other sites More sharing options...
yakoup46 Posted April 18, 2010 Author Share Posted April 18, 2010 What do you mean? Quote Link to comment Share on other sites More sharing options...
-Karl- Posted April 18, 2010 Share Posted April 18, 2010 $sql="INSERT INTO $table VALUES ('$_POST[$inputbox]')"; How does it know what table to insert it in to? Also, you need (inputbox) VALUES ('$_POST[$inputbox]') otherwise the script is clueless which row it goes in. Quote Link to comment Share on other sites More sharing options...
yakoup46 Posted April 18, 2010 Author Share Posted April 18, 2010 I fixed the row thing... but doesnt the bold stuff tell it what table to put it into? checkAnswer("buttonsumofodds", "inputbox1", "views1", "Sumofodds", "280"); checkAnswer("buttonsumofchain", "inputbox3", "views3", "Sumofchain", "820"); checkAnswer("buttonsumofsquares", "inputbox2", "views2", "Sumofsquares", "2870"); checkAnswer("buttonsumofprimes", "inputbox5", "views5", "Sumofprimes", "1060"); checkAnswer("buttonsumofdifference", "inputbox4", "views4", "Diffofsums", "-144"); Quote Link to comment Share on other sites More sharing options...
-Karl- Posted April 18, 2010 Share Posted April 18, 2010 Ignore the first part, I'm an idiot and didn't realise it was a function. In your query I still believe you need (inputbox) VALUES ('$_POST[$inputbox]'), providing your table has an inputbox row. Quote Link to comment Share on other sites More sharing options...
yakoup46 Posted April 18, 2010 Author Share Posted April 18, 2010 I did as you had suggested an still got the query was empty error! ARRRGHHH!!! LOL function checkAnswer($button, $inputbox, $session, $table, $column, $answer) { if(isset($_POST[$button]) || $_POST[$inputbox]) { $sql="INSERT INTO $table ($column) VALUES ('$_POST[$inputbox]')"; if($_POST[$inputbox] != $answer) { echo "Sorry Wrong!"; session_start(); if(isset($_SESSION[$session])) { $_SESSION[$session] = $_SESSION[$session] + 1; // Could use $_SESSION[$session]++; } else { $_SESSION[$session] = 1; } } else { echo "Correct!"; } echo "<br />Number of incorrect answers = ". $_SESSION[$session]; } } checkAnswer("buttonsumofodds", "inputbox1", "views1", "Sumofodds", "ValueOdds", "280"); checkAnswer("buttonsumofchain", "inputbox3", "views3", "Sumofchain", "ValueChain", "820"); checkAnswer("buttonsumofsquares", "inputbox2", "views2", "Sumofsquares", "ValueSquares", "2870"); checkAnswer("buttonsumofprimes", "inputbox5", "views5", "Sumofprimes", "ValuePrimes", "1060"); checkAnswer("buttonsumofdifference", "inputbox4", "views4", "Diffofsums", "ValueDiff", "-144"); echo "<br /><a href='http://luta.comuv.com/Easy/easy.html'>Go Back</a>"; if(!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
DavidAM Posted April 19, 2010 Share Posted April 19, 2010 You are executing the query OUTSIDE of the function. $sql is NOT defined in that scope so the query is empty. You probably want to execute the query inside the function after assigning $sql Quote Link to comment Share on other sites More sharing options...
Andy-H Posted April 19, 2010 Share Posted April 19, 2010 You should also move session_start(); out of the function (and above any output, best as the first line of PHP in the script), it is currently attempting to initialize the session after output is given, and every time the function is called. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted April 19, 2010 Share Posted April 19, 2010 <?php session_start(); //first line of php function checkAnswer($button, $inputbox, $session, $table, $column, $answer) { static $questionNum = 1; if(isset($_POST[$button]) || $_POST[$inputbox]) { $sql="INSERT INTO $table ($column) VALUES ('" . mysql_real_escape_string($_POST[$inputbox]) . "')"; if(!mysql_query($sql)) { die('Error: ' . mysql_error()); } if($_POST[$inputbox] != $answer) { echo "<span style=\"color: red; display: block;\">Question " . $questionNum . ". Sorry wrong answer!</span>"; if(isset($_SESSION[$session])) { $_SESSION[$session] = $_SESSION[$session] + 1; // Could use $_SESSION[$session]++; } else { $_SESSION[$session] = 1; } } else { echo "<span style=\"color: green; display: block;\">Question " . $questionNum . ". Correct!</span>"; } echo "<br />Number of incorrect answers = " . (int)$_SESSION[$session]; } $questionNum++; } checkAnswer("buttonsumofodds", "inputbox1", "views1", "Sumofodds", "ValueOdds", "280"); checkAnswer("buttonsumofchain", "inputbox3", "views3", "Sumofchain", "ValueChain", "820"); checkAnswer("buttonsumofsquares", "inputbox2", "views2", "Sumofsquares", "ValueSquares", "2870"); checkAnswer("buttonsumofprimes", "inputbox5", "views5", "Sumofprimes", "ValuePrimes", "1060"); checkAnswer("buttonsumofdifference", "inputbox4", "views4", "Diffofsums", "ValueDiff", "-144"); echo "<br /><a href='http://luta.comuv.com/Easy/easy.html'>Go Back</a>"; mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
yakoup46 Posted April 19, 2010 Author Share Posted April 19, 2010 Thank you very much the code works like a charm! 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.