【问题标题】:insert sql to database not inserting properly将sql插入数据库未正确插入
【发布时间】:2013-04-12 23:33:32
【问题描述】:

我想将我的数据插入数据库,但是,它不能正常工作,例如,当我将 4 个产品添加到购物车时,它只会插入四个中的两个。并且小计显示它捕获产品的第一数量作为我的小计,然后它显示整个产品的总量。而且我的数量没有插入到我的数据库中。

 <table>
    <tr>
        <th>Name</th>
        <th>Quantity</th>
        <th>Price per item</th>
        <th>Total cost</th>
    </tr>

    <?php
    $sql = "SELECT * FROM productsc WHERE id_product IN (";
    foreach ($_SESSION['cartCity'] as $id => $value) {
        $sql .= $id . ",";
    }
    $sql = substr($sql, 0, -1) . ") ORDER BY name ASC";
    $query = mysql_query($sql);
    $total_price = 0;
    if (!empty($query)) {
        while ($row = mysql_fetch_assoc($query)) {
            $subtotal = $_SESSION['cartCity'][$row['id_product']]['quantity'] * $row['price'];
            $total_price += $subtotal;
            ?>
            <tr>
                <td><?php echo $row['name']; ?></td>
                <td><input type="text" name="quantity-<?php echo $row['id_product']; ?>" size="5" value="<?php echo $_SESSION['cartCity'][$row['id_product']]['quantity']; ?>" /></td>
                <td><?php echo "$" . $row['price']; ?></td>
                <td><?php echo"$" . $_SESSION['cartCity'][$row['id_product']]['quantity'] * $row['price']; ?></td>
            </tr>
            <?php
            if ($row = mysql_fetch_assoc($query)) {
                $sql2 = "INSERT INTO order_details (name,quantity,price,subtotal) VALUES ('" . $row['name'] . "','" . $quantity . "','" . $row['price'] . "','" . $total_price . "')";
                $query2 = mysql_query($sql2) or die(mysql_error());
            }
        }
    }
    ?>

    <tr>
        <td></td>
        <td></td>
        <td>Total price:</td>
        <td><?php echo"$" . $total_price; ?></td>
    </tr>
</table>

【问题讨论】:

  • 你得到什么错误?
  • 使用允许配置抛出异常的数据库库,以便更好地处理变量。为什么不选择PDO?
  • 没有太大的错误,但数量没有显示在数据库中。并且小计没有正确加起来@MikeBrant

标签: php mysql cart sql-insert


【解决方案1】:

可能是因为你在循环内调用 $row = mysql_fetch_assoc($query) 做同样的事情,这意味着外部循环只会运行一半的时间,而你只会输入其他所有项目

试试

    while ($row = mysql_fetch_assoc($query)) {
        $subtotal = $_SESSION['cartCity'][$row['id_product']]['quantity'] * $row['price'];
        $total_price += $subtotal;
        ?>
        <tr>
            <td><?php echo $row['name']; ?></td>
            <td><input type="text" name="quantity-<?php echo $row['id_product']; ?>" size="5" value="<?php echo $_SESSION['cartCity'][$row['id_product']]['quantity']; ?>" /></td>
            <td><?php echo "$" . $row['price']; ?></td>
            <td><?php echo"$" . $_SESSION['cartCity'][$row['id_product']]['quantity'] * $row['price']; ?></td>
        </tr>
        <?php
        $sql2 = "INSERT INTO order_details (name,quantity,price,subtotal) VALUES ('" . $row['name'] . "','" . $quantity . "','" . $row['price'] . "','" . $total_price . "')";
        $query2 = mysql_query($sql2) or die(mysql_error());

    }

【讨论】:

  • 所以如果我在循环之外运行它们会更好吗?
  • 我认为你不需要if语句:if ($row = mysql_fetch_assoc($query))
  • 好吧,那我用什么代替它呢?只是 $query 会做吗? @otternq
  • 我将删除 $sql2 = "Insert..." 上方的行和 $query2 = mysql_query(...) 下方的行。如果我了解您的问题,您不必添加任何内容
  • 如果是这样,那么它不会进入我的数据库。我需要数据。如果我不需要插入 sql @otternq,它会很好地工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
相关资源
最近更新 更多