更新购物车数量PHP

时间:2020-03-12 13:51:50

标签: php cart

我正在使用PHP / mySQL构建测试购物网站

这是我的代码:

show_product.php:

const fs = require('fs')
const crafts = require('./FILEPATCH');

if (message.content.startsWith(prefix + " craft")) {
    let args = message.content.split(' ')
    if(args.length < 4) return message.reply('Incorrect format, use `!v craft T4 Plate Boots`')

    let category = args.splice(args.length - 2, 2).join(' ')
    let type = args[args.length -1]

    if (!crafts.hasOwnProperty(category)) return message.reply('Incorrect format, use `!v craft T4 Plate Boots`')
    if (!crafts[category].hasOwnProperty(type)) return message.reply('Incorrect format, use `!v craft T4 Plate Boots`')

    crafts[category][type].push(message.author.id)

    fs.writeFileSync('filepatch/craft.json', JSON.stringify(crafts));
}

addtocart.php

<form method ="get" action="addtocart.php">
                <input type="hidden" name="id_product" value='.$row['id_product'].'>
                    <tr>
                        <th colspan="2"><a href="#">'.$row['name'].'</a></th>
                    </tr>                     
                        <td>
                        <a href="#"><img align="center"  src='.$row['photo'].' alt=""></a>
                    </td>
                    <td>'.mb_substr($row['description'],0,200).'....</td>                                                 
                    <tr></tr>                           
                        <td>
                        '.$row['price'].'€<input type="number" name="quantity" value="1" min="1" max="20">
                    </td>

                    <td> <button type="submit" class="btn">add to cart</button></form>

和cart.php的一些代码

session_start()
if(isset($_GET) & !empty($_GET)){   
    $id_product = $_GET['id_product'];
    if(isset($_GET['quantity']) & !empty($_GET['quantity'])){ $quant = $_GET['quantity']; }else{ $quant = 1;}
    $_SESSION['cart'][$id_product] = array("quantity" => $quant);   
    header('location: cart.php');
}else{

    header('location: cart.php');
}
echo "<pre>";
print_r($_SESSION['cart']);
echo "</pre>";

?>

如果我向购物车发送一定数量的物品,则该物品正在工作。

问题是我无法更改cart.php中的数量,我对Java知之甚少。 请任何帮助如何做??

1 个答案:

答案 0 :(得分:0)

我是在购物车内提交表单的:)

<form class="userform" method='get' name="change" action="addtocart.php">
                        <input type='hidden' name='id_product' value="<?php echo $row["id_product"]; ?>" />
                        <td><input size="5" type="number" name="quantity" value="<?php echo $value['quantity'] ?>" min="1" max="20" onchange="this.form.submit()"></td></form>