Sorry for the amateur question here, but I've created a form that, for the life of me, I cannot debug the reason as to why it won't submit the updates into the database.
An example is here: www.dominicharrison.co.uk/projects/Portfolio/admin
Code:
<?php
require_once('../_inc/glob.php');
$query = mysql_query("SELECT * FROM `pages`");
echo "<table border=\"1\"><tr><td>ID</td><td>Name</td><td>Location</td><td>Edit</td><td>Delete</td></tr>";
while($array = mysql_fetch_assoc($query)) {
echo "<tr><td>" . $array['id'] . "</td><td>" . $array['name'] . "</td><td>" . $array['location'];
echo "</td><td><a href=\"?editid={$array['id']}\">Edit</a></td>";
echo "<td><a href=\"?delete={$array['id']}\">Delete</a></td></tr>";
}
echo "</table>";
// Defines the edit and delete functions
$edit = mysql_real_escape_string($_GET['editid']);
$delete = mysql_real_escape_string($_GET['delete']);
// Edit function!
$query2 = mysql_query("SELECT * FROM `pages` WHERE `id` = '{$edit}'");
$array2 = mysql_fetch_assoc($query2);
if($edit != "") {
?>
<p>
<form method="POST" action="">
<input name="name" value="<?php echo $array2['name']; ?>" type="text"><br />
<input name="location" value="<?php echo $array2['location']; ?>" type="text"><br />
<input type="submit" value="Edit!">
</form></p>
<?php
if($_POST['submit'])
{
$name = mysql_real_escape_string($_POST['name']);
$location = mysql_real_escape_string($_POST['location']);
mysql_query("UPDATE `pages` SET id = '{$edit}', name = '{$name}', location = '{$location}' WHERE id = '{$edit}'");
}
}
Your form isn't submitting the id, so your SQL can't match anything in the WHERE id=
clause.
1). Add a hidden field to your form to submit the editID you're getting from the $_GET array:
<form method="POST" action="">
<input type="hidden" name="id" value="<?php echo $edit; ?>" />
<input name="name" value="<?php echo $array2['name']; ?>" type="text"><br />
<input name="location" value="<?php echo $array2['location']; ?>" type="text"><br />
<input type="submit" value="Edit!">
</form>
2). Capture the edit id in your submit logic:
if($_POST['submit'])
{
$name = mysql_real_escape_string($_POST['name']);
$location = mysql_real_escape_string($_POST['location']);
$edit = mysql_real_escape_string($_POST['id']);
mysql_query("UPDATE `pages` SET id = '{$edit}', name = '{$name}', location = '{$location}' WHERE id = '{$edit}'");
}
You don't have a $_POST['submit'];
var_dump($_POST);
Add name to your submit button. This creates the post variable that you're missing.
<input type="submit" name="submit" value="Edit!">
And your table will update. However, you will need to refresh the page, as you're calling the select long before you call update.
Since your form posts to the same page, this is what happens...
$edit = mysql_real_escape_string($_GET['editid']); // THIS IS NULL FOR POSTS
...
if($edit != "") { // THIS WON'T BE TRUE FOR POSTS
and thus, it will never get to your POST process of your script. Reorganize your script - your POST processing code should likely be at the top (IF you continue to go with this one-script business.... which you shouldn't).