zed420 Posted June 3, 2008 Share Posted June 3, 2008 Hi All I wonder if you can help me, I'm fairly new to PHP, I'm trying create a quiz for my website I did well so far till it came to use of html tags. I've managed to display them by using 'htmlspecialchar' but now the query wont count those questions that have tags as their options, for example Question Choose the correct HTML tag to make a text italic Opt1 <Italic> Opt2 <i> Opt3 <ii> When you click on Opt2 this should be counted as one of your right answer but it DOES NOT. but on same question if you don't use tags in options (Opt) it will work. This is how I am finding the results; $query = "SELECT * FROM php_tb ORDER BY id"; $result = mysql_query($query) or die ("Couldn't execute query 2."); if (!$_POST['submit']) { echo "<form method=post action=$PHP_SELF>"; while ($row = mysql_fetch_array($result)){ $id= $_POST["id"]; $question= $_POST["question"]; $opt1= $_POST["opt1"]; $opt2= $_POST["opt2"]; $opt3= $_POST["opt3"]; $answer= $_POST["answer"]; } } elseif ($_POST['submit']) { $score = 0; $total = mysql_num_rows($result); while ($row = mysql_fetch_array($result)){ $answer= htmlspecialchars($answer); $answer = $row[answer]; $z = "q$row[id]"; $z = trim($z); if ($_POST[$z] == $answer) { $score++; } } echo "<p align=center><b>You scored $score out of $total</b></p>"; echo "<p>"; if ($score == $total) { echo "Congratulations! You got all the question right!"; echo "<p>Well done $name, with a score of $score, </p>"; Thanks Zed Link to comment https://forums.phpfreaks.com/topic/108590-htmlspecialchar-issue/ Share on other sites More sharing options...
Jabop Posted June 3, 2008 Share Posted June 3, 2008 You're inserting it without htmlspecialchars and then selecting it with, which are separate values Link to comment https://forums.phpfreaks.com/topic/108590-htmlspecialchar-issue/#findComment-556881 Share on other sites More sharing options...
zed420 Posted June 4, 2008 Author Share Posted June 4, 2008 Thanks for replying Jabop I've tried like this it still wont work; $query = "SELECT * FROM php_tb ORDER BY id"; $result = mysql_query($query) or die ("Couldn't execute query 2."); if (!$_POST['submit']) { echo "<form method=post action=$PHP_SELF>"; while ($row = mysql_fetch_array($result)){ $id= $_POST["id"]; $question= $_POST["question"]; $question= htmlspecialchar($question); $opt1= $_POST["opt1"]; $opt1= htmlspecialchar($opt1); $opt2= $_POST["opt2"]; $opt2= htmlspecialchar($opt2); $opt3= $_POST["opt3"]; $opt3= htmlspecialchar($opt3); $answer= $_POST["answer"]; $answer= htmlspecialchar($answer); } } elseif ($_POST['submit']) { $score = 0; $total = mysql_num_rows($result); while ($row = mysql_fetch_array($result)){ $answer= htmlspecialchars($answer); $answer = $row[answer]; $z = "q$row[id]"; $z = trim($z); if ($_POST[$z] == $answer) { $score++; } } echo "<p align=center><b>You scored $score out of $total</b></p>"; echo "<p>"; if ($score == $total) { echo "Congratulations! You got all the question right!"; echo "<p>Well done $name, with a score of $score, </p>"; Any more suggestions... Link to comment https://forums.phpfreaks.com/topic/108590-htmlspecialchar-issue/#findComment-557313 Share on other sites More sharing options...
Wolphie Posted June 4, 2008 Share Posted June 4, 2008 Try $myStr = mysql_real_escape_string(htmlspecialchars(htmlentities($_POST['myPost']))); Only use this when inserting into the database, it's not needed otherwise. Link to comment https://forums.phpfreaks.com/topic/108590-htmlspecialchar-issue/#findComment-557318 Share on other sites More sharing options...
zed420 Posted June 4, 2008 Author Share Posted June 4, 2008 It didn't work Wolphie, thanks for trying. Link to comment https://forums.phpfreaks.com/topic/108590-htmlspecialchar-issue/#findComment-557343 Share on other sites More sharing options...
zed420 Posted June 4, 2008 Author Share Posted June 4, 2008 Thanks Wolphie I've just relised with your code the query is counting the question with tags on but the only problem now is its not displaying them. e.g Question blah blah blah Opt1 Opt2 OPt3 There is nothing infront of Options(Opt). The data is deff going into databse but without < and > Help needed Zed Link to comment https://forums.phpfreaks.com/topic/108590-htmlspecialchar-issue/#findComment-557402 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.