php - 提交按钮不会设置

标签 php mysql html

我们不能获取删除“提交”按钮来设置,否则会起作用,我们无法理解代码的哪一部分是错误的。

想象一个有几列的表行,其中包含来自 td 中数据库的数据

// While it goes through each row (which is selected from another SQL query above, but I won't bother pasting that)
while ($row = mysql_fetch_array($result)) {
// Variable is equal to the Request ID (e.g. 10)
$numb = $row['ReqID']; 

// Echo's the table td rows with each column name in the database
echo "<tr>";
echo "<td class='req' align='center'>".$row['ReqID']."</td>";
echo "<td class='req' align='center'>".$row['ModTitle']."</td>"; 
echo "<td class='req' align='center'>".$row['BuildingID']."</td>";
echo "<td class='req' align='center'>".$row['RoomID']."</td>";
echo "<td class='req' align='center'>".$row['Priority']."</td>";
echo "<td class='req' align='center'>".$row['W1']."</td>";
echo "<td class='req' align='center'>".$row['P1']."</td>";
// Delete button also in a td row, uses the variable "$numb" to set the button name
echo "<td class='req' align='center'><input type='submit' name='{$numb}' value='Delete'></td>";
echo "</tr>";

// If the delete button is pressed, delete the row (this query works, we tested it, but the button just doesn't set so it doesn't activate the SQL command)
if (isset($_POST[$numb])) {
$result2 = mysql_query("DELETE FROM Request WHERE ReqID = {$numb} LIMIT 1", $connection);
}
}

最佳答案

使用隐藏输入来存储值:

在某处...

<form method="post" action="delete.php">

下面一点...

echo "<td class='req' align='center'><input type='submit' name='submit' value='Delete'>";
echo '<input name="hello" type="hidden" value="$numb" /></td>';
...
$number = mysql_real_escape_string($_POST["hello"]);
$result2 = mysql_query("DELETE FROM Request WHERE ReqID = ".$number." LIMIT 1", $connection);

在底部:

</form>

注意:

您的方法不够安全。我可以轻松地编辑 DOM 并为隐藏字段(或按钮的另一个名称)赋予另一个值,从而导致从数据库中删除其他元素。请记住这一点。

关于php - 提交按钮不会设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9417541/

相关文章:

php - 如何确保数据库中的产品 ID 不被篡改

php - 如何在php中获取两天之间的差异

php - 将 yii2 网站从 Windows 迁移到 Linux,链接损坏

mysql - 具有多个 where 和 order by 子句的慢查询

用户的 MySQL 数据库

php - 仪表板中未出现 wordpress 自定义主题

javascript - 尝试在 if 子句中将变量从 js 发布到 php

php - MySQL 错误 1064 使用 LIMIT 子句

javascript - 单引号内连接两个变量

html - 如何在同一 HTML 表格单元格中将中间的文本与图像垂直对齐