簡體   English   中英

如何從PHP中的很多表中刪除1個選定的行(來自mysql)?

[英]How can I delete 1 selected row from a lot of tables in PHP (from mysql)?

我有一個PHP函數,從MYSQL寫出我的表。 我做了一個按鈕,刪除選中的行(名為torles)。

如何刪除我的'torles'按鈕,我按下它的行?

function munkalapok(){
  kapcsolat();  //connect to mysql, and write out all rows

  $sql="SELECT * FROM munkalap ORDER BY id DESC";
  $vissza=mysql_query($sql);
  mysql_close(kapcsolat()); //close mysql

  print "<div class='datagrid'><center>";
  print "<table border='1' align='center'>";
  print "<thead><th>Módosítás</th><th>Munkalapszám</th></thead>";

  $i='1'; //for count rows
  while ($sor = mysql_fetch_array($vissza)) {
    print "<tr><tbody>";
    print "<form method='POST'>".
          "<input type='submit' value='$i. Törlés' name='torles'>".
          "</form>" . "</td>";
    //Create the button
    if(isset($_POST['torles'])){
      kapcsolat(); //open mysql
      $parancs="DELETE FROM munkalap WHERE munkalapszam = '$munka'";                 
      mysql_query ($parancs);
      mysql_close (kapcsolat());
      header("Location: ./Munkalapok.php");
    }
    print "<td>" . $sor['munkalapszam'] . "</td>";
    print "</tbody></tr>";
    $i++;
  }
  print "</table>";
  print "</center></div>";
}

什么是$munka ,你是怎么得到這個的......可能應該是id ...

所以你可以在你的表單中有一個隱藏的字段,並將其作為行id(無論你有什么)

試試:

print "<tr><tbody>";

print "<form method='POST'><input type='hidden' name='id' value='$sor[munkalapszam]'><input type='submit' value='$i. Törlés' name='torles'></form>" . "</td>";
    //Create the button
if(isset($_POST['torles'])){

kapcsolat(); //open mysql
$munka=(int) $_POST['id'];  //i did this for id
$parancs="DELETE FROM munkalap WHERE munkalapszam = '$munka'";                 
mysql_query ($parancs);
mysql_close (kapcsolat());

這可能對你有所幫助

在您的代碼中,您執行以下操作:

connect to SQL, get rows and print them
while printing
  show a button
  if this button is pressed, delete a row and redirect
end while

這是你的第一個錯誤。 相反,您的代碼邏輯應該如下所示:

if there is posted data
  delete the data
connect to SQL, get rows and print

您的主要問題(您要問的問題)是您創建按鈕,但無法確定按下按鈕的行。
您可以添加另一個隱藏的輸入,而不是在按鈕的值中添加$ i變量:

print '<input type="hidden" name="row_id" value="'.$i.'"/>';

這樣,您就可以找到按下按鈕的行:

if (isset($_POST['torles'])) {
  $id = (int) $_POST['row_id'];
  // delete where id = $id (assuming that $i is an id)
}

您是否使用ID來識別數據庫中的記錄? 您應該使用自動增量功能將ID與每條記錄相關聯。 那將是你的主鍵。 然后使用記錄ID來標識表行,並且不要使用該$ i變量。

您應該查看數據庫的正常表單。 http://www.1keydata.com/database-normalization/first-normal-form-1nf.php

我希望你的數據庫中的每個元素都有一個id。 並且id是主鍵。 說,修改

print "<td>" . $sor['munkalapszam'] . "</td>";

print "<td>" . $sor['munkalapszam'] . "<span id='".$sor['id']."' >X</span></td>";

這樣,表中的每一行都可以唯一標識.X是刪除符號。

下一步是使用JS來處理click並使用ajax將id發送到服務器以刪除該行。

$( "td" ).click(function() {
   //write the ajax code here to send the id to the server.
});

這不是最安全的方法。 但這會給你一個關於如何開始的基本想法。

你可以在這里閱讀jQuery ajax: http//api.jquery.com/jQuery.ajax/ jQuery click事件: http//api.jquery.com/click/

在后端你需要一個名為delete.php或soemthing的文件。在這個文件中使用id刪除一行的邏輯。通過ajax將id作為參數傳遞給它。

暫無
暫無

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

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