Start New Discussion within our Web Development Community

When I press the save button it wont update the row if i make some changes. whats wrong with my code can you please help mee.

What is lacking or what do i need to put . Thanks for who will answer I will really appreciate it.

$code =$_GET['Code'];
$result = mysql_query("SELECT * FROM books WHERE Code = '$code'");

$row = mysql_fetch_array($result);
if (!$result)
die("Error: Data not found..");
$code =$row['Code'];
$Title=$row['Title'] ;
$Price= $row['Price'] ;
$Stock=$row['Stock'] ;

$title_save = $_GET['title'];
$price_save = $_GET['price'];
$stock_save =$_GET['stock'];

mysql_query("UPDATE books SET Title ='$title_save', Price ='$price_save',
     Stock='$stock_save'  WHERE Code= '$code'")
            or die(mysql_error()); 
echo "Saved!";

header("Location: home_admin.php");         


<form method="get">
<td><input type="text" name="title" value="<?php echo $Title ?>"/></td>
<td><input type="text" name="price" value="<?php echo $Price ?>"/></td>
<td><input type="text" name="stock" value="<?php echo $Stock ?>"/></td>

    <td><input type="submit" name="editsave" value="save" /></td>



I suggest you echo the query to see whether it is what you are expecting. Modifiy the part where you construct the query like this:

// put the query in a variable
$q = "UPDATE books SET Title ='$title_save', Price ='$price_save', Stock='$stock_save' WHERE Code= '$code'";

// submit query using the variable
mysql_query($q) or die(mysql_error(); 

// add this to display the query

Now you will be able to see if the query gets constructed OK and you can copy it to phpmyadmin or mysql client and test it.

Two other notes:

  1. your form has no </form> tag. It should be after </table> tag.
  2. your form is missing an action attribute (I think it is required)
This article has been dead for over six months. Start a new discussion instead.