php - PHP mysql 中的数据未更新

标签 php mysql sql

消息显示:

Product not updated!

不通过表单更新。其余代码工作正常。数据库工作正常。我认为问题出在更新查询中。

HTML 表单位于 product.php 中, Controller 位于 product-controller.php 中,函数代码位于 admin-model.php 中。

选择插入和删除查询有效,但更新查询无效,我不明白为什么。

HTML 表单

<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");
}
?>

函数代码

 <?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);

关于php - PHP mysql 中的数据未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38319833/

相关文章:

sql - sql中如何根据条件计算两列数据

mysql - 针对特定模式的多对多复杂 MySQL 查询

向雷鸟发送 html 邮件时,PHP 邮件无法显示链接

php mysql数据库日期范围表格

PHP openssl_sign 停止执行所有脚本

mysql - mysql memcached ndb 集群中如何进行更新

php - Laravel 5.1 将关系数据加载到选定行

php - Firebase 使用 php 过滤数据

PHP表单使用多维数组提交到数据库

sql - 更新主表的复杂sql查询