Page 1 of 2

Script not working

Posted: Wed Feb 14, 2007 8:11 pm
by fastmike
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello. i got a good login script with administrator rights, ban user giving more access to the user in the database. i was trying to create an online test. so i added a table in my database which is as follow:
[syntax="sql"]
DROP TABLE IF EXISTS users;

CREATE TABLE users (
 username varchar(30) primary key,
 password varchar(32),
 userid varchar(32),
 userlevel tinyint(1) unsigned not null,
 email varchar(50),
 timestamp int(11) unsigned not null
);


#
#  Table structure for active users table
#
DROP TABLE IF EXISTS active_users;

CREATE TABLE active_users (
 username varchar(30) primary key,
 timestamp int(11) unsigned not null
);


#
#  Table structure for active guests table
#
DROP TABLE IF EXISTS active_guests;

CREATE TABLE active_guests (
 ip varchar(15) primary key,
 timestamp int(11) unsigned not null
);


#
#  Table structure for banned users table
#
DROP TABLE IF EXISTS banned_users;

CREATE TABLE banned_users (
 username varchar(30) primary key,
 timestamp int(11) unsigned not null
);
CREATE TABLE quiz (
id tinyint(4) NOT NULL auto_increment,
q text NOT NULL,
question text NOT NULL,
opt1 text NOT NULL,
opt2 text NOT NULL,
opt3 text NOT NULL,
answer text NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM; 
everytime i try to insert data it gives me an error column count does not match with row 1
for eg

Code: Select all

insert into quiz(id, q, question, opt1, opt2, opt3, answer)
VALUES('0001', '1', 'who is the president of United States?', 'Clinton', 'Bush', 'Regan', 'Bush');
it gives me an error any idea y ? plus i have more question
Thanks


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Feb 14, 2007 8:48 pm
by fastmike
here is my php code for the quiz everytime i try to load the php file on my server it gives me error.

Code: Select all

<?php


include("contentdb.php");

$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);

if (!$submit) {


	echo "<form method=post action=$PHP_SELF>";
	echo "<table border=0>";

	while ($row = mysql_fetch_array($display)) {

	$id = $row["id"];
	$question = $row["question"];
	$opt1 = $row["opt1"];
	$opt2 = $row["opt2"];
	$opt3 = $row["opt3"];
	$answer = $row["answer"];

	echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
	echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>";

	}

	echo "</table>";
	echo "<input type='submit' value='See how you did' name='submit'>";
	echo "</form>";

}

elseif ($submit)

{

	$score = 0;
	$total = mysql_num_rows($display);
		while ($result = mysql_fetch_array($display))


		{

			$answer = $result["answer"];
			$q = $result["q"];

		if ($$q == $answer)
		{
		$score++;
		}

	}

	echo "<p align=center><b>You scored $score out of $total</b></p>";
	echo "<p>";

	if   ($score == $total) {
	echo "Congratulations! You got every question right!";
	}
	elseif ($score/$total < 0.34) {
	echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
	}
	elseif ($score/$total > 0.67) {
	echo "Well done! You certainly know your stuff.";
	}
	else {
	echo "Not bad - but there were a few that caught you out!";
	}

echo "</p>";

echo "<p>Here are the answers:";

echo "<table border=0>";
$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
while ($row = mysql_fetch_array($display)) {

$question = $row["question"];
$answer = $row["answer"];
$q = $row["q"];

echo "<tr><td><br>$question</td></tr>";

if ($$q == $answer)
		{
		echo "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
		}
elseif ($$q == "") {
echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
}
else {
echo "<tr><td>&raquo;you answered ${$q}. The answer is $answer</td></tr>";
}

}
echo "</table></p>";



}

?>
error

Code: Select all

Notice: Undefined variable: submit in c:\program files\easyphp1-7\www\quizv1.0\quiz1.php on line 9

Notice: Undefined variable: PHP_SELF in c:\program files\easyphp1-7\www\quizv1.0\quiz1.php on line 12
here is my admin script for the quiz

Code: Select all

<HTML>
<link rel="stylesheet" href="quiz.css" type="text/css">
<body><center>
<P>&nbsp;</P>

<B>Admin area - edit the quiz</B>
<br><br>
  <table width="300" border="0" cellspacing="0" cellpadding="0">

        <?php

include("contentdb.php");

$result = mysql_query("SELECT id, question FROM $table ORDER BY id",$db);

echo "<table>";

while ($row = mysql_fetch_array($result)) 
{
	
	$id = $row["id"]; 
	$question = $row["question"]; 
	if ($alternate == "1") { 
	$color = "#ffffff"; 
	$alternate = "2"; 
	} 
	else { 
	$color = "#efefef"; 
	$alternate = "1"; 
	} 
	echo "<tr bgcolor=$color><td>$id:</td><td>$question</td><td>[ <a href='editquiz.php?id=$id'>edit</a> ]</td><td>[ <a href='deletequiz.php?id=$id' onClick=\"return confirm('Are you sure?')\">delete</a> ]</td></tr>"; 
} 
echo "</table>";
?>
        <br>
        <br>
        <a href="editquiz.php">Add a new question to the quiz</a></td>
    </tr>
  </table>
  <br>
  <br>
<a href="quizinfo.php">See the full quiz table</a>
</center>
</body>
</HTML>
error

Code: Select all

Admin area - edit the quiz 
Notice: Undefined variable: submit in c:\program files\easyphp1-7\www\quizv1.0\editquiz.php on line 9

Notice: Undefined variable: update in c:\program files\easyphp1-7\www\quizv1.0\editquiz.php on line 16

Notice: Undefined variable: id in c:\program files\easyphp1-7\www\quizv1.0\editquiz.php on line 22
any help will be really appreciated.

Posted: Wed Feb 14, 2007 10:48 pm
by fastmike
Php gurus where are you?

Posted: Wed Feb 14, 2007 11:00 pm
by superdezign
Maybe they feel that your answer is too obvious...?

Your PHP scripts are giving you the error "undefined variable." Therefore, you are attempting to use an undefined variable.

At no point do I see you assign a value to $submit.
$PHP_SELF should be $_SERVER['PHP_SELF'], and feyd will only tell you that PHP_SELF shouldn't be used.

You don't have your editquiz.php script up, but I'm sure you never assign a value to $submit, $update, or $id. The reason I hadn't responded till now is because I assumed you'd fix something so simple. Before I would need to explain it.

Also, for future reference, I'm sure bumping your post is against the rules. If you want to be noticed, make a single post and you'll be in the "Unanswered Questions" section.

Posted: Wed Feb 14, 2007 11:13 pm
by fastmike
allrite. i saw this undefined variable and yes i looked at it to so i changed my $php_self to quiz1.php. i guess i am new to this php that is y i can't understand how to assign a value to submit. yes you are rite its giving me the same error. i am still trying to fix it. i am able to fix php_self but now that submit is giving me error.

Posted: Thu Feb 15, 2007 12:09 am
by superdezign
You don't know how to give it a value? It's simple. What condition must be fulfilled for submit to be true? False?

If it's a post variable, possibly... The form submit button? You give the submit button a name/id, and access the name/id through $_POST.

Code: Select all

<input type="submit" name="formSubmit" value="Submit!" />

Code: Select all

$submit = isset($_POST['formSubmit']);

Posted: Thu Feb 15, 2007 12:59 am
by feyd
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not receive a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.

Posted: Thu Feb 15, 2007 1:08 am
by fastmike
superdesign you dont know how much i appreciate your patience and help and treating me as a noob. it worked. i made some changes to my quiz1.php file (php_self with _SERVER[PHP_SELF] works fine for me now and the isset submit you told me ) no more notice of warnings and etc. i am using easy php4.1 with mysql 4.1. People have given different ideas that its a bug in 4.0, etc. i got 2 more things to take care of it then once that is done i can play with my login script and put everything all together. when i click on (See how you did) the page does not display anything it just resets the fields. in my admin page i tried to add questions to the database it gives me a big error + a notice on a page. i read some articles regarding notice and they have said that you can ignore it if you want to because it is not an error.

Code: Select all

Notice: Undefined variable: alternate in c:\program files\easyphp1-7\www\quizv1.0\editquizlist.php on line 23
there is a button in my editquizlist.php file by the name of (Enter information). i entered a question with the options and the rite answer but when i click on enter information button there is a big error.

Code: Select all

You don't have permission to access /quizv1.0/<br /><b>Notice</b>: Undefined variable: PHP_SELF in <b>c:/program files/easyphp1-7/www/quizv1.0/editquiz.php</b> on line <b>56</b><br /> on this server.
Once again i know its frustrating but if you would like to help that will be great if not then my search will still go on. appreciated.

Posted: Thu Feb 15, 2007 2:18 am
by mikeq
On your SQL insert issue

Code: Select all

INSERT INTO quiz(id, q, question, opt1, opt2, opt3, answer) 
VALUES('0001', '1', 'who is the president of United States?', 'Clinton', 'Bush', 'Regan', 'Bush'); 
You dont enter a value for the ID as it is an auto_increment field, just miss it out in your insert statement

Code: Select all

INSERT INTO quiz(q, question, opt1, opt2, opt3, answer) 
VALUES('1', 'who is the president of United States?', 'Clinton', 'Bush', 'Regan', 'Bush'); 

Posted: Thu Feb 15, 2007 2:36 am
by shwanky
fastmike wrote:superdesign you dont know how much i appreciate your patience and help and treating me as a noob. it worked. i made some changes to my quiz1.php file (php_self with _SERVER[PHP_SELF] works fine for me now and the isset submit you told me ) no more notice of warnings and etc. i am using easy php4.1 with mysql 4.1. People have given different ideas that its a bug in 4.0, etc. i got 2 more things to take care of it then once that is done i can play with my login script and put everything all together. when i click on (See how you did) the page does not display anything it just resets the fields. in my admin page i tried to add questions to the database it gives me a big error + a notice on a page. i read some articles regarding notice and they have said that you can ignore it if you want to because it is not an error.

Code: Select all

Notice: Undefined variable: alternate in c:\program files\easyphp1-7\www\quizv1.0\editquizlist.php on line 23
there is a button in my editquizlist.php file by the name of (Enter information). i entered a question with the options and the rite answer but when i click on enter information button there is a big error.

Code: Select all

You don't have permission to access /quizv1.0/<br /><b>Notice</b>: Undefined variable: PHP_SELF in <b>c:/program files/easyphp1-7/www/quizv1.0/editquiz.php</b> on line <b>56</b><br /> on this server.
Once again i know its frustrating but if you would like to help that will be great if not then my search will still go on. appreciated.
try using $_SERVER['PHP_SELF'] instead of $PHP_SELF. Also, if you want to avoid those Notice errors for undefined variables you can either turn them using the error_reporting(E_ALL ~ E_NOTICE) or simply define the variable before using it. It's just a basic concept that you can't use some that doesn't exist. So before calling for $submit or any other variable define it, $submit = ''; Another thing you might want to do is turn register_globals off in the php.ini and use php 4's super globals $_POST, $_GET, $_SESSION, $_SERVER. It tends to be a we bit of a security issue especially for beginners, it can complicate things.

Posted: Thu Feb 15, 2007 7:31 am
by feyd
Never ever ever turn off E_NOTICE. That does not get rid of the errors, which still happen and fire. They will slow your applications and seriously irritate other developers who have E_NOTICE on and use your application. isset(), empty() and array_key_exists() exist for a reason.

Posted: Thu Feb 15, 2007 10:51 pm
by fastmike
allrite. i did as you specified. well i checked the register_globals which were off already and the error_reporting was already set for what shawnky has wroted. Wow i got the first part working now no notices nothing in my quiz1.php but my question is there is a button by the name of (See How You DId). when a person clicks on it he/she should see the results if he/she did the quiz rite or wrong. but for some reason when i click on that button it just resets the field.

Code: Select all

<?php


include("contentdb.php");

$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
$submit = isset($_POST['update']);
if (!$submit) {


	echo "<form method='post' action='$_SERVER[PHP_SELF]'>";
	echo "<table border=0>";

	while ($row = mysql_fetch_array($display)) {

	$id = $row["id"];
	$question = $row["question"];
	$opt1 = $row["opt1"];
	$opt2 = $row["opt2"];
	$opt3 = $row["opt3"];
	$answer = $row["answer"];

	echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
	echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>";

	}

	echo "</table>";
	echo "<input type='submit' value='See how you did' name='submit'>";
	echo "</form>";

}

elseif ($submit)

{

	$score = 0;
	$total = mysql_num_rows($display);
		while ($result = mysql_fetch_array($display))


		{

			$answer = $result["answer"];
			$q = $result["q"];

		if ($$q == $answer)
		{
		$score++;
		}

	}

	echo "<p align=center><b>You scored $score out of $total</b></p>";
	echo "<p>";

	if   ($score == $total) {
	echo "Congratulations! You got every question right!";
	}
	elseif ($score/$total < 0.34) {
	echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
	}
	elseif ($score/$total > 0.67) {
	echo "Well done! You certainly know your stuff.";
	}
	else {
	echo "Not bad - but there were a few that caught you out!";
	}

echo "</p>";

echo "<p>Here are the answers:";

echo "<table border=0>";
$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
while ($row = mysql_fetch_array($display)) {

$question = $row["question"];
$answer = $row["answer"];
$q = $row["q"];

echo "<tr><td><br>$question</td></tr>";

if ($$q == $answer)
		{
		echo "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
		}
elseif ($$q == "") {
echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
}
else {
echo "<tr><td>&raquo;you answered ${$q}. The answer is $answer</td></tr>";
}

}
echo "</table></p>";



}

?>

Posted: Thu Feb 15, 2007 11:22 pm
by feyd
  • You don't have a form field named "update."
  • Do not use $_SERVER['PHP_SELF'] or $PHP_SELF, you can use "#" instead most often.
  • Do not look for the submit button, they aren't always submitted by browsers. Instead look for a field that is always submitted, or possibly $_SERVER['REQUEST_METHOD'] being "POST."
  • It is often good to always quote all html tag attribute values. This avoids errors, but also gets you closer to validating.

Posted: Thu Feb 15, 2007 11:53 pm
by fastmike
oh wow. i dont have a form field update. That is messed up. i got this script online and was trying to use it with my login script. works fine for the guy who made it
http://www.widgetmonkey.com/quiz/quiz1.php
but when i download it it has so many errors which i fixed it and with devnetwork forum help. if i was that good in php i could have done it but i am a noob i have a book but not very helpful. now when you say i need to have an update field do you mean i need to define a variable ?
$update = "";
i dont see any $_SERVER['REQUEST_METHOD'] or "post" all i see is form method = 'post'. this script was really very simple and easy to use as i already have a database with user id, login and password and admin rights if this script worked then all i had to do is just give a link for a user who logs in to take the test and then later on some how store the test result in my database.

Posted: Fri Feb 16, 2007 12:00 am
by fastmike
And the same thing is with my editquiz.php. there is a button by the name of(Enter Information) when the admin opens the form editquiz.php he/she will be able to add question with all the 3 options and the rite answer and when he/she clicks on enter information button the data should be added to my database, but it gives me a huge error.

Code: Select all

You don't have permission to access /quizv1.0/<br /><b>Notice</b>: Undefined variable: PHP_SELF in <b>c:/program files/easyphp1-7/www/quizv1.0/editquiz.php</b> on line <b>59</b><br /> on this server.