0

it's a update form and i want when pressing edit, the input text to be changed with the text in mysql database but it doesn't work.
is there some kind of error which i don't see? or is there alternative way to do this form?

update.php

<?php
include("contentdb.php");
if(isset($_POST['submit'])){
$id= $_GET['id'];
$question=mysql_query("SELECT question FROM quiz");
$opt1=mysql_query("SELECT opt1 FROM quiz");
$opt2=mysql_query("SELECT opt2 FROM quiz");
$opt3=mysql_query("SELECT opt3 FROM quiz");
$answer=mysql_query("SELECT answer FROM quiz");
$sql = "UPDATE $table SET question='$question',opt1='$opt1',opt2='$opt2',opt3='$opt3',answer='$answer' WHERE id='$id'";
$result = mysql_query($sql);
if(mysql_query($sql)) {
echo "<br><br>The quiz has been succesfully updated.<br><br>\n";
}
else{
echo "error";
}
}
?>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<?php echo $id ?>">
    <b>Question:</b><br>
    <input type="Text" name="question" value="<?php echo $question ?>" size="50">
    <br>
    <b>Option 1:</b><br>
    <input type="Text" name="opt1" value="<?php echo $opt1 ?>" size="30">
    <br>
    <b>Option 2:</b><br>
    <input type="Text" name="opt2" value="<?php echo $opt2 ?>" size="30">
    <br>
    <b>Option 3:</b><br>
    <input type="Text" name="opt3" value="<?php echo $opt3 ?>" size="30">
    <br>
    <b>Answer</b> (must be identical to correct option):<br>
    <input type="Text" name="answer" value="<?php echo $answer ?>" size="30">
    <br>
    <br>
<input type="submit" name="update" value="Update information">
</form>

contentdb.php

<?php
include("config.php");

$db = mysql_connect("$hostname", "$user","$pass") or die(mysql_error());
mysql_select_db("$database",$db);

?>

config.php

<?php
$database = "database";
$user = "user";
$pass = "pass";
$hostname = "localhost";
$table = "table";
?>

editquizlist.php (the file with edit link)

<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='update.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>
4
Contributors
7
Replies
11
Views
7 Years
Discussion Span
Last Post by Koekoeksklok
0

what do you mean by "it doesnt work". What exactly happens?

when i press EDIT link in (editquizlist.php) it doesn't fill the form with the values from the database...

0

when i press EDIT link in (editquizlist.php) it doesn't fill the form with the values from the database...

Assuming that everything else works, try this for update.php:

<?php
include("contentdb.php");
if(!isset($_POST['update'])){
	$id= $_GET['id'];
	$sql = "SELECT * FROM quiz WHERE id=''";
	$result = mysql_query($sql) or die('\"'.$sql.'\" Query failed: ' . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_BOTH);
	$question = $row['question'];
	$opt1 = $row['opt1'];
	$opt2 = $row['opt2'];
	$opt3 = $row['opt3'];
	$answer = $row['answer'];
} else {
	$id = $_POST['id'];
	$question = $_POST['question'];
	$opt1 = $_POST['opt1'];
	$opt2 = $_POST['opt2'];
	$opt3 = $_POST['opt3'];
	$answer = $_POST['answer'];
	$sql = "UPDATE $table SET question='$question',opt1='$opt1',opt2='$opt2',opt3='$opt3',answer='$answer' WHERE id='$id'";
	$result = mysql_query($sql) or die('\"'.$sql.'\" Query failed: ' . mysql_error());
	echo "<br><br>The quiz has been succesfully updated.<br><br>\n";
}

?>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<?php echo $id ?>">
    <b>Question:</b><br>
    <input type="Text" name="question" value="<?php echo $question ?>" size="50">
    <br>
    <b>Option 1:</b><br>
    <input type="Text" name="opt1" value="<?php echo $opt1 ?>" size="30">
    <br>
    <b>Option 2:</b><br>
    <input type="Text" name="opt2" value="<?php echo $opt2 ?>" size="30">
    <br>
    <b>Option 3:</b><br>
    <input type="Text" name="opt3" value="<?php echo $opt3 ?>" size="30">
    <br>
    <b>Answer</b> (must be identical to correct option):<br>
    <input type="Text" name="answer" value="<?php echo $answer ?>" size="30">
    <br>
    <br>
<input type="submit" name="update" value="Update information">
</form>

My 2¢. Hope this helps.

0

no, it doesn't work ... if you have any other suggestions pls tell me
thanks anyway

Edited by solarb: n/a

0

In order for us to help, you have to tell me more than "It didn't work."

HOW didn't it work? That will help me understand where to go from here. :)

0

In order for us to help, you have to tell me more than "It didn't work."

HOW didn't it work? That will help me understand where to go from here. :)

From what i understand the script don't work because it maybe cant retrieve the values from database and place them as value in the form
it gets the id from the address and the other values from database , but i don't understand why it leaves them blank as i press edit.

i test it on my local server , is there someting in php.ini to be changed?
or can i send you all files so you can test them?

Thanks again

0

I think this:

#
$opt1=mysql_query("SELECT opt1 FROM quiz");
#
$opt2=mysql_query("SELECT opt2 FROM quiz");
#
$opt3=mysql_query("SELECT opt3 FROM quiz");

Should be :

#
$opt1=mysql_query("SELECT opt1 FROM quiz WHERE id=$id");
#
$opt2=mysql_query("SELECT opt2 FROM quiz WHERE id=$id");
#
$opt3=mysql_query("SELECT opt3 FROM quiz WHERE id=$id");

In fact all the SELECT statements should be followed by WHERE ... I think.

Hope it helps ;-)

Edited by Koekoeksklok: Saw that something needed to be added

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.