php - 将数据从 html id 属性传输到 php 变量方法 get
问题描述
看了一堆例子。什么都没发生。我尝试转移到输入元素的 ID 值。数据库中用于后续删除记录的行标识符。但无论我怎么尝试,Get 都不会从 id 属性中获取这个值
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
$sql = "SELECT * from main";
$faq = $db_handle->runQuery($sql);
?>
<table class="one">
<thead>
<tr>
<th class="table-header" width="10%">№</th>
<th class="table-header">Дата</th>
<th class="table-header">Адрес</th>
<th class="table-header" width="10%">Наименование проекта</th>
<th class="table-header">Что изменено</th>
<th class="table-header">Кем</th>
<th class="table-header">Документ</th>
</tr>
</thead>
<tbody>
<?php
foreach($faq as $k=>$v) {
?>
<tr class="table-row">
<td><?php echo $k+1; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'destination','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["destination"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'name_change','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["name_change"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'changee','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["changee"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'date_change','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["date_change"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'moderator','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["moderator"]; ?></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'image1','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["image1"]; ?></td>
<td> <input type="submit" name="delete" value="Delete" onclick="location.href='delete.php'" id=''<?php echo $faq[$k]["id"]; ?>''></td>
</tr>
<?php
}
?>
</tbody>
</table>
都是关于这条线的
<td> <input type="submit" name="delete" value="Delete" onclick="location.href='delete.php'" id=''<?php echo $faq[$k]["id"]; ?>''></td>
删除.php
<?php
$id = $_GET['id'];
//Connect DB
//Create query based on the ID passed from you table
//query : delete where Staff_id = $id
// on success delete : redirect the page to original page using header() method
$dbname = "registration_change";
$conn = mysqli_connect("10.191.24.99", "asutp", "asutp", $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM main WHERE id = $id";
echo($sql);
if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
header('Location: main-edit.php'); //If book.php is your main page where you list your all records
exit;
} else {
echo "Error deleting record";
}
?>
解决方案
只需将 id 传递给 url :
// Notice this ---------------------------------------------------------------------v-------------------------------v
<input type="submit" name="delete" value="Delete" onclick="location.href='delete.php?id=<?php echo $faq[$k]["id"]; ?>'" id='<?php echo $faq[$k]["id"]; ?>'>
请注意,您在 中添加了多余的单引号id=''<?php echo $faq[$k]["id"]; ?>''
,我在示例中删除了它们
推荐阅读
- json - 为每个实体实现返回不同的属性是一种不好的做法吗?
- python - 使用 xlsxwriter 将 pandas 数据框写入 Excel 并包含 `write_rich_string` 格式
- selenium-chromedriver - 是否有必要在无头 chrome 模式下关闭浏览器?
- database - Laravel - 评论系统
- python - 循环卷积的哪个输出最初是正确的?FFT 卷积或 scipy 信号环绕卷积
- delphi - 如何反转 Delphi 的 TTrackbar.Orientation 值?
- javascript - 比较对象数组中的项目
- c++ - 代码编辑器实现缺少 Qt 的 QFontMetrics::horizontalAdvance()
- json - 如何按名称过滤键,然后使用 jq 访问嵌套对象
- php - Laravel Web 套接字无法在生产服务器上运行