Hi, I’m trying to write a simple php edit form based on Kevin Yank’s sitepoint guide but I’m getting an error and can’t figure out why. I suspect that there is a problem with posting the id out from the form. It seems to work fine until I hit the update button and get the following error:
Error updating submitted content.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 ‘WHERE id=’‘’ at line 3
Here are the relevant bits of code for the form process. If anyone can help me figure out what’s probably a very basic mistake, I’d be very appreciative.
//Code from content display page:
<tr>
<td><?php echo $basiccontenthome ?></td>
<td>
<form action="?" method="post">
<div>
<input type="hidden" name="id" value="<?php
htmlout($basiccontent['id'] = '1'); ?>"/>
<input type="submit" name="action" value="Edit"/>
</div>
</form>
</td>
</td>
</tr>
//Code from form
<form action="?<?php htmlout($action); ?>" method="post">
<div>
<label for="basiccontent">Type your content here:</label>
<textarea id="basiccontent" name="basiccontent" rows="10" cols="40"><?php
htmlout($basiccontent); ?></textarea>
</div>
<div>
<input type="hidden" name="id" value="<?php
htmlout($id); ?>"/>
<input type="submit" value="<?php htmlout($button); ?>"/>
</div>
</form>
// code from the index page
if (isset($_POST[‘action’]) and $_POST[‘action’] == ‘Edit’)
{
$id = mysqli_real_escape_string($link, $_POST[‘id’]);
$sql = “SELECT basiccontent FROM basicelements WHERE id=‘$id’”;
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = ‘Error fetching content details.’;
include ‘error.html.php’;
exit();
}
$row = mysqli_fetch_array($result);
$pagetitle = 'Edit Content';
$action = 'editform';
$basiccontent = $row['basiccontent'];
$id = $row['id'];
$button = 'Update content';
include 'form.html.php';
exit();
}
if (isset($_GET[‘editform’]))
{
$basiccontent = mysqli_real_escape_string($link, $_POST['basiccontent']);
$id = mysqli_real_escape_string($link, $_POST['id']);
$sql = "UPDATE basicelements SET
basiccontent='$basiccontent',
WHERE id='$id'";
if (!mysqli_query($link, $sql))
{
$error = 'Error updating submitted content.' . mysqli_error($link);
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
As, I said, any help would be much appreciated and sorry to be bringing such basic questions to the forum but I’m really struggling here! ![]()