如何修复添加功能?

时间:2019-04-26 07:31:15

标签: php html

我正在做一个基于php的项目,并且我想将数据添加到数据库中,我是使用php,PDO完成的。我用OOC创建了代码。但是,HTML主页面中还有一个if部分。在那部分中,其他部分正在运行。但是我什至没有点击插入按钮。页面加载时,else部分正在运行。我观看了一个视频,此代码运行正常。这是代码。.

Insert.php-getter,setter,sql查询等。

    class Stock
    {
        protected $ItemNo;
        protected $ItemName;
        protected $brand;
        protected $qty;
        protected $Description;
        protected $ItemDate;
        protected $SupplierName;
        protected $SupplierMail;
        private $tableName = 'StockDetails';
        private $dbconn;

        function setItemNo($ItemNo) { $this->ItemNo = $ItemNo; }
        function getItemNo() { return $this->ItemNo; }
        function setItemName($ItemName) { $this->ItemName = $ItemName; }
        function getItemName() { return $this->ItemName; }
        function setBrand($brand) { $this->brand = $brand; }
        function getBrand() { return $this->brand; }
        function setQty($qty) { $this->qty = $qty; }
        function getQty() { return $this->qty; }
        function setDescription($Description) { $this->Description = $Description; }
        function getDescription() { return $this->Description; }
        function setItemDate($ItemDate) { $this->ItemDate = $ItemDate; }
        function getItemDate() { return $this->ItemDate; }
        function setSupplierName($SupplierName) { $this->SupplierName = $SupplierName; }
        function getSupplierName() { return $this->SupplierName; }
        function setSupplierMail($SupplierMail) { $this->SupplierMail = $SupplierMail; }
        function getSupplierMail() { return $this->SupplierMail; }

        public function __construct()
        {
            require_once ('Database.php');
            $db = new Database();
            $this->dbconn = $db->connect();

        }
        public function insert(){

            $sql = "INSERT INTO $this->tableName VALUES (:ItemNo, :ItemName, :brand, :qty, :Description, :ItemDate, :SupplierName, :SupplierMail)";
            $stmt = $this->dbconn->prepare($sql);

            $stmt->bindParam(':ItemNo', $this->ItemNo);
            $stmt->bindParam(':ItemName', $this->ItemName);
            $stmt->bindParam(':brand', $this->brand);
            $stmt->bindParam(':qty', $this->qty);
            $stmt->bindParam(':Description', $this->Description);
            $stmt->bindParam(':ItemDate', $this->ItemDate);
            $stmt->bindParam(':SupplierName', $this->SupplierName);
            $stmt->bindParam(':SupplierMail', $this->SupplierMail);

            if($stmt->execute()){

                return true;

            }else{

                return false;
            }}}  
?>

Action.php-对AddItem.php进行表单操作

    require_once ('Insert.php');

    class action{

        function __construct(){

            switch ($_POST['submit']) {
                case 'insert':
                    $obInsert = new Stock;

                    $obInsert->setItemNo($_POST['ItemNo']);
                    $obInsert->setItemName($_POST['ItemName']);
                    $obInsert->setBrand($_POST['brand']);
                    $obInsert->setQty($_POST['qty']);
                    $obInsert->setDescription($_POST['Description']);
                    $obInsert->setItemDate(date('Y-m-d H:i:s'));
                    $obInsert->setSupplierName($_POST['SupplierName']);
                    $obInsert->setSupplierMail($_POST['SupplierMail']);

                    if($obInsert->insert()) {

                        header('location: AddItem.php?insert=1');

                    } else{

                        header('location: AddItem.php?insert=0');
                    }

                    break;      
                default:
                    header('location: AddItem.php');
                    break;
            }
        }
    }

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

        $object = new action;
    }

AddItem.php

从数据库中获取品牌和ItemName。品牌会根据ItemName更改。

<?php
require_once ('GetBrand.php');
$ItemName = LoadItemName();

?>

   $(document).ready(function(){
        $("#ItemName").change(function(){
          var aid = $("#ItemName").val();
          $.ajax({
             url: 'GetBrand.php',
             method: 'post',
             data: 'aid=' + aid
           }).done(function(brand){
             console.log(brand);
             brand = JSON.parse(brand);
             $('#brand').empty();
             brand.forEach(function(bnamee){
             $('#brand').append('<option>' + bnamee.brand + '</option>')
             })
           })
         })
        })

表格

<form name="form0" method="post" action="Action.php">
    <div class="form-group">
         <label for="text">Item No</label>
         <input type="text" name="ItemNo" id="ItemNo" placeholder="Item No" required>
     </div>
     <form name="form1" action="AddItem.php" method="post">
          <div class="form-group">
              <label for="text">Item Name</label>
              <table>
                 <tr>
                    <td><select name="ItemName" id="ItemName" required>
                    <option value="" disabled="" selected>Select Name</option>
                    <?php foreach($ItemName as $iname)
                        echo "<option id='".$iname['ItemNo']."' value='".$iname['ItemNo']."'>".$iname['ItemName']."</option>";
                    ?>
                  </select></td>
                  <td><label for="text">Add Item Name : </label></td>
                  <td><input type="text" name="name" id="name"> </td>
                  <td><button>Add</button></td>
                </tr>
             </table>
         </div>
         <div class="form-group">
             <label for="text">Brand Name</label>
             <table>
                <tr>
                   <td><select name="brand" id="brand" required>
                   <option value="">Select Brand</option>
                   </select></td>
                   <td<label for="text">Add Brand : </label></td>
                   <td><input type="text" name="Bname" id="Bname"  class="form-control" > </td>
                   <td><button>Add</button></td>
               </tr>
           </table>
        </div>
     </form>
     <div class="form-group">
        <label for="text">Quantity</label>
        <input type="text" name="qty" id="qty" placeholder="Quantity" required>
     </div>
     <div class="form-group">
        <label for="text">Item Description</label>
        <input type="text" name="Description" id="Description" placeholder="Description" required>
      </div>
      <div class="form-group">
          <label for="text">Date</label>
          <input type="date" name="ItemDate" id="ItemDate" required>
      </div>
      <div class="form-group">
          <label for="text">Supplier Name</label>
          <input type="text" name="SupplierName" id="SupplierName"  placeholder="Supplier Name" required>
      </div>
      <div class="form-group">
          <label for="text">Supplier Mail</label>
          <input type="text" name="SupplierMail" id="SupplierMail" placeholder="Supplier Mail" required>
       </div><br/>
       <div class="form-group">
           <button id="submit" name="submit" value="insert">Insert</button>
           <?php 
              if(isset($_GET['insert']) && ($_GET['insert'] == 1)){
                echo "Inserted Successfully ";
              }else // This else part is running when the page is loading 
                echo "Ooops..! Something went wrong.";
              }
           ?>
       </div>
     </form> 

1 个答案:

答案 0 :(得分:1)

      <?php 
          if(isset($_GET['insert']) && ($_GET['insert'] == 1)){
            echo "Inserted Successfully ";
          }else // This else part is running when the page is loading 
            echo "Ooops..! Something went wrong.";
          }
       ?>

您对此运行感到惊讶吗?

您在这里说的是,如果有一个名为insert的$ _GET参数,它等于1,则在可能存在的任何其他情况下回显“插入...”

Php无法自然地分辨出这是初始页面加载,重定向,按下按钮还是您的想法。如果只希望在单击按钮后才能运行该程序,则需要对整个操作进行一些额外的检查,例如

if(isset($_POST['submit'])) {
///add in your checks to
}

或更可能更适合您的是

if(isset($_GET['insert']) {

          if($_GET['insert'] == 1)){
            echo "Inserted Successfully ";
          } else {// you're missing a brace here 
            echo "Ooops..! Something went wrong.";
          }
}

关键是,您的代码将始终运行,因为它是一个if / else,即执行此操作执行该操作-但是您想说的是是i fa -执行 b c 不执行其他操作。我认为您需要尝试并确切了解条件的工作原理