Skip to content
Advertisement

PHP Shopping cart // show results of different rows

Hell community, I am trying to build up a shopping cart which basically looks like an excel table. The layout and the main code is working good but I can add only products from one row to the shopping cart table. My target is to choose and add as much products I want from row I want.

Here the code I am using:

<?php
        $query = "SELECT * FROM products ORDER BY id ASC";
        $result = mysqli_query($connect, $query);
            if(mysqli_num_rows($result) > 0)
            {
            while($row = mysqli_fetch_array($result))
        { 
    ?>

    <div id="product">
        <table>
            <tr>
                <th>Product</th>
                <th>A</th>
                <th>B</th>
                <th>C</th>
                <th>D</th>
                <th>E</th>
            </tr>
                <?php
                    $conn = mysqli_connect("localhost", "root", "", "shop");
                    // Check connection
                    if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                    }
                    $sql = "SELECT id, product, A, B, C, D, E FROM products";
                    $result = $conn->query($sql);
                    if ($result->num_rows > 0) {
                    // output data of each row
                    while($row = $result->fetch_assoc()) {
                        $Id = $row ["id"];
                        $Product = $row ["product"];
                        $A = $row ["A"];
                        $B = $row ["B"];
                        $C = $row ["C"];
                        $D = $row ["D"];
                        $E = $row ["E"];

                        echo "<tr>";
                        echo "<td>" . $row["product"] . "</td><br />";
                        echo "<td>" . $row["A"] . "
                        <form id= "Form_1" method="post" action="tbl_sc.php?action=add&id=$Id">
                        <input name="add_to_cart" type="submit" value="Add to cart"></td><br />";
                        echo "<td>" . $row["B"] . "
                        <input name="add_to_cart" type="submit" value="Add to cart"></td><br />";
                        echo "<td>" . $row["C"] . "
                        <input name="add_to_cart" type="submit" value="Add to cart"></td><br />";
                        echo "<td>" . $row["D"] . "
                        <input name="add_to_cart" type="submit" value="Add to cart"></td><br />";
                        echo "<td>" . $row["E"] . "
                        <input name="hidden_name" type="hidden" value="$Product">
                        <input name="hidden_price" type="hidden" value="$A">
                        <input name="add_to_cart" type="submit" value="Add to cart"></td><br />";
                        echo "<td>
                        </form></td></tr>"; 
                    }
                    echo "</table>";
                    } else { echo "0 results"; }
                ?>
        </table>
    </div>
    <?php
            }
        }
    ?>

What I have found out is that when I cklick th “Add to cart” button my code shows always only the content in the shopping cart table of the variable which is mentioned in the “hidden_price” row. What do I have to add or change in the code to be able to add also the other rows (B, C, D…) to the shopping cart and to be able to choose a product from the row I want?

Thanks upfront for your support!

Advertisement

Answer

What you are trying to do is not working this wat. You should either have a different form for each product wether use checkbox. Below is the code for the first case. Keep in mind name is how you call the field and value is “what you want to get from that field**.

When you use POST , you do not need something after action=”somefile.php” this is what post does, “send all variables to the file somefile.php”. Only problem with the below is that you need your tbl_sc.php after “posting” the data to redirect in the same page.

<div id="product">
    <table>
        <tr>
            <th>Product</th>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th>D</th>
            <th>E</th>
        </tr>
            <?php

                    $Id = "111";
                    $Product = "xx;";
                    $A = 1;
                    $B = 2;
                    $C = 3;
                    $D = 4;
                    $E = 5;

                    <?php
    $query = "SELECT * FROM products ORDER BY id ASC";
    $result = mysqli_query($connect, $query);
        if(mysqli_num_rows($result) > 0)
        {
        while($row = mysqli_fetch_array($result))
    {
?>

<div id="product">
    <table>
        <tr>
            <th>Product</th>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th>D</th>
            <th>E</th>
        </tr>
            <?php
                $conn = mysqli_connect("localhost", "root", "", "shop");
                // Check connection
                if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
                }
                $sql = "SELECT id, product, A, B, C, D, E FROM products";
                $result = $conn->query($sql);
                if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    $Id = $row ["id"];
                    $Product = $row ["product"];
                    $A = $row ["A"];
                    $B = $row ["B"];
                    $C = $row ["C"];
                    $D = $row ["D"];
                    $E = $row ["E"];

                    echo "<tr>";

                    echo "<td>" . $row["product"] . "</td><br />";

                    echo "<form method="post" action="tbl_sc.php">";
                    echo "<td>$A <br>";
                    echo "<input name="add_to_cart" type="hidden" value="$A">";
                    /* If you need to know also the row Id add the below and remove the ?action=add&id=$Id in action */
                    echo "<input name="id" type="hidden" value="$id">";
                    echo "<button type="submit"> Add to Cart </button>";
                    echo "</td>";
                    echo "</form>";

                    echo "<form method="post" action="tbl_sc.php">";
                    echo "<td>$B <br>";
                    echo "<input name="add_to_cart" type="hidden" value="$B">";
                    /* If you need to know also the row Id add the below and remove the ?action=add&id=$Id in action */
                    echo "<input name="id" type="hidden" value="$id">";
                    echo "<button type="submit"> Add to Cart </button>";
                    echo "</td>";
                    echo "</form>";

                    echo "<form method="post" action="tbl_sc.php">";
                    echo "<td>$C <br>";
                    echo "<input name="add_to_cart" type="hidden" value="$C">";
                    /* If you need to know also the row Id add the below and remove the ?action=add&id=$Id in action */
                    echo "<input name="id" type="hidden" value="$id">";
                      echo "<button type="submit"> Add to Cart </button>";
                    echo "</td>";
                    echo "</form>";

                    echo "<form method="post" action="tbl_sc.php">";
                    echo "<td>$D <br>";
                    echo "<input name="add_to_cart" type="hidden" value="$D">";
                    /* If you need to know also the row Id add the below and remove the ?action=add&id=$Id in action */
                    echo "<input name="id" type="hidden" value="$id">";
                      echo "<button type="submit"> Add to Cart </button>";
                    echo "</td>";
                    echo "</form>";

                    echo "<form method="post" action="tbl_sc.php">";
                    echo "<td>$E <br>";
                    echo "<input name="add_to_cart" type="hidden" value="$E">";
                    /* If you need to know also the row Id add the below and remove the ?action=add&id=$Id in action */
                    echo "<input name="id" type="hidden" value="$id">";
                      echo "<button type="submit"> Add to Cart </button>";
                    echo "</td>";
                    echo "</form>";


                    echo "</tr>";



                    echo "</tr>";
                }
                echo "</table>";
                } else { echo "0 results"; }
            ?>
    </table>
</div>
<?php
        }
    }
?>
                echo "</table>";
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement