字母输入值未存储在数据库中

时间:2018-11-05 07:56:46

标签: javascript php mysql

我有一个脚本,可以在多个记录中添加产品,价格,数量,产品宽度,产品高度之后创建每一行的总计。当我在包括产品名称在内的所有输入字段中输入数值时,查询将运行并输入记录,但是当我输入字母产品名称时,查询将不会运行。这个脚本是怎么回事。请在这方面指导我。

<!DOCTYPE html>
<html>
<head>
    <title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form method="POST" action="">
<table>
  <thead>
    <th>Product</th>
    <th>Price</th>
    <th>Quantity</th>
    <th>Width</th>
    <th>Height</th>
    <th>Total</th>
  </thead>

  <tbody id="product_table">
    <tr>
        <td><input type="text" name="product[]"></td>
        <td><input type="text" name="price[]"></td>
        <td><input type="text" name="quantity[]"></td>
        <td><input type="text" name="width[]" value="0"></td>
        <td><input type="text" name="height[]" value="0"></td>
        <td><input type="text" name="total[]" class="totalPrice" readonly></td>
    </tr>
    <tr>
        <td><input type="text" name="product[]"></td>
        <td><input type="text" name="price[]"></td>
        <td><input type="text" name="quantity[]"></td>
        <td><input type="text" name="width[]" value="0"></td>
        <td><input type="text" name="height[]" value="0"></td>
        <td><input type="text" name="total[]" class="totalPrice" readonly></td>
    </tr>
  </tbody>
<button name="send">Submit</button>
</table>
</form>
</body>
<?php
include('database.php');

if (isset($_POST['send'])) {
$product = $_POST['product'];
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$width = $_POST['width'];
$height = $_POST['height'];
$total = $_POST['total'];
$invoice_number = 1;

for($i=0; $i<count($_POST['total']); $i++) {
        if($i <> count($_POST['total'])) {

  $sql = ("INSERT INTO invoice_order(invoice_number, product, price, quantity, width, height, total) 
      VALUES (".$invoice_number.",".$_POST['product'][$i].",".$_POST['price'][$i].",".$_POST['quantity'][$i].",".$_POST['width'][$i].",".$_POST['height'][$i].",".$_POST['total'][$i].")");

      $query = mysqli_query($connect, $sql);  
    }}
  if ($query) {
          echo "Record inserted Successfully";
       }else{
          echo "Unable to insert Record";
       }}?>

<script>
const table = document.getElementById('product_table');
table.addEventListener('input', ({ target }) => {
  const tr = target.closest('tr');
  const [product, price, quantity, width, height, total] = tr.querySelectorAll('input');

  var size = width.value * height.value;
  var rate = price.value * quantity.value;
  var nameproduct = product.value;
  if (size != "") {
    total.value = size * rate;
  }else{
    total.value = rate; 
  }
  totalPrice();
});
</script>
<style>
    table,tr,td,th { border: 1px black solid;}
</style>
</html>

0 个答案:

没有答案