PHP在线商店,循环按钮&数据库

时间:2016-02-06 01:19:04

标签: php shopping-cart

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

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

1 个答案:

答案 0 :(得分:0)

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

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

希望有所帮助