Jump to content

[SOLVED] Update A Row


EternalSorrow

Recommended Posts

I'm currently working on creating a form which will allow me to edit rows in my mysql database, but I've hit a large snag.  I have only two test rows in the database and am using two fields called 'title' and 'author'.

 

Here are the problems:

 

[*]When I submit the edit form to the database to the first row, it does not change.

[*]However, the second row information will change to the first row information (as if I had inputted the information into the second row all along).

 

I'd be grateful for any help on this matter, as I'm stumped as to the problem.

 

Here's the full code for the edit, and here's a link to a 'working' example.

 

<?

mysql_connect(localhost,user,pw);
@mysql_select_db(db) or die( "Unable to select database");

if(!isset($cmd))
{

$result = mysql_query("SELECT * FROM `book`");

while ($row = mysql_fetch_array($result))
{
extract($row);

echo "<a href='edit.php?cmd=edit&id=$id'><b>$title</b> by $author</a>";
echo "<p>";
}
}
?>

<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$sql = "SELECT * FROM book WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>

<form action="edit.php" method="post">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">

Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>"><br>
Author:<INPUT TYPE="TEXT" NAME="author" VALUE="<?php echo $myrow["author"] ?>"><br>

<input type="hidden" name="cmd" value="edit">

<input type="submit" name="submit" value="submit">

</form>

<? } ?>

<?
if ($_POST["submit"])
{
$title = $_POST["title"];
$who = $_POST["author"];

$sql = "UPDATE book SET title='$title',author='$author' WHERE id=$id" or die(mysql_error());

$result = mysql_query($sql);
echo "Thank you! Information updated.";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/165891-solved-update-a-row/
Share on other sites

change

<?
if ($_POST["submit"])
{
$title = $_POST["title"];
$who = $_POST["author"];

$sql = "UPDATE book SET title='$title',author='$author' WHERE id=$id" or die(mysql_error());

$result = mysql_query($sql);
echo "Thank you! Information updated.";
}
}
?>

tochange

<?
if ($_POST["submit"])
{
$title = $_POST["title"];
$who = $_POST["author"];
$id = $_POST['id'];
$sql = "UPDATE book SET title='$title',author='$who' WHERE id=$id"; 

$result = mysql_query($sql) or die(mysql_error());
echo "Thank you! Information updated.";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/165891-solved-update-a-row/#findComment-875029
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.