Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

php - how to update a product in database

i am trying to update a product in mysql DB

here is my product table structure : [table product structure]1

it has a foreign key id_category that references to the table category's id : [table category structure]2

and this my code :

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!!";

when i try it, it states

modification problem!!

the POST method works fine but i can't figure out where's the error in the sql statement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you should change "category" to "id_category" in your query

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...