php - Can not get the id from the cart session -


i'm working on homework. have cart session , can on attribute in mysql database base on product id.

<?php $total_price=0; if(isset($_session['cart']) ? $_session['cart'] : null) { echo "<tr>     <th></th>     <th>product</th>     <th>price</th>     <th>quantity</th>     </tr>"; foreach ($_session['cart'] $key => $product) {     $the_query = "select * products id=" . $product->id;     $the_product = $db->query($the_query) or die('query failed: '.mysql_error());         $the_product->execute();         $the_product->setfetchmode(pdo::fetch_obj);          while ($row = $the_product->fetch()){         $total_price = $total_price + $row->price*$product->quantity;         echo "<tr><td>";         echo "<img src='".$row->image_url_small."' /></a></td>";         echo "<td><strong>".$row->name."</strong></td><td><em>$".$row->price."</em>";         echo '</td>';         echo '<td><input type="text" id="'.$row->id.'" class="override" value="'.$product->quantity.'"/></td>';         echo '<td><a href="do_deletecart.php?id="'.$row->id.'">delete item </a></td></tr>';     }}      echo "<tr><td colspan='2'></td></tr>";     echo "<tr><td style='text-align:center;font-size:40px;'>$</td><td><strong>total</strong><br /><em>$".$total_price."</em></td></tr>";  }  else { echo "your cart empty."; } ?> 

update can pass id do_deletecart.php. can delete product cart do_deletecart.php

<?php session_start();  $product = $_get['id'];  foreach($_session['cart'] $key=>$value) {  if($product == $value)  {      unset($_session['cart'][$key]);          break;   } }  header("location:cart.php");      ?> 

well, assuming $row->id contains expect, have enclosed quote marks, terminate <a> element's href attribute.

you need update code follows:

echo '<td><a href="do_deletecart.php?id='.$row->id.'">delete item </a></td></tr>'; 

also, might want check have started session. in order access $_session superglobal, need first have called session_start() before output sent browser.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -