简体   繁体   中英

Data not updating in PHP mysql

Message show:

Product not updated!

Not updating through form. Rest of the code is working fine. Database is working fine. I think problem is in update query.

HTML form is in product.php and Controller is in product-controller.php and function code is in admin-model.php .

Select insert and delete query is working but update query is not working I don't understand why.

HTML Form

<form action="product-controller.php?type=update" method="post">
    <input type="hidden" value="<?php echo $r["productid"]; ?>" name="productID" id="productID"/>
    <div>
        <label>Product Name</label>
        <input type="text" value="<?php echo $r["product_name"]; ?>" name="productName" placeholder="Product Name">
    </div>
    <div>
        <label>Product Price</label>
        <input type="number" value="<?php echo $r["product_price"]; ?>" name="productPrice" placeholder="Product Price">
    </div>
    <div>
        <label>Product Quantity</label>
        <input type="number" value="<?php echo $r["product_quantity"]; ?>" name="productQuantity" placeholder="Product Quantity">
    </div>
    <div>
        <label>Product Description</label>
        <textarea name="productDesc"><?php echo $r["product_description"]; ?></textarea>
    </div>
    <div>
        <button type="submit">Update Product</button>
    </div>
</form>

Controller

<?php
include("admin-model.php");
if(isset($_GET["type"]))
{
    $dbobj=new AdminModel;
    if($_GET["type"]=="add")
    {
            $name=$_POST["productName"];
            $price=$_POST["productPrice"];
            $quantity=$_POST["productQuanity"];
            $desc=$_POST["productDesc"];
            $res=$dbobj->addProduct($name, $price, $quantity, $desc);

            if($res>0)
            {  
                header("Location: product.php?msg=sucess");
            }
            else
            {
                header("Location: product.php?msg=fail");
            }

    }
    else if($_GET["type"]=="update")
    {
            $id=$_POST["productID"];
            $name=$_POST["productName"];
            $price=$_POST["productPrice"];
            $quantity=$_POST["productQuantity"];
            $desc=$_POST["productDesc"];
            $res=$dbobj->updateProduct($id, $name, $price, $quantity, $desc);

            if($res>0)
            {  
                header("Location: product.php?msg=upsucess");
            }
            else
            {
                header("Location: product.php?msg=upfail");
            }

    }
    else if($_GET["type"]=="del")
    {
            $id=$_GET["productID"];
            $res=$dbobj->deleteProduct($id);
            if($res>0)
            {  
                header("Location: product.php?msg=delsucess");
            }
            else
            {
                header("Location: product.php?msg=delfail");
            }

    }
}
else
{
    header("Location: product.php");
}
?>

Function Code

 <?php
class AdminModel
{
    var $con, $com;
    var $res;
    public function __construct()
    {
        $this->con=mysql_connect("localhost", "root", "");
        mysql_select_db("mywebsitedb");
    }

    public function updateProduct($id, $name, $price, $quantity, $desc)
            {
                $this->com=mysql_query("update product set product_name='$name', product_price='$price', product_quantity='$quantity', product_description='$desc', where productid='$id'", $this->con);
                return $this->com;
            }

请按以下步骤更正您的更新查询:

$this->com=mysql_query("update product set product_name='$name', product_price='$price', product_quantity='$quantity', product_description='$desc' where productid='$id'", $this->con);

This Query SQL is wrong syntax :

update product set product_name='$name', product_price='$price', 
product_quantity='$quantity', product_description='$desc', where productid='$id'
-->
update product set product_name='$name', product_price='$price', 
product_quantity='$quantity', product_description='$desc' where productid='$id'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM