数据没有在PHP mysql中更新

时间:2016-07-12 04:02:02

标签: php mysql sql

消息显示:

  

产品未更新!

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

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>

控制器

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

2 个答案:

答案 0 :(得分:0)

请更正您的更新查询,如下所示:

    return $class->$method;

答案 1 :(得分:0)

此查询SQL语法错误:

define('MERCHANTAPIURL', 'https://spectrocoin.com/api/merchant/1');