gpt4 book ai didi

php - 从sql php中删除动态加载iterm

转载 作者:可可西里 更新时间:2023-11-01 08:24:10 26 4
gpt4 key购买 nike

我正在尝试为学校项目制作一个非常基本的手推车。这些项目是从数据库动态加载的,我希望能够在 POST 时动态删除它们,但我不知道如何动态跟踪名称属性,所以我可以在我的删除查询中发布。

 <?php 

require_once('dbconnection.php');
session_start(); //starts seesion for username this needs to be at the top of every page.

// head
$page_title = 'Search Results';
require_once('layouts/head.php');

//Handles deleted items
if (isset($_POST['delete'])) {
$remove = $mysqli->prepare("DELETE cart.* FROM `cart`
inner join 'product'
on product.id = cart.product_id
WHERE `product_name` = ?");

//TODO:get the post for the specific item name and add to bind method
$remove->bind_param('s', $name);

if(!$remove->execute() === true) {
echo $mysqli->error;
}
}


<body>
<!-- inlclude nav -->
<?php require_once('layouts/nav.php'); ?>

<main role="main">

<!-- include jumbo -->
<?php
require_once('layouts/jumbotron.php');
?>

<div class="container">
<!-- Container Heading -->
<h1>
<?php
if(isset($_SESSION['username'])){
echo $_SESSION['username']. "'s Cart <hr>" ;

}
else{
echo "Your Cart <hr>";
}
?>

</h1>




<ul class="panel panel-default">

<?php

global $globalArr;
$sum = 0;

// TODO: check if user has an active cart if they do then display itemes
$sql = "SELECT user.username, status.status_type, product.product_name, product.price, product.id from cart
inner join status
on status.id = cart.status_id
inner join user
on user.id = cart.user_id
inner join product
on product.id = cart.product_id
WHERE status_type = 'purchased' AND username = '".$_SESSION['username']."'";

$result = $conn->query($sql);

// TODO: add a remove from cart
if ($result->num_rows > 0) {
// output data of each row
echo "<h3>" .$result->num_rows. " item(s) in cart </h3>";


while($row = $result->fetch_assoc()) {

echo '<from action="cart.php" method="post"><li class="list-group-item" name="'.$row["product_name"].'">'
.$row["product_name"]. ' - $'. $row["price"].' <button class="badge" type="submit" name="delete"> X </button> </li></form>';
array_push($globalArr, $row["product_name"]);
$sum += floatval($row['price']);

}//END WHILE

echo $sum;

} else {

echo "<h3> YOUR CART IS EMPTY! BUY SOMETHING! </h3>";
}

echo '</div>'


?>


</ul>
<hr>
</div>
<!-- /container -->

</main>

<!-- include footer -->
<?php
require_once('layouts/footer.php');
?>

<!-- Placed at the end of the document so the pages load faster -->
<?php
require_once('layouts/scripts.php');
?>

</body>
</html>

我认为我的帖子非常清楚,但我被要求添加更多细节,所以这里有一些更酷的细节可能会有所帮助。我很难解决这个问题。您可能拥有的任何解决方案将不胜感激。感谢您花时间表现得如此出色,而不是抨击我的帖子。再次感谢!

最佳答案

我不确定我是否理解你的问题,但如果你想通过 POST 请求发送要删除的购物车项目的名称,你可以使用 hidden inputs为此

while($row = $result->fetch_assoc()) {
echo '<from action="cart.php" method="post">'
.'<li class="list-group-item" name="'.$row["product_name"].'">'
.$row["product_name"]. ' - $'. $row["price"]
.'<input type="hidden" name="item_name" value="'.$row["product_name"].'">'
.'<button class="badge" type="submit" name="delete"> X </button>'
.'</li>'
.'</form>';
array_push($globalArr, $row["product_name"]);
$sum += floatval($row['price']);
}//END WHILE

然后使用$remove->bind_param('s', $_POST['item_name']);

希望对你有帮助

关于php - 从sql php中删除动态加载iterm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50053182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com