user2953877
user2953877

Reputation: 43

mysql UPDATE doesn't update the mysql database and it returns an error?

I really don't understand why this simple task has to be so complicated. I am trying to UPDATE the mysql database and no matter what I do, it doesn't update it!

I keep getting this Notice message as well..

Notice: Undefined index: thisID in

basically i get all the values echo-ed in the fields properly but I cannot update the mysql when i press the submit button and i get the error above!

I parse the info/data like so from listing.php

<?php 
// This block grabs the whole list for viewing
$product_list = "";
$sql = "SELECT * FROM products ORDER BY date_added DESC";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $quantity = $row["quantity"];
             $shipping = $row["shipping"];
             $category = $row["category"];
             $manufactor = $row["manufactor"];
             $special = $row["special"];
             $stock = $row["stock"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $product_list .= "Product ID: $id - <strong>$product_name</strong> - £$price - <em>Added $date_added</em> &nbsp; &nbsp; &nbsp; <a href='edit.php?pid=$id'>edit</a> &bull; <a href='listing.php?deleteid=$id'>delete</a><br />";
    }
} else {
    $product_list = "You have no products listed in your store yet";
}
?>

and this is the EDIT page edit.php

<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {

      $pid = mysqli_real_escape_string($db_conx, $_POST['thisID']);
    $product_name = mysqli_real_escape_string($db_conx, $_POST['product_name']);
    $price = mysqli_real_escape_string($db_conx, $_POST['price']);
    $quantity = mysqli_real_escape_string($db_conx, $_POST['quantity']);
    $shipping = mysqli_real_escape_string($db_conx, $_POST['shipping']);
    $category = mysqli_real_escape_string($db_conx, $_POST['category']);
    $manufactor = mysqli_real_escape_string($db_conx, $_POST['manufactor']);
    $special = mysqli_real_escape_string($db_conx, $_POST['special']);
    $stock = mysqli_real_escape_string($db_conx, $_POST['stock']);
    $details = mysqli_real_escape_string($db_conx, $_POST['details']);
    // See if that product name is an identical match to another product in the system
    $sql = "UPDATE products SET product_name='$product_name', price='$price', quantity='$quantity', shipping='$shipping', category='$category', manufactor='$manufactor', special='$special', stock='$stock', details='$details', WHERE id=$pid";
    if (!$sql) {
    echo mysqli_errno($db_conx) . ": " . mysqli_error($db_conx) . "\n";
    die();
}
    $query = mysqli_query($db_conx, $sql);
    header("location:");
    exit();
}
?>

could someone please shed a light on this for me?

Thanks in advance.

Upvotes: 0

Views: 156

Answers (1)

Mureinik
Mureinik

Reputation: 310983

You've got an extra comma before your `WHERE clause:

$sql = "UPDATE products SET product_name='$product_name', price='$price', quantity='$quantity', shipping='$shipping', category='$category', manufactor='$manufactor', special='$special', stock='$stock', details='$details', WHERE id=$pid";

Just remove it, and you should be fine:

$sql = "UPDATE products SET product_name='$product_name', price='$price', quantity='$quantity', shipping='$shipping', category='$category', manufactor='$manufactor', special='$special', stock='$stock', details='$details' WHERE id=$pid";

Upvotes: 5

Related Questions