Jump to content

Inserting Issue


zed420

Recommended Posts

Hi All

 

I wonder if someone can help me out here . I'm trying to make a quiz in my website, so far so good till I'm inserting these records into my database some of them won't display, the question shows but Options will not display.

 

INSERT INTO php_tb 
VALUES ('3','Choose the correct HTML tag for the largest heading','<h6>','<head>','<h1>','<h1>');

INSERT INTO php_tb 
VALUES ('4',' What is the correct HTML tag for inserting a line break?','<lb>','<break>','<br>','<br>');

INSERT INTO php_tb 
VALUES ('5',' What is the correct HTML for adding a background color?','<background>yellow</background> 
','<body bgcolor="yellow">','<body color="yellow">','<body bgcolor="yellow">');

INSERT INTO php_tb 
VALUES ('6','Choose the correct HTML tag to make a text bold','<bld>','<bold>','<b>','<b>');

INSERT INTO php_tb 
VALUES ('7','Choose the correct HTML tag to make a text italic','<italic>','<i>','<ii>','<i>');

 

Thanks in advance

Zed

Link to comment
Share on other sites

Thanks for the reply fenway, I've managed to display the data by using "htmlspecialchar", now the problem is its not counting those questions that have html tags "< >"as an option.  I hope I'm making sense.

 

$Query="INSERT into $table (id,question,opt1,opt2,opt3,answer)

values ('$id','$question', '$opt1' , '$opt2', '$opt3' , '$answer')";

 

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

I find your code really hard to understand, anyway I'm pretty sure your problem is the way you treat data with htmlspecialchars(): if you store answers in db in plain html (as to say <b > - the space was left just for not bolding the text in my post! ) and then you use htmlspecialchars to give a value to your option field (as to say <b>), of course the user's selection won't match what you have in the db, because they are different strings...

 

You'd better use some sort of an answer key (let's say a, b, or c) instead of its value.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.