【问题标题】:how to update a product in database如何更新数据库中的产品
【发布时间】:2020-12-31 20:51:22
【问题描述】:

我正在尝试更新 mysql DB 中的产品

这是我的产品表结构: 【表产品结构】

它有一个外键 id_category 引用表类别的 id : 【表类结构】

这是我的代码:

if(isset($_POST['submit'])){
            $id=htmlspecialchars($_POST['id']);
            $name=htmlspecialchars($_POST['name']);
            $photo=htmlspecialchars($_POST['photo']);  
            $quantity=htmlspecialchars($_POST['quantity']);
            $price=htmlspecialchars($_POST['price']);
            $description=htmlspecialchars($_POST['description']);
            $category=htmlspecialchars($_POST['category']);
            $brand=htmlspecialchars($_POST['brand']); 
            $obj=new Connexion();
            $cnx=$obj->getconnexion();
            //sql statement
            $sql="update product  set id=$id,name='$name',photo='$photo',quantity=$quantity,price=$price,description='$description',category=(select id_category from category where category_name='$category'),brand='$brand' where id=1";
            $res=$cnx->exec($sql);
            if($res)
            { header("location:findall.html.php");}
            else echo "modification problem!!";

当我尝试它时,它会说

修改问题!!

POST 方法工作正常,但我不知道 sql 语句中的错误在哪里。

【问题讨论】:

    标签: php sql database


    【解决方案1】:

    您应该在查询中将“类别”更改为“id_category”

    id_category=(select id_category from category where category_name='$category')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 2014-08-30
      • 2019-12-19
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多