0

I have a page of entries with an edit button behind each entry, clicking it brings you to the edit page of that entry, the form has the existing data of that entry as default values. I change the values and click update, it redirects where it should, no errors but also no change in the data entry values.

My form:

<form method="post" action="edit.php" enctype="multipart/form-data">
Item name:</br>
<input type="text" name="item_name" value="<?php echo $row[1]; ?>"></br></br>
<input type="hidden" name="item_id" value="<?php echo $row[0]; ?>">
Item price:</br>
<input type="text" name="item_price" value="<?php echo $row[3]; ?>" ></br></br>
Item image:</br>
<input type="file" name="item_image"value="<?php echo $row[4]; ?>" ></br></br>
Item description:</br>
<textarea type="text" class="txtinput" cols="55" rows="20" name="item_description"><?php   echo $row[2]; ?>"</textarea></br></br>
<input type="submit" name="submit" value="Uppdate entry">
</form>

My PHP:

<?php
include("includes/connect.php");

if( isset($_GET['edit']))
{
$id= $_GET['edit'];
$res= mysql_query("SELECT * FROM shop_items WHERE item_id=$id");
$row= mysql_fetch_array($res);
}

if( isset($_POST['submit']))
{
    $item_name          = $_POST['item_name'];
    $id                 = $_POST['item_id'];
    $item_price         = $_POST['item_price'];
    $item_image         = $_FILES['item_image'];
    $image_tmp          = $_FILES['item_image'] ['tmp_name'];
    $item_description   = $_POST['item_description'];

    if ($item_name=='' or $item_price=='' or $item_image=='' or $item_description==''){


         echo "<script>alert('One or more of your fields are blank, please ensure you have entered content in ALL fields.')</script>";

}
else {
    move_uploaded_file($image_tmp,"images/$item_image");

    $sql = "UPDATE shop_item SET item_name='$item_name', item_price='$item_price,' item_image='$item_description', item_name='$item_description' WHERE item_id='$id'";
    echo "<meta http-equiv='refresh' content='0;url=admin_shop.php'>";
}
}
?>
2
  • The mysql_ functions are deprecated. Use mysqli or PDO instead Commented Apr 12, 2014 at 20:18
  • 2
    You're not actually running your SQL query to update the database! You've stored the SQL query in a variable, $sql, but you haven't actually called mysql_query($sql); Commented Apr 12, 2014 at 20:18

2 Answers 2

2

You're not actually running your SQL query to update the database! You've stored the SQL query in a variable, $sql, but you haven't actually called mysql_query($sql);

} else {
    move_uploaded_file($image_tmp,"images/$item_image");

    $sql = "UPDATE shop_item SET item_name='$item_name', item_price='$item_price,' item_image='$item_description', item_name='$item_description' WHERE item_id='$id'";


    // Add this line
    mysql_query($sql);

    echo "<meta http-equiv='refresh' content='0;url=admin_shop.php'>";
}

However, MySQL functionality is deprecated. You should look into PDO: https://www.php.net/pdo or mysqli: https://www.php.net/mysqli

Sign up to request clarification or add additional context in comments.

1 Comment

Still not doing anything... Might have other errors as well. Pretty new to this, so I'll do some digging
1

Change this -

 $sql = "UPDATE shop_item SET 
           item_name='".mysql_real_escape_string($item_name)."', 
           item_price='".mysql_real_escape_string($item_price)."',
           item_image='".mysql_real_escape_string($item_description)."', 
           item_name='".mysql_real_escape_string($item_description)."' 
         WHERE item_id='$id'";

 $exe = mysql_query($sql) or die(mysql_error());

NOTE: Avoid using mysql_* function since they are deprecated and use mysql_* or PDO instead.

3 Comments

Adding this gives me the following error: 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 'item_image='this is just a test to see how the shop will display items. This is ' at line 1
Use mysql_real_escape_string(). Check edits and try
Almost there! It updates all the info, but seems to not like the image. Notice: Array to string conversion in /Applications/XAMPP/xamppfiles/htdocs/templacreations/admin/edit.php on line 27 Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /Applications/XAMPP/xamppfiles/htdocs/templacreations/admin/edit.php on line 32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.