SELECT statement problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sm_558
Forum Newbie
Posts: 5
Joined: Sun Apr 06, 2008 5:52 pm

SELECT statement problem

Post by sm_558 »

Hi, im doing an editquiz functionality as part of my quiz management system. At the moment the forms prints out everything that is in the fields of the edit quiz form. I need to do a SELECT statement whivh compares the values in the fields to that in the database. If the user has changed anything i want to print out the changed text in red and if the fields are the same to data in database then i just want it to be printed in black. This way the users modified fields are highlighted in red.
Ive done the following code but it does not seem to work:

require_once('mysql_connect.php');

$quiz = $_POST['quizID'];
echo "<h2>The Quiz Number is: $quiz</h2>";
foreach ($_POST as $variableName => $value) {
if (beginsWith($variableName,"newQuestion_")) {
// the $variableName starts off with newQuestion_
// Hence we're interested in it, because this variable contains one of our questions.

//e.g. $variableName = "newQuestion_1" or $variableName = "newQuestion_5" etc. etc.
//so let's just grab the question number (cos we currently don't know what it is).

//$QNumber = the stuff inside $variableName, but with "newQuestion_" removed from it.
$QNumber = str_replace("newQuestion_", "", $variableName);
$QuestionText = $value;
//echo "we have got question number $QNumber, which contains: $QuestionText<br/>\n";
$rightAnswerVariableName = "newRightAns_$QNumber";
$rightAnswer = $_POST["$rightAnswerVariableName"];
//echo "The above question has a right answer of: $rightAnswer <br />\n";

$newWAns1VariableName = "newWrongAns1_$QNumber";
$WAns1 = $_POST["$newWAns1VariableName"];
//echo "Wrong Answer 1 for above question is: $WAns1 <br />\n";

$newWAns2VariableName = "newWrongAns2_$QNumber";
$WAns2 = $_POST["$newWAns2VariableName"];
//echo "Wrong Answer 2 for above question is: $WAns2 <br />\n";

$newWAns3VariableName = "newWrongAns3_$QNumber";
$WAns3 = $_POST["$newWAns3VariableName"];
//echo "Wrong Answer 3 for above question is: $WAns3 <br /><br />\n";


// compare the two and if diff print out in red
$selectQuery = "SELECT * FROM questions WHERE question = $QuestionText";
$updateResult=mysql_query ($selectQuery);

if ($updateResult = $QuestionText)
{
echo "we have got question number $QNumber, which contains:</font> $QuestionText<br/>\n";
}

else
{
echo "<font color = 'red'> we have got question number $QNumber, which contains:</font> $QuestionText<br/>\n";
}
//echo stuff here
//update the database with this question's data.
$updateQuery="UPDATE questions SET question = '$QuestionText', right_ans='$rightAnswer', wrong_ans1='$WAns1', wrong_ans2='$WAns2', wrong_ans3='$WAns3' WHERE quizID=$quiz AND question_no=$QNumber";
$updateResult=mysql_query($updateQuery) or die(mysql_error());
//echo "update";


}


}
Post Reply