英文:
Inserting values into database when a button is clicked
问题
Here is the translated content:
当我点击“添加到收藏夹”时,它会添加'user_id'和'golf_id',但是'golf_price'始终以'0'的形式输入,而不是实际价格。 任何帮助将不胜感激?
public function isFavourite($golf_id){
$query = "SELECT * FROM user_favourites WHERE user_id = :user_id AND golf_id = :golf_id";
$stmt = $this->Conn->prepare($query);
$stmt->execute([
"user_id" => $_SESSION['user_data']['user_id'],
"golf_id" => $golf_id
]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
public function toggleFavourite($golf_id){
// 检查俱乐部是否已在购物篮中
$is_favourite = $this->isFavourite($golf_id);
if($is_favourite) {
// 已经是收藏夹中的项目 - 所以移除
$query = "DELETE FROM user_favourites WHERE user_fav_id = :user_fav_id";
$stmt = $this->Conn->prepare($query);
$stmt->execute([
"user_fav_id" => $is_favourite['user_fav_id']
]);
return false; // 返回false表示“已移除”
} else {
// 不是收藏夹中的项目 - 所以添加
$query = "INSERT INTO user_favourites (user_id, golf_id, golf_price) VALUES (:user_id, :golf_id, :golf_price)";
$stmt = $this->Conn->prepare($query);
$golf_price = "SELECT golf_price FROM products WHERE golf_id = :golf_id";
return $stmt->execute(array(
"user_id"=> $_SESSION['user_data']['user_id'],
"golf_id" => $golf_id,
"golf_price" => $golf_price
));
return true; // 返回true表示“已添加”
// 1. 使用golf_id从产品中选择以获取与golf_id匹配的golf_price
}
}
英文:
When I click add to favourites, it add's the user_id and the golf_id, however 'golf_price' keeps entering as '0' instead of the actual price. Any help would be appreciated?
public function isFavourite($golf_id){
$query = "SELECT * FROM user_favourites WHERE user_id = :user_id AND golf_id = :golf_id";
$stmt = $this->Conn->prepare($query);
$stmt->execute([
"user_id" => $_SESSION['user_data']['user_id'],
"golf_id" => $golf_id
]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
public function toggleFavourite($golf_id){
// Check if club is already in basket
$is_favourite = $this->isFavourite($golf_id);
if($is_favourite) {
// Is already favourite - so remove.
$query = "DELETE FROM user_favourites WHERE user_fav_id = :user_fav_id";
$stmt = $this->Conn->prepare($query);
$stmt->execute([
"user_fav_id" => $is_favourite['user_fav_id']
]);
return false; // Return false for "removed"
} else {
// Is not favourite - so add
$query = "INSERT INTO user_favourites (user_id, golf_id, golf_price) VALUES (:user_id, :golf_id, :golf_price)";
$stmt = $this->Conn->prepare($query);
$golf_price = "SELECT golf_price FROM products WHERE golf_id = :golf_id";
return $stmt->execute(array(
"user_id"=> $_SESSION['user_data']['user_id'],
"golf_id" => $golf_id,
"golf_price" => $golf_price
));
return true; // Return false for "added"
//1.use golf_id to select from products to get golf_price where golf_id matches
}
}
答案1
得分: 0
You should put the SELECT
query into the INSERT
query.
// Is not favourite - so add
$query = "INSERT INTO user_favourites (user_id, golf_id, golf_price)
SELECT :user_id, golf_id, golf_price
FROM products WHERE golf_id = :golf_id";
$stmt = $this->Conn->prepare($query);
return $stmt->execute(array(
"user_id" => $_SESSION['user_data']['user_id'],
"golf_id" => $golf_id
));
英文:
You're never executing the query in $golf_price
, you're trying to set golf_price
to that literal string. Since it's not numeric, it gets converted to 0
.
You should put the SELECT
query into the INSERT
query.
// Is not favourite - so add
$query = "INSERT INTO user_favourites (user_id, golf_id, golf_price)
SELECT :user_id, golf_id, golf_price
FROM products WHERE golf_id = :golf_id";
$stmt = $this->Conn->prepare($query);
return $stmt->execute(array(
"user_id"=> $_SESSION['user_data']['user_id'],
"golf_id" => $golf_id
));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论