Jump to content

[SOLVED] update trouble


RyanSF07

Recommended Posts

I really can't figure this out. Please help.

Any and all ideas appreciated.

 

 

In testing this, the fields populate with data. I click "update information." I get the success message. BUT the update is never SET. That is, though I'm trying to update a field, the new data does not replace the old data in the database, eventhough I'm getting the success message.

I'm pulling my hair out. What in the heck could I be missing? Thank you again for your time and help.

 

PS This code looks gamey because I've tried so many different things. Even so, it works, and produces the result above. I'm missing something elemental, I guess  ???

 

<?php
session_start(); 
header("Cache-control: private"); //IE 6 Fix 
include("contentdb.php");
include "qinsert.php";

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<link href="main_styles.css" rel="stylesheet" type="text/css">
</head>
<body>

<div id = "edit_quiz_nav">
<table><tr>
<td>Edit quiz:</td>
<td><a href = "editquiz_details.php">details</a></td>
<td><a href = "editquiz_questions.php">questions</a> </td>
<td><a href = "editquiz_transcript.php">transcript</a></td>
<td><a href = "editquiz_notes.php">notes</a></td>
</tr>
</table>
</div>

<br>

<?php
$id = $_GET[id];
$update = $_POST['update'];
$question = $myrow['question']; 
$opt1 = $_POST['opt1'];
$opt2 = $_POST['opt2'];
$opt3 = $_POST['opt3'];
$answer = $_POST['answer'];


if ($update) {

// php validation 
if ($_POST[question] <> "") {
	$a = TRUE;
} else {
	$b = FALSE;
	$content .= "<p>Please enter a transcript for your quiz.</p>\n";
}

}
if ($a) {


$sql = "UPDATE quiz SET question = '$question', opt1='$opt1', opt2='$opt2', opt3='$opt3', answer='$answer' WHERE id='$id'";
$result = mysql_query($sql);
echo "

The quiz has been succesfully updated.

\n";
}
else if($id)
{
$result = mysql_query("SELECT * FROM quiz WHERE quiz.id='$id'",$db);
$myrow = mysql_fetch_array($result);
?>


Edit this question.<?echo "$_GET[id] $id $_SESSION[get]";?>
<form method="post" action="editquiz_updateQ.php">
<input type="hidden" name="id" value="<?php echo $myrow[id]?>">
    <b>Question:</b><br>
    <input type="Text" name="question" value="<?php echo $myrow[question]?>" size="50">
    <br>
    <b>Option 1:</b><br>
    <input type="Text" name="opt1" value="<?php echo $myrow['opt1']?>" size="30">
    <br>
    <b>Option 2:</b><br>
    <input type="Text" name="opt2" value="<?php echo $myrow['opt2']?>" size="30">
    <br>
    <b>Option 3:</b><br>
    <input type="Text" name="opt3" value="<?php echo $myrow['opt3']?>" size="30">
    <br>
    <b>Answer</b> (must be identical to correct option):<br>
    <input type="Text" name="answer" value="<?php echo $myrow['answer']?>" size="30">
    <br>
    <br>
<input type="Submit" name="update" value="Update information"></form>
<?

}

?>

</body>
</HTML>

Link to comment
Share on other sites

Hi Guys,

Thank you very much for your help. I'm glad to get an error, though still making some simple mistake. Almost there! Please have a look  :) 

 

Here is the error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/5640/domains/eslvideo.net/html/ESL_WebTech_2/editquiz_updateQ.php on line 62

 

(line 62 begins with: if($id) {  )

 

Here is the code:

<?php
session_start(); 
header("Cache-control: private"); //IE 6 Fix 
include("contentdb.php");
include "qinsert.php";

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<link href="main_styles.css" rel="stylesheet" type="text/css">
</head>
<body>

<div id = "edit_quiz_nav">
<table><tr>
<td>Edit quiz:</td>
<td><a href = "editquiz_details.php">details</a></td>
<td><a href = "editquiz_questions.php">questions</a> </td>
<td><a href = "editquiz_transcript.php">transcript</a></td>
<td><a href = "editquiz_notes.php">notes</a></td>
</tr>
</table>
</div>

<br>

<?php
$id = $_GET[id];
$update = $_POST['update'];
$question = $myrow['question']; 
$opt1 = $_POST['opt1'];
$opt2 = $_POST['opt2'];
$opt3 = $_POST['opt3'];
$answer = $_POST['answer'];


if ($update) {

// php validation 
if ($_POST[question] <> "") {
	$a = TRUE;
} else {
	$b = FALSE;
	$content .= "<p>Please enter a transcript for your quiz.</p>\n";
}

}
if ($a) {



$result = mysql_query($sql);
if($result){ echo "The quiz has been succesfully updated."; }
else{ echo "Failed updating: " . mysql_error() . "\n$sql\n"; }
}
if($id) {
$sql = mysql_query("SELECT * FROM quiz WHERE quiz.id='$id'",$db);
$myrow = mysql_fetch_array($result);
}

?>


Edit this question.<?echo "$_GET[id] $id $_SESSION[get]";?>
<form method="post" action="editquiz_updateQ.php">
<input type="hidden" name="id" value="<?php echo $myrow[id]?>">
    <b>Question:</b><br>
    <input type="Text" name="question" value="<?php echo $myrow[question]?>" size="50">
    <br>
    <b>Option 1:</b><br>
    <input type="Text" name="opt1" value="<?php echo $myrow['opt1']?>" size="30">
    <br>
    <b>Option 2:</b><br>
    <input type="Text" name="opt2" value="<?php echo $myrow['opt2']?>" size="30">
    <br>
    <b>Option 3:</b><br>
    <input type="Text" name="opt3" value="<?php echo $myrow['opt3']?>" size="30">
    <br>
    <b>Answer</b> (must be identical to correct option):<br>
    <input type="Text" name="answer" value="<?php echo $myrow['answer']?>" size="30">
    <br>
    <br>
<input type="Submit" name="update" value="Update information"></form>
<?



?>

</body>
</HTML>

Link to comment
Share on other sites

Hi again,

 

The code below is producing the following error:

 

Failed updating: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' Resource id #3' at line 1 UPDATE quiz SET question = '', opt1='call him', opt2='send this video to friends', opt3='both of the above', answer='both of the above' WHERE quiz.id='', Resource id #3

 

I'm googling this now. If you know the correct format to use for newer versions of MySQL, please let me know. (I'm new to this:)

Might just be something simple like "quotes" instead of 'single quotes' ?

 

Here's the code. Thanks again so much for your help!

 

if ($update) {

$sql = ("UPDATE quiz SET question = '$question', opt1='$opt1', opt2='$opt2', opt3='$opt3', answer='$answer' WHERE quiz.id='$id', $db");
$result = mysql_query($sql);
if($result){ echo "The quiz has been succesfully updated."; }
else{ echo "Failed updating: " . mysql_error() . "\n$sql\n"; }
}
else if($id)
{
$result = mysql_query("SELECT * FROM $table WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);

Link to comment
Share on other sites

Hey Again,

 

Now I'm getting this error again?

 

Parse error: syntax error, unexpected T_ELSE in ...editquiz_updateQ.php on line 47

 

line 47 begins with:  else if($id)

 

Note the new format for listing values. Hope this works. Please help me eliminate the T-ESLE error. Thanks very much!!

$sql = ("UPDATE quiz SET question = 'value[$question'], opt1='value[$opt1', opt2='value[$opt2]', opt3='value[$opt3]', answer='value[$answer]' WHERE quiz.id='$id', $db");
$result = mysql_query($sql);
if($result){ echo "The quiz has been succesfully updated."; }
else{ echo "Failed updating: " . mysql_error() . "\n$sql\n"; }

else if($id)
{
$result = mysql_query("SELECT * FROM $table WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
}

Link to comment
Share on other sites

Here is your obvious mistake.

 

else if($id)  // u do not even have a matched if statement for this. So it is unexpected T_ELSE error{

 

$result = mysql_query("SELECT * FROM $table WHERE id=$id",$db);

$myrow = mysql_fetch_array($result);  // This should be change to mysql_fetch_assoc or mysql_fetch_row.You have put an inccorect parameter for mysql_fetch_array. It should contain MYSQL_NUM or MYSQL_ASSOC for the second parameter. eg, mysql_fetch_array($result, MYSQL_ASSOC)}

Link to comment
Share on other sites

For your case,

 

if you would like to do PHP validation, add this:

 

if($question == ''){

echo "<script>alert(\"  Please fill in your question.\");history.go(-1)</script>";

 

}else{ 

 

    if ($update)

 

    $query = "SELECT * FROM $table WHERE id='$id' ";

    $result = mysql_query($query);

    if ($result)

    {

$query = "UPDATE quiz SET question = '$question', opt1='$opt1', opt2='$opt2', opt3='$opt3', answer='$answer' "; 

$result = mysql_query($query);

echo "Update is successful! Thank You";

 

   

 

 

 

   

  }

 

Link to comment
Share on other sites

Thank you for your help tauchai83,

 

Your code sample has moved things forward. Now, however, the changes take place to not only one row, but to all. They are global. In other words, $id = $_GET[id]; and WHERE id='$id' are not having any effect. If I change the question_text or answer_text to one question, all of the questions and answers in the table are updated to become that exact question and/or answer. Still, at least an update is happening. Hurray. Why is WHERE id='$id' failing?

(Note: echoing $id and GET_[id] display the correct values.)

 

If you have bit more time, I'd love to learn how to fix this.

Thank you very much!

 

<?php
session_start(); 
header("Cache-control: private"); //IE 6 Fix 
include("contentdb.php");

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="main_styles.css" rel="stylesheet" type="text/css">
</head>
<body>

<div id = "edit_quiz_nav">
<table><tr>
<td>Edit quiz:</td>
<td><a href = "editquiz_details.php">details</a></td>
<td><a href = "editquiz_questions.php">questions</a> </td>
<td><a href = "editquiz_transcript.php">transcript</a></td>
<td><a href = "editquiz_notes.php">notes</a></td>
</tr>
</table>
</div>

<br>

<?php
$id = $_GET[id];
$update = $_POST['update'];
$question = $_POST['question']; 
$opt1 = $_POST['opt1'];
$opt2 = $_POST['opt2'];
$opt3 = $_POST['opt3'];
$answer = $_POST['answer'];


if ($update)
    $query = "SELECT * FROM $table WHERE id='$id' ";
    $result = mysql_query($query);
    if ($result)
    {
    $query = "UPDATE quiz SET question = '$question', opt1='$opt1', opt2='$opt2', opt3='$opt3', answer='$answer' "; 
    $result = mysql_query($query);
    echo "Update is successful! Thank You";
    }

if($id) {
$result = mysql_query("SELECT * FROM $table WHERE id=$id");
$myrow = mysql_fetch_array($result);

?>
Edit this question.<?echo "$_GET[id] $id $_SESSION[get]";?>
<form method="post" action="editquiz_updateQ.php">
<input type="hidden" name="id" value="<?php echo $myrow[id]?>">
    <b>Question:</b><br>
    <input type="Text" name="question" value="<?php echo $myrow[question]?>" size="50">
    <br>
    <b>Option 1:</b><br>
    <input type="Text" name="opt1" value="<?php echo $myrow[opt1]?>" size="30">
    <br>
    <b>Option 2:</b><br>
    <input type="Text" name="opt2" value="<?php echo $myrow[opt2]?>" size="30">
    <br>
    <b>Option 3:</b><br>
    <input type="Text" name="opt3" value="<?php echo $myrow[opt3]?>" size="30">
    <br>
    <b>Answer</b> (must be identical to correct option):<br>
    <input type="Text" name="answer" value="<?php echo $myrow[answer]?>" size="30">
    <br>
    <br>
<input type="Submit" name="update" value="Update information"></form>
<?
}
?>

</body>
</HTML>

Link to comment
Share on other sites

Hey Fenway,

 

Sorry. This problem has been solved.

 

Where is the icon/button to click "solved"? A note on the PHP Help forum says it's at the bottom of the thread, but I don't see it. Is there another thread view? sorry for such a lame question, but I'd like to know how to signify solved to same yours and others' time.

Thanks again very much for your help over the past couple days!

Ryan

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.