【发布时间】:2022-10-04 22:34:02
【问题描述】:
我想从几个表中更新这些细节,我能知道我该怎么做吗?它似乎没有出现。
<body>
<form class=\"\" action=\"view_update_emp.php\" method=\"post\">
<label>Employee Number</label><br>
<b><?php echo $row[\'emp_num\']; ?><br></b>
<label>First Name</label><br>
<input type=\"text\" name=\"first_name\" class=\"form-control\" placeholder=\"Enter first name\" value=\"<?php echo $row[\'first_name\']; ?>\"><br>
<label>Last Name</label><br>
<input type=\"text\" name=\"last_name\" class=\"form-control\" placeholder=\"Enter last name\" value=\"<?php echo $row[\'last_name\']; ?>\"><br>
<label>Date of Birth</label><br>
<b> <?php echo $row[\'birth_date\']; ?></b><br><br>
<label>Date assigned for the job position</label><br>
<b><?php echo $row[\'date_assign\']; ?></b><br>
<label>Salary</label><br>
<input type=\"number\" name=\"emp_salary\" class=\"form-control\" placeholder=\"Enter Salary\" value=\"<?php echo $row[\'emp_salary\']; ?>\"><br>
<button type=\"submit\" class=\"btn btn-primary\" name=\"update\" value=\"Update Data\">Update</button><br>
<button type=\"submit\" class=\"btn btn-primary\" name=\"cancel\" value=\"Cancel Data\">Cancel</button><br>
</form>
</body>
<?php
$connection = mysqli_connect(\"localhost\", \"root\", \"\");
$db = mysqli_select_db($connection, \'amaz\');
if (isset($_POST[\'update\'])) {
$first_name = $_POST[\'first_name\'];
$last_name = $_POST[\'last_name\'];
$query = \"UPDATE `employee` SET first_name=\'$_POST[first_name]\',last_name=\'$_POST[last_name]\' WHERE employee.first_name=\'$_POST[id]\";
$query_run = mysqli_query($connection, $query);
if ($query_run) {
echo \"sucess\";
}
} else if (isset($_POST[\'cancel\'])) {
echo \"fail\";
}
?>
-
您的脚本对SQL Injection Attack 开放。即使是if you are escaping inputs, its not safe!,您也应该始终在
MYSQLI_或PDOAPI 中使用prepared parameterized statements,而不是将用户提供的值连接到查询中。永远不要相信任何用户输入!这也将消除未转义字符问题,例如文本字符串中的\'。 -
您需要查找如何使用隐藏的
<input type=\"hidden\" ...以允许您访问 id -
显然
first_name不会等于 ID 值....这没有任何意义。而且您的表单甚至不包含要提交的 ID 值...请参阅上面 RiggsFolly 的评论。 -
不过,您的实际问题尚不清楚……除了代码中显示的表之外,您还需要更新哪些其他表?你这样做有什么问题,究竟是什么?
-
顺便说一句,
if (isset($_POST[\'cancel\'])) { echo \"fail\";没有逻辑意义。用户取消某事不是系统故障。