如何修复添加功能?

问题描述 投票:-1回答:1

我正在做一个基于php的项目。我想将数据添加到数据库。我使用php,PDO做到了。我用OOC创建了代码。但是在主HTML页面中有一个if else部分。在那部分中,else部分正在运行。但我甚至没有点击插入按钮。当页面加载时,else部分正在运行。我观看了一段视频,这段代码工作得很好。这是代码..

Insert.php - Getters,Setters,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> 
php html
1个回答
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,那么在任何其他可能存在echo'oops ......的情况下,echo“inserted ...”

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即做或做 - 但你要说的是 - 如果a - 做b或c,否则什么都不做。我认为您需要尝试准确理解条件的工作原理

© www.soinside.com 2019 - 2024. All rights reserved.