Jump to content

Php Question Answering


Recommended Posts

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.

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 />';
}
?>

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...