php - how do i change this code mysql to pdo -
this question has answer here:
- php - using pdo in clause array 3 answers
orignal php code
$sql = "select * products id in("; foreach($_session['cart'] $id => $value){ $sql .=$id. ","; } $sql=substr($sql,0,-1) . ") order id asc"; $query = mysql_query($sql); $totalprice=0; $totalqunty=0; if(!empty($query)){ while($row = mysql_fetch_array($query)){ $quantity=$_session['cart'][$row['id']]['quantity']; $subtotal= $_session['cart'][$row['id']] ['quantity']*$row['productprice']; $totalprice += $subtotal; $_session['qnty']=$totalqunty+=$quantity;
i tried this
$sql = $conn->prepare("select * products id in("); foreach($_session['cart'] $id => $value){ $sql .= $id . ","; } $sql.=substr($sql,0,-1) . ") order id asc"; $query = $conn->prepare($sql); $query->execute(); $totalprice=0; $totalqunty=0; if(!empty($query)){ while($row = $query->fetch(pdo::fetch_assoc) ){ $quantity=$_session['cart'][$row['id']]['quantity']; $subtotal= $_session['cart'][$row['id']] ['quantity']*$row['productprice']; $totalprice += $subtotal; $_session['qnty']=$totalqunty+=$quantity;
but not working, shopping cart part code.so please can tell me how can change code in pdo , what's wrong written code in pdo.
you have add parameters execute
$query->execute(array_values($_session['cart']));
however should not use superglobals directly. should filter values , take care, variables not security threat.
Comments
Post a Comment