客户信息已成功保存在表 1 中,但不将客户产品信息保存在表 2 中

  • 本文关键字:客户 保存 存在 信息 成功 php
  • 更新时间 :
  • 英文 :


当我保存客户的信息(姓名、地址、报价日期等(时,它成功地将数据保存在名为"tbl_order"的表格中,但产品名称、价格、数量等的数据没有保存在名为"tbl_order_item"的表格中。奇怪的是,代码在我的本地主机(生产(上运行并完美运行,但是当我将代码上传到实际站点时,它无法正常运行。

注意:我还没有清理代码(重构(。一旦它正常工作,我就会这样做。

提前感谢您的帮助!

下面是我的代码:

    <?php session_start(); ?>
<?php
  if (!isset($_SESSION['username']))
  {
    header("Location: index.php");
  }
?>
<?php
  include('./includes/database_connection_main.php');
  $statement = $connect->prepare("
    SELECT * FROM tbl_order
    ORDER BY order_id DESC
  ");
  $statement->execute();
  $all_result = $statement->fetchAll();
  $total_rows = $statement->rowCount();
  if(isset($_POST["create_invoice"]))
  {
    $order_total_before_tax = 0;
    $order_total_tax1 = 0;
    $order_total_tax = 0;
    $order_total_after_tax = 0;
    $statement = $connect->prepare("
      INSERT INTO tbl_order
        (quote_date, lead_time, good_until, order_receiver_name, company_name, address_line_1, city, state, zip_code, phone_number, email, note_1, note_2, note_3, note_4, order_total_before_tax, order_total_tax1, order_total_tax, order_total_after_tax, order_datetime)
        VALUES (:quote_date, :lead_time, :good_until, :order_receiver_name, :company_name, :address_line_1, :city, :state, :zip_code, :phone_number, :email, :note_1, :note_2, :note_3, :note_4, :order_total_before_tax, :order_total_tax1, :order_total_tax, :order_total_after_tax, :order_datetime)
    ");
    $statement->execute(
      array(
          ':quote_date'                   =>  trim($_POST["quote_date"]),
          ':lead_time'                    =>  trim($_POST["lead_time"]),
          ':good_until'                   =>  trim($_POST["good_until"]),
          ':order_receiver_name'          =>  trim($_POST["order_receiver_name"]),
          ':company_name'                 =>  trim($_POST["company_name"]),
          ':address_line_1'               =>  trim($_POST["address_line_1"]),
          ':city'                         =>  trim($_POST["city"]),
          ':state'                        =>  trim($_POST["state"]),
          ':zip_code'                     =>  trim($_POST["zip_code"]),
          ':phone_number'                 =>  trim($_POST["phone_number"]),
          ':email'                        =>  trim($_POST["email"]),
          ':note_1'                       =>  trim($_POST["note_1"]),
          ':note_2'                       =>  trim($_POST["note_2"]),
          ':note_3'                       =>  trim($_POST["note_3"]),
          ':note_4'                       =>  trim($_POST["note_4"]),
          ':order_total_before_tax'       =>  $order_total_before_tax,
          ':order_total_tax1'             =>  $order_total_tax1,
          ':order_total_tax'              =>  $order_total_tax,
          ':order_total_after_tax'        =>  $order_total_after_tax,
          ':order_datetime'               =>  date("Y-m-d")
      )
    );
      $statement = $connect->query("SELECT LAST_INSERT_ID()");
      $order_id = $statement->fetchColumn(); //use this to view the quote number on the pdf document
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
        $order_total_before_tax = $order_total_before_tax + floatval(trim($_POST["order_item_actual_amount"][$count]));
        $order_total_tax1 = $order_total_tax1 + floatval(trim($_POST["order_item_tax1_amount"][$count]));
        $order_total_after_tax = $order_total_after_tax + floatval(trim($_POST["order_item_final_amount"][$count]));
        $statement = $connect->prepare("
          INSERT INTO tbl_order_item
          (order_id, sku, item_name, order_item_quantity, order_item_price, order_item_actual_amount, order_item_tax1_rate, order_item_tax1_amount, order_item_final_amount)
          VALUES (:order_id, :sku, :item_name, :order_item_quantity, :order_item_price, :order_item_actual_amount, :order_item_tax1_rate, :order_item_tax1_amount, :order_item_final_amount)
        ");
        $statement->execute(
          array(
            ':order_id'                   =>  $order_id,
            ':sku'                        =>  trim($_POST["sku"][$count]),
            ':item_name'                  =>  trim($_POST["item_name"][$count]),
            ':order_item_quantity'        =>  trim($_POST["order_item_quantity"][$count]),
            ':order_item_price'           =>  trim($_POST["order_item_price"][$count]),
            ':order_item_actual_amount'   =>  trim($_POST["order_item_actual_amount"][$count]),
            ':order_item_tax1_rate'       =>  trim($_POST["order_item_tax1_rate"][$count]),
            ':order_item_tax1_amount'     =>  trim($_POST["order_item_tax1_amount"][$count]),
            ':order_item_final_amount'    =>  trim($_POST["order_item_final_amount"][$count])
          )
        );
      }
      $order_total_tax = $order_total_tax1;
      $statement = $connect->prepare("
        UPDATE tbl_order
        SET order_total_before_tax = :order_total_before_tax,
        order_total_tax1 = :order_total_tax1,
        order_total_tax = :order_total_tax,
        order_total_after_tax = :order_total_after_tax
        WHERE order_id = :order_id
      ");
      $statement->execute(
        array(
          ':order_total_before_tax'     =>  $order_total_before_tax,
          ':order_total_tax1'           =>  $order_total_tax1,
          ':order_total_tax'            =>  $order_total_tax,
          ':order_total_after_tax'      =>  $order_total_after_tax,
          ':order_id'                   =>  $order_id
        )
      );
      header("location:invoice_list.php");
  }
  if(isset($_POST["update_invoice"]))
  {
    $order_total_before_tax = 0;
      $order_total_tax1 = 0;
      $order_total_tax = 0;
      $order_total_after_tax = 0;
      $order_id = $_POST["order_id"];

      $statement = $connect->prepare("
                DELETE FROM tbl_order_item WHERE order_id = :order_id
            ");
            $statement->execute(
                array(
                    ':order_id'       =>      $order_id
                )
            );
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
        $order_total_before_tax = $order_total_before_tax + floatval(trim($_POST["order_item_actual_amount"][$count]));
        $order_total_tax1       = $order_total_tax1 + floatval(trim($_POST["order_item_tax1_amount"][$count]));
        $order_total_after_tax  = $order_total_after_tax + floatval(trim($_POST["order_item_final_amount"][$count]));
        $statement = $connect->prepare("
          INSERT INTO tbl_order_item
          (order_id, sku, item_name, order_item_quantity, order_item_price, order_item_actual_amount, order_item_tax1_rate, order_item_tax1_amount, order_item_final_amount)
          VALUES (:order_id, :sku, :item_name, :order_item_quantity, :order_item_price, :order_item_actual_amount, :order_item_tax1_rate, :order_item_tax1_amount, :order_item_final_amount)
        ");
        $statement->execute(
          array(
            ':order_id'                 =>  $order_id,
            ':sku'                      =>  trim($_POST["sku"][$count]),
            ':item_name'                =>  trim($_POST["item_name"][$count]),
            ':order_item_quantity'      =>  trim($_POST["order_item_quantity"][$count]),
            ':order_item_price'         =>  trim($_POST["order_item_price"][$count]),
            ':order_item_actual_amount' =>  trim($_POST["order_item_actual_amount"][$count]),
            ':order_item_tax1_rate'     =>  trim($_POST["order_item_tax1_rate"][$count]),
            ':order_item_tax1_amount'   =>  trim($_POST["order_item_tax1_amount"][$count]),
            ':order_item_final_amount'  =>  trim($_POST["order_item_final_amount"][$count])
          )
        );
        $result = $statement->fetchAll();
      }
      $order_total_tax = $order_total_tax1;
      $statement = $connect->prepare("
        UPDATE tbl_order
        SET order_no = :order_no,
        order_date = :order_date,
        order_receiver_name = :order_receiver_name,
        order_receiver_address = :order_receiver_address,
        order_total_before_tax = :order_total_before_tax,
        order_total_tax1 = :order_total_tax1,
        order_total_tax = :order_total_tax,
        order_total_after_tax = :order_total_after_tax
        WHERE order_id = :order_id
      ");
      $statement->execute(
        array(
          ':order_no'                 =>  trim($_POST["order_no"]),
          ':order_date'               =>  trim($_POST["order_date"]),
          ':order_receiver_name'      =>  trim($_POST["order_receiver_name"]),
          ':order_receiver_address'   =>  trim($_POST["order_receiver_address"]),
          ':order_total_before_tax'   =>  $order_total_before_tax,
          ':order_total_tax1'         =>  $order_total_tax1,
          ':order_total_tax'          =>  $order_total_tax,
          ':order_total_after_tax'    =>  $order_total_after_tax,
          ':order_id'                 =>  $order_id
        )
      );
      $result = $statement->fetchAll();
      header("location:invoice_list.php");
  }
  if(isset($_GET["delete"]) && isset($_GET["id"]))
  {
    $statement = $connect->prepare("DELETE FROM tbl_order WHERE order_id = :id");
    $statement->execute(
      array(
        ':id'       =>      $_GET["id"]
      )
    );
    $statement = $connect->prepare(
      "DELETE FROM tbl_order_item WHERE order_id = :id");
    $statement->execute(
      array(
        ':id'       =>      $_GET["id"]
      )
    );
    header("location:invoice_list.php");
  }
  ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="robots" content="noindex, nofollow">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.dataTables.min.js"></script>
    <script src="js/dataTables.bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css"><!--search pagination and delete reference link-->
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script><!--search pagination and delete reference link-->
  </head>
  <body>
    <div class="container-fluid">
      <?php
      if(isset($_GET["add"]))
      {
      ?>
      <form method="post" id="create_form">
        <div class="table-responsive">
          <table class="table table-bordered">
            <tr>
              <td colspan="2" align="">
                <div><?php echo "Logged in as:" . " " . $_SESSION['username']; ?></div>
                <div align=""><li style="list-style-type: none;"><a href="invoice_list.php">Dashboard</a></li></div>
                <div align=""><li style="list-style-type: none;"><a href="./includes/logout.php">Log Out</a></li></div>
              </td>
            </tr>
            <tr>
              <td colspan="2" align="center"><img src="images/dpb_logo.png" alt="DBP Logo" height="" width=""></td>
            </tr>
            <tr>
                <td colspan="2">
                  <div class="row">
                    <div class="col-md-8">
                        <b>RECEIVER (BILL TO)</b>
                        <input type="text" name="order_receiver_name" id="order_receiver_name" class="form-control input-sm" placeholder="customer name" />
                        <input type="text" name="company_name" id="company_name" class="form-control input-sm" placeholder="company name" />
                        <input type="text" name="address_line_1" id="address_line_1" class="form-control" placeholder="address line 1" />
                        <input type="text" name="city" id="city" class="form-control" placeholder="city" />
                        <input type="text" name="state" id="states" class="form-control" placeholder="state" />
                        <input type="text" name="zip_code" id="zip_code" class="form-control" placeholder="zip code" />
                        <input type="text" name="phone_number" id="phone_number" class="form-control" placeholder="phone number" />
                        <input type="text" name="email" id="email" class="form-control" placeholder="email" />
                    </div>
                    <div class="col-md-4">
                      <label for=""><b>Quote Date:</b></label>
                      <input type="date" name="quote_date" id="quote_date" class="form-control input-sm" />
                      <br>
                      <label for=""><b>Lead Time:</b></label>
                      <input type="text" name="lead_time" id="lead_time" class="form-control input-sm" placeholder="Enter Lead Time" />
                      <br>
                      <label for=""><b>Good Until:</b></label>
                      <input type="date" name="good_until" id="good_until" class="form-control input-sm" />
                    </div>
                  </div>
                  <br />
                  <table id="invoice-item-table" class="table table-bordered">
                    <tr>
                      <th width="1%">Items</th>
                      <th width="4%">SKU</th>
                      <th width="40%">Item Description</th>
                      <th width="1%">Quantity</th>
                      <th width="7%">Price</th>
                      <th width="1%">Subtotal</th>
                      <th width="3%" colspan="2">Tax Percentage</th>
                      <th width="3%">Row Total</th>
                    </tr>
                    <tr>
                      <th></th>
                      <th></th>
                      <th></th>
                      <th></th>
                      <th></th>
                      <th></th>
                      <th>Tax%</th>
                      <th>Rate</th>
                      <th></th>
                    </tr>
                    <tr>
                      <td><span id="sr_no">1</span></td>
                      <td><input type="text" name="sku[]" id="sku" class="form-control input-sm" /></td>
                      <td><input type="text" name="item_name[]" id="item_name1" class="form-control input-sm" /></td>
                      <td><input type="text" name="order_item_quantity[]" id="order_item_quantity1" data-srno="1" class="form-control input-sm order_item_quantity" /></td>
                      <td><input type="text" name="order_item_price[]" id="order_item_price1" data-srno="1" class="form-control input-sm number_only order_item_price" /></td>
                      <td><input type="text" name="order_item_actual_amount[]" id="order_item_actual_amount1" data-srno="1" class="form-control input-sm order_item_actual_amount" readonly /></td>
                      <td><input type="text" name="order_item_tax1_rate[]" id="order_item_tax1_rate1" data-srno="1" class="form-control input-sm number_only order_item_tax1_rate" /></td>
                      <td><input type="text" name="order_item_tax1_amount[]" id="order_item_tax1_amount1" data-srno="1" readonly class="form-control input-sm order_item_tax1_amount" /></td>
                      <td><input type="text" name="order_item_final_amount[]" id="order_item_final_amount1" data-srno="1" readonly class="form-control input-sm order_item_final_amount" /></td>
                    </tr>
                  </table>
                  <div align="right">
                    <button type="button" name="add_row" id="add_row" class="btn btn-success btn-xs">+</button>
                  </div>
                </td>
              </tr>
              <tr>
                <td align="right"><b>Total</td>
                <td align="right"><b><span id="final_total_amt"></span></b></td>
              </tr>

                <tr>
                  <td colspan="2">
                    <div align="center"><label for="additional_information"><b><u>Additional Information</u></b></label></div>
                    <input type="text" name="note_1" id="note_1" class="form-control input-sm" placeholder="Enter note 1" />
                    <input type="text" name="note_2" id="note_2" class="form-control input-sm" placeholder="Enter note 2" />
                    <input type="text" name="note_3" id="note_3" class="form-control input-sm" placeholder="Enter note 3" />
                    <input type="text" name="note_4" id="note_4" class="form-control input-sm" placeholder="Enter note 4" />
                  </td>

                  <td></td>
                </tr>
              <tr>
                <td colspan="2" align="center">
                  <input type="hidden" name="total_item" id="total_item" value="1" />
                  <input type="submit" name="create_invoice" id="create_invoice" class="btn btn-info" value="Save" />
                </td>
              </tr>
          </table>
        </div>
      </form>
      <script>
      $(document).ready(function(){
        var final_total_amt = $('#final_total_amt').text();
        var count = 1;
        $(document).on('click', '#add_row', function(){
          count++;
          $('#total_item').val(count);
          var html_code = '';
          html_code += '<tr id="row_id_'+count+'">';
          html_code += '<td><span id="sr_no">'+count+'</span></td>';
          html_code += '<td><input type="text" name="sku[]" id="sku'+count+'" class="form-control input-sm" /></td>';
          html_code += '<td><input type="text" name="item_name[]" id="item_name'+count+'" class="form-control input-sm" /></td>';
          html_code += '<td><input type="text" name="order_item_quantity[]" id="order_item_quantity'+count+'" data-srno="'+count+'" class="form-control input-sm number_only order_item_quantity" /></td>';
          html_code += '<td><input type="text" name="order_item_price[]" id="order_item_price'+count+'" data-srno="'+count+'" class="form-control input-sm number_only order_item_price" /></td>';
          html_code += '<td><input type="text" name="order_item_actual_amount[]" id="order_item_actual_amount'+count+'" data-srno="'+count+'" class="form-control input-sm order_item_actual_amount" readonly /></td>';
          html_code += '<td><input type="text" name="order_item_tax1_rate[]" id="order_item_tax1_rate'+count+'" data-srno="'+count+'" class="form-control input-sm number_only order_item_tax1_rate" /></td>';
          html_code += '<td><input type="text" name="order_item_tax1_amount[]" id="order_item_tax1_amount'+count+'" data-srno="'+count+'" readonly class="form-control input-sm order_item_tax1_amount" /></td>';
          html_code += '<td><input type="text" name="order_item_final_amount[]" id="order_item_final_amount'+count+'" data-srno="'+count+'" readonly class="form-control input-sm order_item_final_amount" /></td>';
          html_code += '<td width="1%"><button type="button" name="remove_row" id="'+count+'" class="btn btn-danger btn-xs remove_row">X</button></td>';
          html_code += '</tr>';
          $('#invoice-item-table').append(html_code);
        });
        $(document).on('click', '.remove_row', function(){
          var row_id = $(this).attr("id");
          var total_item_amount = $('#order_item_final_amount'+row_id).val();
          var final_amount = $('#final_total_amt').text();
          var result_amount = parseFloat(final_amount) - parseFloat(total_item_amount);
          $('#final_total_amt').text(result_amount);
          $('#row_id_'+row_id).remove();
          count--;
          $('#total_item').val(count);
        });
        function cal_final_total(count)
        {
          var final_item_total = 0;
          for(j=1; j<=count; j++)
          {
            var quantity = 0;
            var price = 0;
            var actual_amount = 0;
            var tax1_rate = 0;
            var tax1_amount = 0;
            var item_total = 0;
            quantity = $('#order_item_quantity'+j).val();
            if(quantity > 0)
            {
              price = $('#order_item_price'+j).val();
              if(price > 0)
              {
                actual_amount = parseFloat(quantity) * parseFloat(price);
                $('#order_item_actual_amount'+j).val(actual_amount);
                tax1_rate = $('#order_item_tax1_rate'+j).val();
                if(tax1_rate > 0)
                {
                  tax1_amount = parseFloat(actual_amount)*parseFloat(tax1_rate)/100;
                  $('#order_item_tax1_amount'+j).val(tax1_amount);
                }
                item_total = parseFloat(actual_amount) + parseFloat(tax1_amount);
                final_item_total = parseFloat(final_item_total) + parseFloat(item_total);
                $('#order_item_final_amount'+j).val(item_total);
              }
            }
          }
          $('#final_total_amt').text(final_item_total);
        }
        $(document).on('blur', '.order_item_price', function(){
          cal_final_total(count);
        });
        $(document).on('blur', '.order_item_tax1_rate', function(){
          cal_final_total(count);
        });
        $('#create_invoice').click(function(){
          if($.trim($('#order_receiver_name').val()).length == 0)
          {
            alert("Please Enter Receiver Name");
            return false;
          }
          for(var no=1; no<=count; no++)
          {
            if($.trim($('#item_name'+no).val()).length == 0)
            {
              alert("Please Enter Item Name");
              $('#item_name'+no).focus();
              return false;
            }
            if($.trim($('#order_item_quantity'+no).val()).length == 0)
            {
              alert("Please Enter Quantity");
              $('#order_item_quantity'+no).focus();
              return false;
            }
            if($.trim($('#order_item_price'+no).val()).length == 0)
            {
              alert("Please Enter Price");
              $('#order_item_price'+no).focus();
              return false;
            }
          }
          $('#create_form').submit();
        });
      });
      </script>
      <?php
      }
    ?>
<script>//check if the value that we anter on the price and quantity fields are numbers
$(document).ready(function(){
$('.number_only').keypress(function(e){
return isNumbers(e, this);
});
function isNumbers(evt, element)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
if (
(charCode != 46 || $(element).val().indexOf('.') != -1) &&      // “.” CHECK DOT, AND ONLY ONE.
(charCode < 48 || charCode > 57))
return false;
return true;
}
});
</script>

我找到了我所遇到的问题的答案。由于某种原因,数据库要求使用提交表单中的值输入 $order_total_before_tax、order_total_tax1、order_total_tax、order_total_tax order_total_after_tax 字段。由于有时客户不会对其报价单征税,因此不会输入税字段,从而导致数据库不会在名为"tbl_order_item"的第二个表上插入信息。我会寻找解决这个问题的方法。

最新更新