Php 如何修复Add函数?

Php 如何修复Add函数?,php,html,Php,Html,我正在做一个基于php的项目。我想把数据添加到数据库中。我用php,PDO做的。我用OOC创建了代码。但是在主HTML页面中有一个if-else部分。在该部分中,其他部分正在运行。但我甚至没有点击“插入”按钮。当页面加载时,else部分正在运行。我看了一段视频,这段代码运行得非常好。这是密码 php-getter、setter、SQL查询等 class Stock { protected $ItemNo; protected $ItemName;

我正在做一个基于php的项目。我想把数据添加到数据库中。我用php,PDO做的。我用OOC创建了代码。但是在主HTML页面中有一个if-else部分。在该部分中,其他部分正在运行。但我甚至没有点击“插入”按钮。当页面加载时,else部分正在运行。我看了一段视频,这段代码运行得非常好。这是密码

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

    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;
    }
从数据库中获取品牌和项目名称。品牌根据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>')
             })
           })
         })
        })

$(文档).ready(函数(){
$(“#ItemName”).change(函数(){
var-aid=$(“#ItemName”).val();
$.ajax({
url:'GetBrand.php',
方法:“post”,
数据:“aid=”+aid
}).完成(功能(品牌){
控制台日志(品牌);
brand=JSON.parse(brand);
$('品牌').empty();
brand.forEach(功能(bnamee){
$(“#品牌”)。附加(“”+bnamee.brand+“”)
})
})
})
})
形式

<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> 

项目编号
项目名称
选择名称
添加项目名称:
添加
品牌名称
精选品牌
或者更适合你的是

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-dobc,否则什么也不做。我认为您需要尝试并准确理解条件句是如何工作的

请添加最少的代码@ZainFarooq say's请看好,我会编辑this@GaganiSenarathne你到底想在这里做什么?这段代码很混乱,太多了。你做了基本的调试吗?有错误吗?实际上我不知道怎么说。。我是php新手。。不,我没有做任何调试。我想说我的代码中有一个问题,我不知道如何解决。。我花了三天时间。。但这并没有解决问题。问题是,当运行else部分的AddItem.php运行时,还有else部分(我也对其进行了注释)。。
if(isset($_GET['insert']) {

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