Hi this is driving me a little crazy, have been looking everywhere for a solution for the last few days. I'm new to php and i'm trying to create a simple food shopping cart, i have a repeat region for food extras attached to the food item.

<table width="100%" border="0" cellspacing="6" cellpadding="6">
          <td width="6%"><img src="../images/foodimages/<?php echo $row_rs_fooditem['foodItemImage']; ?>" width="250" height="150" /></td>
          <td width="94%"><?php do { ?>
              <table width="200" border="0" cellspacing="0" cellpadding="0">
                  <td><input <?php if (!(strcmp($row_rs_addedextras['extraFree'],true))) {echo "checked=\"checked\"";} ?> type="checkbox" name="checkbox[<?php echo $row_rs_addedextras['idextra']; ?>]" id="checkbox" />
                  <td><?php echo $row_rs_addedextras['extraName'];?></td>
                  <td>£ <?php echo $row_rs_addedextras['extraPrice']; ?></td>
                  <td colspan="3"><input name="hextraFree[<?php echo $row_rs_addedextras['extraFree']; ?>]" type="text" id="hextraFree[]" value="<?php echo $row_rs_addedextras['extraFree']; ?>" /></td>
              <?php } while ($row_rs_addedextras = mysql_fetch_assoc($rs_addedextras)); ?>

I'm trying to insert food extras into the database provided they have been selected with the checkboxes, separate rows. I have it working fine apart from whether the extra is free or not, it justs enters 0 into each column.

Here's my code

// Added Extras Recordset ////////////////////////////
$colname_rs_addedextras = "1";
if (isset($_GET['idfooditem'])) {
  $colname_rs_addedextras = $_GET['idfooditem'];
mysql_select_db($database_dbconnect, $dbconnect);
$query_rs_addedextras = sprintf("SELECT extra.idextra, extra.extraName, extra.extraPrice, addedextras.extraFree FROM addedextras, extra WHERE fooditemID = %s AND addedextras.extraID = extra.idextra", GetSQLValueString($colname_rs_addedextras, "int"));
$rs_addedextras = mysql_query($query_rs_addedextras, $dbconnect) or die(mysql_error());
$row_rs_addedextras = mysql_fetch_assoc($rs_addedextras);
$totalRows_rs_addedextras = mysql_num_rows($rs_addedextras);
// Submit to Order Form /////////////////////////////
if (isset($_POST["addtoorderButton"]))
    mysql_query("INSERT INTO cart(cookieID, customerID, fooditemID, foodItemName, foodItemPrice) VALUES('$cookieID', '$customerID', '$fooditemID', '$foodItemName', '$foodItemPrice' ) ") or die(mysql_error());
    if ($_POST['checkbox'] != NULL)
            $cart_id = mysql_insert_id();
            print_r ($_POST['checkbox']);
            echo "<br />";
            foreach($_POST['checkbox'] as $key => $value)
                    $extraFree = $_POST['hextraFree'];
                    mysql_query("INSERT INTO cartextras(cartID, cookieID, extraID, extraFree) VALUES('$cart_id', '$cookieID', '$key', '$extraFree') ") or die(mysql_error());




Please if you have the time could you give me some guidence.

Recommended Answers

All 2 Replies

apart from whether the extra is free or not, it just enters 0 into each column.

which input element determines this? What html markup is your script generating? It would be easier to understand the problem is you post a link to your page.

Thanks for the offer of help, but I have now started with a different approach.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.