Jump to content

Recommended Posts

Posted

I got forms on my site to ask questions to me that works fine i can find them in my database.

I recall them with the fetch array module and while. i created this script:

 

$ophalen = mysql_query("SELECT * FROM fastquestion") or die(mysql_error());
while ($gegevens = mysql_fetch_array($ophalen))
{
echo "type of the question:	".$gegevens['typequestion'];
echo '<br />';
echo "the Question:			".$gegevens['Question'];
echo '<br />';
echo "in game name or E-mail:  ".$gegevens['ingamenameEmail'];
echo '<br />';
   echo "already answered:		".$gegevens['Answered'];
   echo '<br />';
   echo '<Form Name ="form1" Method ="POST">';
   echo "Answered a question? check the question as answered then:";
   echo '<br />';
   echo '<input value="answered" name="submit" type="submit">';
   echo '</Form>';
echo '<hr />';
   if (isset($_POST['submit']))
   {
	    $QuestionID=$gegevens['QuestionID'];
	    mysql_query("UPDATE fastquestion SET Answered='yes' WHERE questionID='$QuestionID'") or die(mysql_error());
	    echo "done";
   }
}

 

the form and stuff works but ofcourse if i try to submit it changes all values or the last one but it doesnt change the one clicked specific. i understand that it is because of the while but i want to let them only have to click on a button to set the answered tag from no to yes. it would also be an option to use checkboxes but to specify the QuestionID of wich question is answered is difficult because i want the script to fetch all questions submitted to this table.

Posted

The action statement in the form tag isn't specified. Perhaps that will help?

echo "<Form Name ='form1' Method ='POST' action=" . $_SERVER['PHP_SELF'] . ">";

Posted

i changed the code a bit to

$ophalen = mysql_query("SELECT * FROM fastquestion") or die(mysql_error());
while ($gegevens = mysql_fetch_array($ophalen))
{
echo "type of the question:    ".$gegevens['typequestion'];
echo '<br />';
echo "the Question:		    ".$gegevens['Question'];
echo '<br />';
echo "in game name or E-mail:  ".$gegevens['ingamenameEmail'];
echo '<br />';
    echo "already answered:	    ".$gegevens['Answered'];
    echo '<br />';
    echo "<Form Name ='form1' Method ='POST' action=" . $_SERVER['PHP_SELF'] . ">";
    echo "Answered a question? check the question as answered then:";
    $QuestionID=$gegevens['questionID'];
    echo '<br />';
    echo '<input value="answered" name="submit" type="submit">';
    echo '</Form>';
echo '<hr />';
}
    if (isset($_POST['submit']))
    {
		    mysql_query("UPDATE fastquestion SET Answered='no' WHERE questionID='$QuestionID'") or die(mysql_error());
		    echo "done";
    }

but if i click it now it changes the last value is there anyway to collect the questionID from the one wich is clicked??

Posted

but if i click it now it changes the last value is there anyway to collect the questionID from the one wich is clicked??

 

Add a new input element to keep track of question ID, then handle the submit on top of page, escaping the id to avoid sql injections:

 

if (isset($_POST['submit'])) {
$QuestionID = $_POST['questionid'];
mysql_query("UPDATE fastquestion SET Answered='no' WHERE questionID='".mysql_real_escape_string($QuestionID)."'") or die(mysql_error());
echo "done";
}

$ophalen = mysql_query("SELECT * FROM fastquestion") or die(mysql_error());
while ($gegevens = mysql_fetch_array($ophalen))
{
echo "type of the question:	".$gegevens['typequestion'];
echo '<br />';
echo "the Question:				 ".$gegevens['Question'];
echo '<br />';
echo "in game name or E-mail:  ".$gegevens['ingamenameEmail'];
echo '<br />';
		echo "already answered:		 ".$gegevens['Answered'];
		echo '<br />';
		echo "<form name='form1' method='post' action=".$_SERVER['PHP_SELF'].">";
		echo "Answered a question? check the question as answered then:";
		$QuestionID=$gegevens['questionID'];
		echo '<br />';
		echo '<input name="questionid" value="'.$QuestionID.'" />';
		echo '<input value="answered" name="submit" type="submit" />';
		echo '</form>';
echo '<hr />';
}

 

In this way only one form is handled (the user can't submit more than one form at once) and the select sql query gets the updated value.

 

Also note that the html input element must be closed with " />"

Posted

thank u vry nice trick didnt thought of that does it rly make a difference if u put it on top?

i want to say i made the field hidden bcuz it made it look nicer.

The final script

if (isset($_POST['submit'])) {
    $QuestionID = $_POST['questionid'];
    mysql_query("UPDATE fastquestion SET Answered='yes' WHERE questionID='".mysql_real_escape_string($QuestionID)."'") or die(mysql_error());
    echo "done";
    }

$ophalen = mysql_query("SELECT * FROM fastquestion") or die(mysql_error());
while ($gegevens = mysql_fetch_array($ophalen))
{
   echo "type of the question:    ".$gegevens['typequestion'];
   echo '<br />';
   echo "the Question:		    ".$gegevens['Question'];
   echo '<br />';
   echo "in game name or E-mail:  ".$gegevens['ingamenameEmail'];
   echo '<br />';
    echo "already answered:	    ".$gegevens['Answered'];
    echo '<br />';
    echo "<form name='form1' method='post' action=".$_SERVER['PHP_SELF'].">";
    echo "Answered a question? check the question as answered then:";
    $QuestionID=$gegevens['questionID'];
    echo '<br />';
    echo '<input type="hidden"name="questionid" value="'.$QuestionID.'" />';
    echo '<input value="answered" name="submit" type="submit" />';
    echo '</form>';
   echo '<hr />';
}
?>

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...