I have an issue with receiving an error message when trying to do a simple update on a single record in my coding. I display a form with information on it, and allow the user to modify the information, and then click on an update button. Here is the code for that form:
<?php
session_start();
include 'config.php';
?>
<form method="post" action="teacherupdate.php">
<table width="500" cellspacing="2" cellpadding="2" align="left" bordercolor="#804040">
<tr>
<th>QID</th>
<th>Question</th>
<th>Answer</th>
<th>Update</th>
<th>Delete</th>
</tr>
<tr>
<?php
$query = "SELECT * FROM learn";
$result = mysql_query($query) or exit(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<td><?php echo $row['key'];?></td>
<td><textarea name="question" rows="2" cols="25"><?php echo $row['question'];?></textarea></td>
<td><textarea name="answer" rows="2" cols="25"><?php echo $row['answer'];?></textarea></td>
<td><button type="submit" name="update" value=<?php echo $row['key'];?>>UPDATE</button></td>
<td><button type="submit" name="delete" value=<?php echo $row['key'];?>>DELETE</button></td>
</tr>
<?php }
mysql_close();
?>
This works out great to display the information and allow for changing it. Now the form submits to the processing script, and this is the error message I get when the UPDATE button is pressed:
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 'key=1' at line 1
Here is the code in the processor script to do this:
<?php
session_start();
include 'config.php';
$question=$_POST['question'];
$answer=$_POST['answer'];
//$keyid=$_POST['update'];
$keyid = (ord($_POST['update']))-48;
$query = "UPDATE learn SET question='$question', answer='$answer' WHERE key=$keyid ";
$result = mysql_query($query) or exit(mysql_error());
?>
Previously in another php website which I had written, the successful code to do an update worked just fine. So, I simply copied and pasted the code, changing the info as needed to reflect the table names etc.. Here is the copied code:
$query = "UPDATE band SET genre='$genre', description='$description', links='$linkinfo', center='$maincontent', cogs='$widgets', footer='$footinfo' WHERE band = '$band' LIMIT 1";
I have tried modifying the code via information from many different google lookups for code on updating, but none work. I have verified that my $keyid does give me the correct index number, which is what the dsiplay record is based upon. The previous form displays all of the records for that table, and allows the user to update an individual record and update that record. The update button is linked to that record.
Any ideas?
Thank you;
Ice