SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Having problem getting the data from checkboxes and putting them into a temp cart

    Hi All,
    I'm a newbie programmer that just started learning PHP. I would really appreciate if you can help me out with this issue that I have for about 2 days and couldn't solve it.

    Here is the code that I am using to retrieve data from the database and show it as a nice clean table with a check box beside each item:

    Code PHP:
    <?php
     
    $con = mysql_connect("database", "usename", "password");
     
    if (!$con) {
    die('Connection Failed: ' . mysql_error());
    }
     
     if (mysql_select_db("database",$con)){
       if ($qry= mysql_query("select * from INVENTORY")){
            $num=mysql_numrows($qry);
            mysql_close();
    }
    }
     
    ?>
    <html>
    <body>
    <form method="POST">
    <table border="1" cellspacing="2" cellpadding="2">
    <tr>
    <th>Item</th>
    <th>Description</th>
    <th>Price</th>
    <th>Picture</th>
    <th>Selection</th>
    </tr>
     
     
    <?php
    while ($row=mysql_fetch_array($qry)) {
     
    $f1=$row['Item_Image'];
    $f2=$row['Inventory_ID'];
    echo "<tr><td>";
     
    echo $row['Item_Name'];
     
    echo "</td><td>";
     
    echo $row['Item_Details'];
     
    echo "</td><td>";
     
    echo $row['Item_Price'];
     
    echo "</td><td>";
    ?>
    <img src="http://zenit.senecac.on.ca/~php701_121sa03/assign2/<?php echo $f1; ?>" alt= "<?php echo $f1; ?>"  width="100" height="100" />
     
    <td> <input type="checkbox" name= <?php echo $f2; ?>  /></td>
     
    <?php
    echo "</td></tr>";
    }
    echo "</table>";
     
    ?>
    </form>
    <form action="cart.php" method="POST">
    <input type="submit" name="submit" value="Add to the Cart">
    </form>
     
    </body>
    </html>


    So What I wanna do is that when I press submit button, it takes the items that is checked and put them in a temporary cart
    this is the page that is called if I press submit buttom, i make a temporary cart and I am trying to input the values that is set to the checkboxes into the temp cart but I have no idea how to do it ...

    I tried to use cookie to save the values that selected but couldn't sort that out , I need help ...

    Code PHP:
    <?php
     
    $con = mysql_connect("database", "username", "password");
     
    if (!$con) {
     
    die('Connection Failed: ' . mysql_error());
     
    }
     
    $userid = $_COOKIE['loguser'];
     
    $myquery= ("CREATE TABLE " . $userid . "_CART (Cart_Item_ID int auto_increment, PRIMARY KEY(Cart_Item_ID), Item_ID int not null, FOREIGN KEY(Item_ID) REFERENCES INVENTORY(Inventory_ID), Added_On Timestamp default CURRENT_TIMESTAMP)");
     
    $userid = $_COOKIE['loguser'];
     
    if (isset($_POST['submit'])){
     
     if (mysql_select_db("database",$con)) {
     
            if ($qry = mysql_query($myquery,$con)) {
     
                    $myquery1= ("insert into ". $userid . "_CART(Item_ID) select Inventory_ID from INVENTORY where INVENTORY.Inventory_ID = ". $_POST;
     
            } else {
     
                    die('Database Query Error: ' . mysql_error());
     
            }
     
      }
     
    }
     
    ?>


    here is my database tables one is inventory, and the other one is temporary cart , Im trying to input the value of inventory_ID into the Item_ID where the check boxes is selected ....

    Inventory_id is foreign key of temp cart and primary key of Inventory table

    +--------------+-----------+------+-----+-------------------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +--------------+-----------+------+-----+-------------------+----------------+
    | Cart_Item_ID | int(11) | NO | PRI | NULL | auto_increment |
    | Item_ID | int(11) | NO | MUL | | |
    | Added_On | timestamp | YES | | CURRENT_TIMESTAMP | |
    +--------------+-----------+------+-----+-------------------+----------------+
    3 rows in set (0.00 sec)

    mysql> desc INVENTORY;
    +--------------+----------------+------+-----+-------------------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +--------------+----------------+------+-----+-------------------+----------------+
    | Inventory_ID | int(11) | NO | PRI | NULL | auto_increment |
    | Item_Name | varchar(40) | NO | | | |
    | Item_Details | varchar(250) | YES | | NULL | |
    | Item_Image | varchar(25) | YES | | noIMG.jpg | |
    | Item_Price | float unsigned | YES | | 0 | |
    | Added_On | timestamp | YES | | CURRENT_TIMESTAMP | |
    +--------------+----------------+------+-----+-------------------+----------------+
    6 rows in set (0.00 sec)

    Please advise me what to do ?
    If only you can tell me how to get the checkbox values and show it in the cart page as an echo it will solve lots of my issues as well...

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Put this in a file and run it maybe it will help you understand how to identify which checkboxes were checked.

    PHP Code:
    <?php
    if(isset($_POST))
        
    var_dump$_POST )

    ?>

    <form name=form0 method=post action ="">
    <input type=checkbox name=checkbox[0] />
    <input type=checkbox name=checkbox[1] />
    <input type=checkbox name=checkbox[2] />
    <input type = submit>
    </form>


Bookmarks

Posting Permissions

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