只有html表的最后一行才会插入到数据库中

时间:2014-04-20 10:34:45

标签: php mysql

  

bill.php

 <html>
 <head>
 <title>Bill Page</title>
 <link rel="stylesheet" type="text/css" href="bill.css">
  </head>
   <body onload="start()">
         <script src="bill.js"></script>
         <form name='adminform' method='post' action="billdb.php" onload="return start()">
        <input type="text" style="position: absolute;top:100px;" value="<?php echo $tnumber ?>">           
        <div id="date"></div>
        <div id="clock"></div>
      <label id="billno">Bill No:</label>
        <label id="items">Items</label>
        <label id="quantity">Quantity</label>
        <label id="price">Price</label>
        <label id="amount">Amount</label>
        <label id="tax">Service Tax</label>
        <label id="total">Total</label>

         <?php 
         while($row=  mysqli_fetch_array($res2))
        {
        //   $title=$row['title'];
      echo "<table style>";
            echo "<tr>";
     echo "<td id='td1'>" .$row['subtitle'] ;
     echo "<input type='hidden' name='subtitle'  value= '{$row['subtitle']}'>";  
     echo "&nbsp";echo "&nbsp";echo "&nbsp";echo "&nbsp";echo "&nbsp";
     echo "&nbsp";echo "&nbsp";
     echo $row['quantity'] ;
     echo "<input type='hidden' name='quantity'  value= '{$row['quantity']}'>";  
     echo $row['price'];
    echo "<input type='hidden' name='price'  value= '{$row['price']}'>";  
$amount=$row['price'] * $row['quantity'];
echo "&nbsp";echo "&nbsp"; echo "&nbsp";echo "&nbsp";echo "&nbsp";echo "&nbsp"; echo "&nbsp";echo "&nbsp";
echo "&nbsp";echo "&nbsp"; echo "&nbsp";echo "&nbsp";
  echo "$amount.0000</td>";
  echo "<input type='hidden' name='amount'  value= '{$amount}'>";  
  $total=$total+$amount;
  $total1=$total+50.000;

    echo "<input type='hidden' name='total'  value= '{$total}'>"; 
     echo "<input type='hidden' name='tax'  value= '{50.0000}'>";  
     }
       echo "</table>";
     $billno=$row4['last_bill'];
          ?>
         <div id="bd"><?php echo $billno ?></div>    
         <input type="hidden" name="billno" value="<?php echo $billno?>">
         <div id="tamt"><?php echo "$total1.0000"?></div>
         <div id="taxamt"> 50.0000</div>
         <input type="text" name="custno" value="<?php echo $cusno?>">
          <input type="hidden" name="tnumber" value="<?php echo $tnumber?>">
         <input type="submit" id="generate" name="generate" value="Generate Bill">
        </form>
</body> 
</html>
  

billdb.php

   require_once('conn.php');
   $billno=(\filter_input(\INPUT_POST,'billno'));
   $custno=(\filter_input(\INPUT_POST,'custno'));
   $tnumber=(\filter_input(\INPUT_POST,'tnumber'));
   $subtitle=(\filter_input(\INPUT_POST,'subtitle'));
   $quantity=(\filter_input(\INPUT_POST,'quantity'));
   $price=(\filter_input(\INPUT_POST,'price'));
   $amount=(\filter_input(\INPUT_POST,'amount'));
   $tax=(\filter_input(\INPUT_POST,'tax'));
   $total=(\filter_input(\INPUT_POST,'total'));
   $sql="insert into  billmgmt(billno,custno,tnumber,subtitle,quantity,price,amount,tax,total)values('$billno','$c ustno','$tnumber','$subtitle','$quantity','$price','$amount','$tax','$total')" or          die(mysqli_error($dbhandle));
      $res=  mysqli_query($dbhandle,$sql);
      echo "success";
     mysqli_close($dbhandle);
  

我已经将mysql数据库中的内容检索到html表中,从html表中我需要将值存储在billmgmt表中。但是单独使用html表的最后一行值存储,使用此代码。请帮我解决。 ........

2 个答案:

答案 0 :(得分:1)

使用此而不是你的。

   echo "<input type='hidden' name='subtitle'  value= '".$row['subtitle']."'>";


   echo "<input type='hidden' name='quantity'  value= '".$row['quantity']."'>";

和其他输入一样,只需改变它们。

答案 1 :(得分:0)

首先,您需要通过在其名称旁边添加[]来对阵列进行HTML输入:

echo "<input type='hidden' name='subtitle[]'  value= '{$row['subtitle']}'>";
                                        #^here

对于您在表单中重复多次的所有输入,

然后在你的PHP中,你需要遍历它们:

$post = $_POST;

//single values
$billno=mysqli_real_escape_string($dbhandle,$post['billno']); 
$custno=mysqli_real_escape_string($dbhandle,$post['custno']); 
$tnumber=mysqli_real_escape_string($dbhandle,$post['tnumber']); 

//multiple values go in loop
foreach($post['subtitle'] as $key=>$val) 
{ 
$subtitle=$val; 
$quantity=mysqli_real_escape_string($dbhandle,$post['quantity'][$key]); 
$price=mysqli_real_escape_string($dbhandle,$post['price'][$key]); 
$amount=mysqli_real_escape_string($dbhandle,$post['amount'][$key]); 
$tax=mysqli_real_escape_string($dbhandle,$post['tax'][$key]); 
$total=mysqli_real_escape_string($dbhandle,$post['total'][$key]); 

//insert statement combined with single and multiple values
$sql="insert into billmgmt(billno,custno,tnumber,subtitle,quantity,price,amount,tax,total)values('$billno','$custno','$tnumber','$subtitle','$quantity','$price','$amount','$tax','$total')" or die(mysqli_error($dbhandle)); 
//added this line so you can know what's being entered, comment later
echo $sql; 
$res= mysqli_query($dbhandle,$sql); 
} 


echo mysqli_error($dbhandle); 
mysqli_close($dbhandle);

此外,花括号{}需要从此处删除,因为它是静态值:

echo "<input type='hidden' name='tax'  value= '{50.0000}'>";  #remove {}