简体   繁体   English

PHP 每行删除按钮/更改该行表中的值

[英]PHP delete button for every row / change value in table for this row

I want there is a delete button in every row in table and when click the button for example in row 1 then the row 1 deleted.我希望表中的每一行都有一个删除按钮,例如当单击第 1 行中的按钮时,删除第 1 行。

and EXTRA if you know how instead of delete, when I press the button a value in the table changes.如果你知道如何而不是删除,那么当我按下按钮时,表中的值会发生变化。 For example when click delete btn then value "OK" in table change to "DELETED".例如,当单击删除 btn 时,表中的值“OK”更改为“DELETED”。

The code work right for the specific id that put in delete.php file.该代码适用于 delete.php 文件中的特定 ID。

I'm not interested in database security I just want to find a way to make it work.我对数据库安全性不感兴趣,我只是想找到一种让它工作的方法。

index.php index.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "userform";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
$result = $conn->query($sql);



if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo '
<table>
<tr>
<td>Fullname</td>
<td>', $row['name'] . $row['lname'], '</td>
</tr>
<br>
<tr>
<td>Email address</td>
<td>', $row['email'], '</td>
</tr>
<br>
<tr>
<td>Reservation ID</td>
<td>', $row['rescode'], '</td>
</tr>
<br>
<tr>
<td>Check-in</td>
<td>', $row['checkin'], '</td>
</tr>
<br>
<tr>
<td>Check-out</td>
<td>', $row['checkout'], '</td>
</tr>
<br>
<tr>
<td>Mobile number</td>
<td>', $row['mnumber'], '</td>
</tr>
<br>
<tr>
<td>Total price</td>
<td>', $row['totalprice'], '</td>

<td><a href="delete.php">Cancel</a></td>
</tr>

</table>';
  }
} else {
  echo "0 results";
}
$conn->close();
?>

<script>

delete.php删除.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "userform";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
$result = $conn->query($sql);


$sql = "DELETE FROM usertable WHERE id='10'";


if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
  } else {
    echo "Error deleting record: " . $conn->error;
  }

$conn->close();
?>

try this: in index.php,试试这个:在 index.php 中,

  • add 'id' in sql.在 sql 中添加“id”。 $sql = "SELECT id, name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable"; $sql = "SELECT id, name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
  • than pass the id in cancel link, ie href="delete.php?id=".$row['id']比在取消链接中传递 id,即 href="delete.php?id=".$row['id']

in delete.php, add this line $id= $GET['id'];在 delete.php 中,添加这一行 $id= $GET['id']; and pass $id to delete query, $sql = "DELETE FROM usertable WHERE id=$id";并通过 $id 删除查询, $sql = "DELETE FROM usertable WHERE id=$id";

Query询问

This query:这个查询:

$sql = "SELECT name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
$result = $conn->query($sql);

should be应该

$sql = "SELECT id, name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
$result = $conn->query($sql);

Delete link删除链接

This html code此 html 代码

<td><a href="delete.php">Cancel</a></td>

should be应该

<td><a href="delete.php?id=' .  $row['id'] .  '">Cancel</a></td>

Please, ... check your code: you concat strings using "," instead of ".".请...检查您的代码:您使用“,”而不是“。”来连接字符串。

Finally最后

This SQL code此 SQL 代码

$sql = "DELETE FROM usertable WHERE id='10'";

should be应该

$sql = "DELETE FROM usertable WHERE id='" . $_GET['id'] . "'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM