【问题标题】:PHP online store, looping buttons & databases [closed]PHP在线商店,循环按钮和数据库[关闭]
【发布时间】:2016-02-06 01:19:03
【问题描述】:

所以我正在编写这段代码,我想通过使用循环获取数据库的数据来实现它。我遇到的问题是我似乎无法找到一种方法将值从我得到的按钮推送到数据库。我对想法持开放态度。谢谢!

<!Doctype HTML>
<html>
<head>
    <link rel="stylesheet" href="style.css" type="text/css"/>
    <title>Online Catalog</title>
</head>
<body>
<form action="" method="get">

<?php 
session_start();
require('database.php');

echo "<h1><a href=". 'index.php' .">Catalog</a></h1>
<b id=".cart."><a href=". 'cart.php' .">Show cart</a></b>";

if ($result = $connection->query("SELECT * FROM `store`")) {
    $row_cnt = $result->num_rows;
    while ($row = $result->fetch_assoc()) {
        echo "<p>
            <table>
                <th>
                    <img src=".$row[img_path].'/'.$row[img_name].">
                </th>
                <td>
                    <h2>". $row[name] ."</h2><br>
                    ". $row[description] ."<br><br>
                    Price : ". $row[price] ."$<br>
                </td>
                <td >
                    <input type='submit' name='add".$row[id]."' value='Add to cart'/>
                </td>
            </table>
        </p>
        ";

        if(isset($_GET['add.$row[id].'])){
            $cart=1;
            if(mysqli_query($connection, "INSERT INTO `store-project`.`store` (`incart`) VALUES ('$cart')")){
                mysqli_close($connection);
                header("Location: cart.php");
            }   
        }
    }


    $result->close();
}
?>
</form>
</body>
</html>

Here is the database

And here is how it generally looks

【问题讨论】:

  • 您好,Boian,欢迎来到 Stack Overflow。对不起,但绝对不清楚你在问什么。请查看导览页面并查看帮助中心,了解如何提出好的问题。谢谢。

标签: php shopping-cart


【解决方案1】:

尝试在表单中使用 Post 方法。

if($_POST['add.$row['id'].']){
$insert = "insert into store-project.store           
set incart = '".$cart1."';
mysqli_query($insert);
header("Location: cart.php");
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多