簡體   English   中英

PHP 查詢更新/刪除表中動態生成的輸入字段

[英]PHP Query to UPDATE/DELETE a dynamically generated input field in a table

我有一個表,用戶可以在其中動態添加輸入字段(與 jquery 結合)來創建行。 我成功地將它插入到 mysql 數據庫中。

如果用戶想要編輯添加的現有字段,我有一個編輯頁面,其中的值是從 mysql 數據庫中獲取的,然后再次填充到動態可創建的表中。 在此處輸入圖像描述

現在有以下概率:-

  1. 用戶僅對現有值進行微小更改。 在這種情況下,必須使用更改的值更新表
  2. 用戶刪除一行/多行(隨機選擇並根據用戶的意願)。 因此,當表單提交 php 查詢時,應該只刪除數據庫中的特定行。
  3. 用戶將另一行添加到先前的現有行值,在這種情況下,php 查詢應更新先前的值並插入新添加的行值。
  4. 上述順序不必限制相同的順序。 用戶可以同時同時執行以上三個 function。

現在我的問題是(僅針對后端)我發現很難構建 php 和 sql 查詢以便更新到 mysql。

我的 php

if(isset($_POST['submit'])){
    $number1 = count($_POST['item']); //
    for($i=0; $i<$number1; $i++){
       $item = strip_tags(trim($_POST['item'][$i]));
       $description = strip_tags(trim($_POST['description'][$i]));
       $unitcost = strip_tags(trim($_POST['unitcost'][$i]));
       $qty = strip_tags(trim($_POST['qty'][$i])); // Quantity
       $sno = strip_tags(trim($_POST['sno'][$i]));// serial number

       //QUERY1 if minor updates to above variable then UPDATE (eg, qty value is changed from 3 to 4)
       //QUERY2 if row is deleted then DELETE that particular row from db (eg, sno 3 deleted from the table should DELETE corresponding mysql DB values also)
       //QUERY3 if row is added then that particular row values should be INSERT (eg, sno 4 is added which is not in the mysql db. So this has to be INSERTED.)
       }
     }

請原諒我問了這樣的問題。 由於上述查詢無法正確執行,我浪費了很多時間。 我只需要一個想法,不一定需要整個代碼。

希望你們所有人都可以就如何實施這一點給我一些建議。 我在這里先向您的幫助表示感謝。 期待一個積極的答復。

注意:再次提醒您,前端是一個動態添加/刪除輸入字段表

這聽起來像一個前端問題。 您需要定義如何告訴后端發生了什么。

<input name="items[$i][name]" />

這將顯示為在 php 中循環的漂亮數組。

foreach($_POST[items] AS $item){
    if( $item['delete'] ){
        //delete code
    }
}else{
    //Insert/Update
}

如果您想刪除某些內容,只需隱藏該字段並為其添加一個標志。

<input type="hidden" name="items[$i][delete]" value="1" />
<input type="hidden" name="items[$i][id]" />

感謝您的回復,感謝@ckrudelux 和@codefather 幫助我的意圖。 雖然他們的建議並沒有幫助我構建我的查詢。 所以我有一個很長的解決方法,並找到了以下解決方案。 我發布解決方案是因為在 UPDATE/DELETE 動態生成的輸入表方面找不到任何在線文章。

希望這對某人有幫助。

所以我所做的基本上是將所有值放入數組中。

在我動態生成的添加輸入表代碼中,我添加了一個<input type="hidden" name="sno[]" value="newrow"> 因此,這將與表單帖子結合在一起。 我使用的是普通的 html 帖子,而不是 ajax。

現在我的 submit.php 已更改為以下

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

     $productid = $_POST['productd'];// No striptag functions 
                                     // due to illustration purpose

     // First of all, we need to fetch the querying db table. 
     // This is required in order to compare the existing row values 
     // with the posted values
     $fetchproduct  = $link->prepare("SELECT * FROM product WHERE productid=?");
     $fetchproduct  ->bind_param('s',$productid);
     $fetchproduct  ->execute();
     $fetchresult   = $fetchproduct ->get_result();

     $serialnumber=array(); // Assigning array to fetch the primary key: Serial Number
     while($row = $fetchresult->fetch_assoc()){
        $serialnumber[]         = $row["sno"];
     }

     //Newly Inserted Values

     //$_POST['sno'] is taken from the dynamic input field defined earlier in this post. 
     //Basically what we are doing here is we are comparing (the values 
     //which have been posted from the primary page) and (values present in the db table). 
     //The difference will give an array of newly inserted table input field values

     $insert = array_diff($_POST['sno'],$serialnumber); 


     //Deleted Values

     // This will Difference those values in the db table and values which are 
     // deleted from the primary dynamic table page
     $delete = array_diff($serialnumber,$_POST['sno']); 
     $countdelete = count($delete);  // Counting how many values have been
                                     // lined up for deleting   

     //Updated Values

      // array_intersect will give us the common values present in both the array.
      // This means that there is no deletion or insertion to the dynamic table fields.
      $intersect = array_intersect($serialnumber, $_POST['sno']);
      $update = array_values($intersect);
      $countupdate = count($update);

     //INSERT ADDED VALUES TO DB
     foreach($insert as $key=>$ivalue){
        // ID       
        if(isset($_POST['id'][$key]) && !empty($_POST['id'][$key])) {
            $id = strip_tags(trim($_POST['id'][$key]));
        }
        // ITEM
        if(isset($_POST['item'][$key]) && !empty($_POST['item'][$key])) {
            $item = strip_tags(trim($_POST['item'][$key]));
        }
        // DESCRIPTION
        if(isset($_POST['description'][$key]) && !empty($_POST['description'][$key])) {
            $description = strip_tags(trim($_POST['description'][$key]));
        }
        // UNITCOST
        if(isset($_POST['unitcost'][$key]) && !empty($_POST['unitcost'][$key])) {
            $unitcost = strip_tags(trim($_POST['unitcost'][$key]));
        }
        // QUANTITY
        if(isset($_POST['qty'][$key]) && !empty($_POST['qty'][$key])) {
            $qty = strip_tags(trim($_POST['qty'][$key]));
        }
        // AMOUNT
        if(isset($_POST['amount'][$key]) && !empty($_POST['amount'][$key])) {
            $amount = strip_tags(trim($_POST['amount'][$key]));
        }

        // INSERT INTO THE DATABASE
        $inserttable = $link->prepare("INSERT INTO product (productid, item, description, unitcost, qty, amount) VALUES(?,?,?,?,?,?)");
        $inserttable->bind_param('ssssss', $id, $item, $description, $unitcost, $qty, $amount);
        $inserttable->execute();
        if($inserttable){
            header( 'Location:to/your/redirect page.php' ) ; // NOT MANDADTORY, You can put whatever you want
            $_SESSION['updatemsg'] = "Success";
        }
     }  

     //UPDATE EXISTING VALUES TO DB
     for($j=0; $j<$countupdate; $j++){
        // ID       
        if(isset($_POST['id'][$j]) && !empty($_POST['id'][$j])) {
            $uid = strip_tags(trim($_POST['id'][$j]));
        }
        // ITEM
        if(isset($_POST['item'][$j]) && !empty($_POST['item'][$j])) {
            $uitem = strip_tags(trim($_POST['item'][$j]));
        }
        // DESCRIPTION
        if(isset($_POST['description'][$j]) && !empty($_POST['description'][$j])) {
            $udescription = strip_tags(trim($_POST['description'][$j]));
        }
        // UNITCOST
        if(isset($_POST['unitcost'][$j]) && !empty($_POST['unitcost'][$j])) {
            $uunitcost = strip_tags(trim($_POST['unitcost'][$j]));
        }
        // QUANTITY
        if(isset($_POST['qty'][$j]) && !empty($_POST['qty'][$j])) {
            $uqty = strip_tags(trim($_POST['qty'][$j]));
        }
        // AMOUNT
        if(isset($_POST['amount'][$j]) && !empty($_POST['amount'][$j])) {
            $uamount = strip_tags(trim($_POST['amount'][$j]));
        }

        // UPDATE THE DATABASE      
        $updatetable = $link->prepare("UPDATE product SET item=?, description=?, unitcost=?, qty=?, amount=? WHERE sno=?");
        $updatetable->bind_param('ssssss', $uitem, $udescription, $uunitcost, $uqty, $uamount, $update[$j]);
        $updatetable->execute(); 
        if($updatetable){
            $_SESSION['updatemsg'] = "Success";
        }
      }

      //DELETE VALUES FROM DB
      foreach($delete as $sno){
      $deletetable = $link->prepare("DELETE FROM product WHERE sno=?");
      $deletetable->bind_param('s', $sno);
      $deletetable->execute();
      if($deletetable){
        $_SESSION['updatemsg'] = "Success";
      }
    }
   }else {
       $_SESSION['updatemsg'] = "Error";
   }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM