【发布时间】:2019-10-20 15:55:03
【问题描述】:
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
伙计们,我为购物袋做了一张桌子,它的名字叫篮子。我也得到了产品表名称和价格的 id。我的篮子桌就像
this(id[int],name[varchar],email[varchar],price[decimal],count[int],reduction[int],done[boolean]).
最后,当我点击购物袋时,该链接可以添加计数,但我的桌子是空的,没有一行。
<body>
<?php
include"database.php";
$name="";
$price=0;
$email=$_SESSION["email"];
$done=false;
$count=0;
// i get id form product table and product table like this(id[int],description[text],price[decimal])//
$id=$_GET["id"];
$query="SELECT * FROM product WHERE id=:id";
$result=$connect->prepare($query);
$result->bindparam(":id",$id);
$result->execute();
while($row=$result->fetch(PDO::FETCH_ASSOC)){
$name=$row["names"];
$price=$row["price"];
}
// and write a query for make count and insert or update in basket table;
$sql="SELECT * FROM basket WHERE name=:name and email=:email and done=:done";
$result=$connect->prepare($sql);
$result->bindparam(":name",$name);
$result->bindparam(":email",$email);
$result->bindparam(":done",$done);
$result->execute();
while($row=$result->fetch(PDO::FETCH_ASSOC)){
if(!empty($row["count"])){
$count=$row["count"];
}else{
$count=0;
}
}
if($count>0){
$upquery="UPDATE basket SET count=:count WHERE name=:name";
$result=$connect->prepare($upquery);
$count++;
$result->bindparam(":count",$count);
$result->bindparam(":name",$name);
$result->execute();
}else{
// for now i insert the info to table and give 0 value for reduction//
$count1=1;
$reduction=0;
$inquery="INSERT INTO basket (name,email,price,count,reduction)VALUES(:name,:email,:price,:count,:reduction)";
$result=$connect->prepare($inquery);
$result->bindparam(":name",$name);
$result->bindparam(":email",$email);
$result->bindparam(":price",$price);
$result->bindparam(":count",$count1);
$result->bindparam(":reduction",$reduction);
$result->execute();
}
?>
</body>
</html>
我没有 PHP 错误,为什么我的表是空的?
我回应 $email 和 $name 和 $price 和 $done 和 $done 和 $reduction ,这是正确的。
有什么问题请告诉我我是程序员界的初学者。
【问题讨论】:
-
为什么是
select和update/insert?你就不能insert into ... on duplicate update吗? -
你的更新条款,没有多大意义。您所做的是更新该产品名称的所有客户计数,您确定您只有 1 件具有该名称的商品吗?
-
嗨,您是什么意思?为什么更新是无意义的?当我的 $count>0 ...@nbk 时,我会写更新
-
什么项目名称? @nbk
-
UPDATE ... WHERE name=:name" 你填写 $name=$row["names"]; 从产品表中它至少也是电子邮件
标签: php mysql phpmyadmin sql-update sql-insert