Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding to shopping cart

    Hello!

    I'm trying to add an item from products.php (which connects to a database and then displays the products) to simply display in shopping.php with <form action="shopping.php" method="post". For some reason, when I send the Product ID as a value, it only shows the last item in the list. What can I do to send the correct value for each item?

    PHP Code:

    <?php
    //Check if there is a login session
    session_start();
    if (isset(
    $_SESSION['logged']))
    $id=$_SESSION['logged'];
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Shopping Books.com</title>

        <script type = "text/javascript" src = "ShoppingBooks.js" >
        </script>
        <link href="style.css" rel="Stylesheet" type="text/css" />

    </head>
    <body>
    <center>
    <form action="shopping.php" method="post">

    <?php #line 16

    echo "<b>Books</b><br/><br/>";

    //Connection to database

    $conn mysql_connect("localhost","root");

    if (!
    $conn)

        die(
    'Could not connect' mysql_error())
       

    @
    mysql_select_db("ShoppingBooks") or die("Unable to connect");

    //Selecting all rows from Product table

    $sql mysql_query("SELECT * FROM Product");

    echo 
    "<table border='0' width = '750'>";

    echo 
    "<tr><td>Product ID</td><td>Product Name</td><td>Author</td><td>Qty</td><td>Price</td><td>Picture</td><td></td></tr>";

    //while the end of number of rows in the table.

    while($row mysql_fetch_array($sql)) {

        
    //Value of hidden field is the Product_ID of each row.

        
    echo "<tr><td><input type='hidden' name='prid' value='".$row['Product_ID']."'/>$row[Product_ID]</td>";
        echo 
    "<td>$row[Product_Name]</td>";

        echo 
    "<td>$row[Product_Detail]</td>";

        echo 
    "<td>$row[Qty]</td>";

        echo 
    "<td>$row[Price]</td>";

        echo 
    "<td>$row[Picture]</td>";

        echo 
    "<td><input type='submit' value='Add to Cart'/></td></tr>";

    }

    echo 
    "</table>";

    if (!
    $sql)

      die(
    'Error: ' mysql_error());

    mysql_close($conn);

    ?>
    </form>
    </center>
    </body>
    </html>

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    To send an array of values to a php script the name of the input element must end with [].

    try

    Code:
    name='prid[]'
    You also have a syntax error in your php code which will probably need to be fixed.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •