Page 1 of 1

htmlspecialchars issue

Posted: Wed Jun 04, 2008 4:44 pm
by zed420
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 insert html tags into databse. I've managed to insert and display them by using 'htmlspecialchar' but now the query wont count those questions that have tags as their options, for example

Code: Select all

 
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;

Code: Select all

 
$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 = $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 help would be appreciated
Zed

Re: htmlspecialchars issue

Posted: Thu Jun 05, 2008 8:57 am
by vargadanis
Hi!
Welcome to the forums.

So, the htmlspecialchars decodes the chars to HTML codes such as: á -> &aacute; If you search the database for á but &aacute; is stored, it will not return true.
So, if you are searching for the <i> tag, use the htmlspecialchars for the posted result as well. This way the posted option will be with the one stored in the DB.

Re: htmlspecialchars issue

Posted: Thu Jun 05, 2008 5:41 pm
by zed420
Where in result page would you suggest I should put this .
Thanks
Zed

Re: htmlspecialchars issue

Posted: Fri Jun 06, 2008 1:10 am
by RobertGonzalez
If you are inserting htmlspecialchars strings then you need to search htmlspecialchars. What that means is when a user searches, take their search term and htmlspecialchars it.

Re: htmlspecialchars issue

Posted: Fri Jun 06, 2008 8:14 am
by vargadanis
Where in result page would you suggest I should put this .
Thanks
Zed
Everah answered it. :)