基于会话向数据库输入多个数据时出错

时间:2016-11-04 07:34:20

标签: php session

所以我创建了一个基于会话的购物车,然后成功输入到数据库,但输入的数据是错误的。

输入数据库后输入的错误数据是barang_id& kuantitas。

这里是代码:

if (isset($_SESSION['items'])) {
                foreach ($_SESSION['items'] as $key => $value) {
                    $barang_id = $_SESSION['items'][$key];
                    $kuantitas = $value;

                    $query_barang = mysqli_query($conn, "SELECT * FROM `barang` WHERE `id` = '$barang_id'");
                    // ambil data dari data barang
                    $rs_barang = mysqli_fetch_array($query_barang);
                    $harga = $rs_barang['harga'];

                    $jumlah_harga = $harga * $kuantitas;
                    $total += $jumlah_harga;
                    mysqli_query($conn, "INSERT INTO `item_penjualan` (`penjualan_id`, `barang_id`, `harga`, `kuantitas`, `jumlah_harga`) VALUES ('$penjualan_id', '$barang_id', '$harga', '$kuantitas', '$jumlah_harga')");
                }
            }

以及购物车代码:

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="viewer">
    <tr>
        <th align="left" scope="col">Kode Barang</th>
        <th align="left" scope="col">Nama Barang</th>
        <th align="right" scope="col">Harga</th>
        <th align="right" scope="col">Qty</th>
        <th align="right" scope="col">Jumlah Harga</th>
        <th align="right" scope="col">Aksi</th>
    </tr>
    <?php
    $total = 0;
    mysqli_select_db($conn,$database_conn);
    if (isset($_SESSION['items'])) {
        foreach ($_SESSION['items'] as $key => $val) {
            $query = mysqli_query($conn, "select barang.id, barang.nama, barang.harga from barang where barang.id = '$key'");
            $rs = mysqli_fetch_array($query);

            $jumlah_harga = $rs['harga'] * $val;
            $total += $jumlah_harga;
            ?>
            <tr>
                <td><?php echo $rs['id']; ?></td>
                <td><?php echo $rs['nama']; ?></td>
                <td align="right"><?php echo number_format($rs['harga']); ?></td>
                <td align="right"><?php echo number_format($val); ?></td>
                <td align="right"><?php echo number_format($jumlah_harga); ?></td>
                <td align="right"><a href="cart.php?act=plus&amp;barang_id=<?php echo $key; ?>&amp;ref=index.php">+</a> | <a href="cart.php?act=min&amp;barang_id=<?php echo $key; ?>&amp;ref=index.php">-</a> | <a href="cart.php?act=del&amp;barang_id=<?php echo $key; ?>&amp;ref=index.php">Hapus</a></td>
            </tr>
            <?php
            mysqli_free_result($query);
        }
    }
    ?>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td align="right">&nbsp;</td>
        <td align="right">&nbsp;</td>
        <td align="right"><?php echo number_format($total); ?></td>
        <td align="right"><a href="cart.php?act=clear&amp;ref=index.php">Clear</a></td>
    </tr>

    <tr>
        <td colspan="6">
            <hr/>
            <a href="checkout.php">Checkout</a>
        </td>
    </tr>
</table>

0 个答案:

没有答案