PDA

View Full Version : Strange MySQL problem when using PHP forms to update database



scazy_lumbag
May 7th, 2009, 08:57 AM
Hi,

I've got a problem that I just can't seem to fix, and I'm sure the code I am using is all good, as I have used the exact same code for another form, and it worked fine, all I did was change the values..

I am using a PHP form to update values in a mySQL database, and I get this error when I try and submit.


Error in query: UPDATE proudnoise_update SET title = 'Welcome!', update = 'The site will be up and running soon!ds', link = 'www.houseofpod.net/proudnoise/' WHERE id = '8'. 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 'update = 'The site will be up and running soon!ds', link = 'www.houseofpod.net/p' at line 1

Heres my form:


<?php
include "inc/connect.php";
$id = $_GET['id'] ;
$query = "SELECT * FROM proudnoise_update WHERE id = '$id' ";
$result = mysql_query($query);

if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
?>

<form action="updates_edit_action.php" method="POST">
<table>
<tr>
<td colspan="2"class="labelcell"><label for="ID">ID</label></td>
<td colspan="2"class="fieldcell"> <input readonly="yes" name="id1" type="text" value="<?php print $row["id"]?>" size="3" ></td>
</tr>
<tr>
<td colspan="2"class="labelcell"><label for="title">Title</label></td>
<td colspan="2"class="fieldcell"> <input type="text" name="title1" id="title1" tabindex="1" value="<?php echo $row["title"] ?>"/></td>
</tr>
<tr>
<td colspan="2"class="labelcell"><label for="update">Update</label></td>
<td colspan="2"class="fieldcell"><TEXTAREA NAME="update1" ID="update1" ROWS=10 COLS=30><?php echo $row["update"]?></TEXTAREA></td>
</tr>
<tr>
<td colspan="2"class="labelcell"><label for="link">Link</label></td>
<td colspan="2"class="fieldcell"> <input type="text" name="link1" id="link1" value=<?php print $row["link"] ?>/></td>
</tr>
<tr>
<td colspan="4"><input type="submit" name="submit" class="box" value="Submit" tabindex="7" /></td>
</tr>
</table>
</form>

<?php
}
else print "No records were found";
?>

Heres the php action that the form links to (Apparantly the syntax error is in here, but I don't see it):


<?php
include "inc/connect.php";

$id = $_POST['id1'];
$title = $_POST["title1"];
$update = $_POST["update1"];
$link = $_POST["link1"];

$sql = "UPDATE proudnoise_update SET title = '$title', update = '$update', link = '$link' WHERE id = '$id'";

$result = mysql_query($sql);
if (mysql_affected_rows() > 0) {
header("Location: {$_SERVER['HTTP_REFERER']}");
}
else {
echo "Error in query: $sql. ".mysql_error();
exit;
}
?>Can anyone spot the problem? I've been staring at it for hours, tweaking it, stripping it down to the bare essentials and it just won't work..

Thanks

scazy_lumbag
May 7th, 2009, 09:06 AM
Ah, well I fixed it!

Only through reading my code on my post here did i spot the problem:

MySQL was getting confused as one of my fields was called "Update", I changed it to news, and it works fine now..

Should have spotted that one earlier..

simplistik
May 7th, 2009, 11:41 AM
in the future you can just escape your column and table names by adding `` around it so you could've left it as update but you just would've done


$sql = "UPDATE `proudnoise_update` SET `title` = '$title', `update` = '$update', `link` = '$link' WHERE `id` = '$id'";