php中遇到的非数字值

时间:2017-05-21 18:51:43

标签: php

我想在文本字段中获取数量并将其与总价格相乘,但我总是得到以下错误,有人可以帮助我吗?

  

警告:遇到非数字值

<table class="table">
   <tr align="center">
        <th>Remove</th>
        <th>Product(S)</th>
        <th>Quantity</th>
        <th>Total Price</th>
   </tr>
   <?php 
       $total = 0;
       global $con; 
       $ip = getIp();
       $sel_price = "SELECT * FROM Cart WHERE IP_Address='$ip'";
       $pricerows = $con->query($sel_price)->fetchall(PDO::FETCH_ASSOC);
       foreach ($con->query($sel_price) as $row_price) {
            $pro_id = $row_price['Product_ID'];
            $pro_price = "SELECT * FROM Products WHERE Product_ID='$pro_id'";
            foreach ($con->query($pro_price) as $row_proprice) {
                 $product_price = array($row_proprice['Price']);
                 $product_title = $row_proprice['Product_Name'];
                 $product_imaage = $row_proprice['Photo'];
                 $single_price = $row_proprice['Price']; 
                 $values = array_sum($product_price);
                 $total += $values;
    ?>  
 <tr align="center">
   <td><input type="checkbox" name="remove[]" value="<?php echo $pro_id; ?>"></td>
   <td><?php echo $product_title; ?><br><img src="images/<?php echo $product_imaage; ?>" width="60" height="60"/></td>  
   <td><input type="text" size="4" id="qty" name="qty" value="<?php echo $_SESSION['qty'];?>"/></td>
<?php

  if (isset($_POST['update_cart'])) {

    $qty = $_POST['qty'];
    $update_qty = "UPDATE cart set Quantity='$qty'";
    $updaterows = $con->prepare($update_qty);
    $updaterows->execute();

    $_SESSION['qty'] = $qty;

    $total = $total*$qty;   //ERROR APPEARS HERE

 }
?>
<td><?php echo $single_price . " L.E";?></td>
</tr>
</table>

2 个答案:

答案 0 :(得分:0)

请不要将未经过滤的$ _POST变量插入到sql查询中,总是使用过滤器来阻止sql注入!

据我所知,您试图将$total(未初始化)与$qty相乘。

请在此计算之前的某处插入$total = 0(或相同的初始化)!

答案 1 :(得分:0)

您需要将值转换为整数,您可以使用

int(your value) 

$total = $total* int($qty); 

我希望这能解决问题。