gpt4 book ai didi

PHP : Data which is of one digit is not deleted, 但两位数数据被删除

转载 作者:行者123 更新时间:2023-11-29 07:48:07 26 4
gpt4 key购买 nike

我有 2 个 PHP 页面用于从表中删除员工数据。为此,用户插入员工 ID,然后按删除键,从表中删除数据。

现在,问题是,每当我插入一位数字的 id(2、3、8 等)时,id 都不会被删除。但是,如果插入两位数字 ID(12、19、99 等),它将被删除。请帮我解决我哪里错了。

这是我的第一个 PHP 页面的代码:

<form action="deleteemp.php" method="post" onSubmit="return confirm('Are you sure to delete?')">
Enter id to delete data<input type="text" name="EmpId" required>
<button type="submit" >Delete</button>
</form>

这是我的 PHP 操作页面,

<?php

$EmpId = $_POST['EmpId'];

$connection = mysql_connect("localhost", "root", "");
if (!$connection) {
die("Connection failed " . mysql_error());
}
$db_conn = mysql_select_db("hms", $connection);
if (!$db_conn) {
die("Connection failed " . mysql_error());
}
$query = "DELETE FROM employee_details WHERE emp_id = " . $EmpId;
$db_result = mysql_query($query, $connection);

if ($db_result) {
echo "Data Deleted Successfully !";
echo "<br>";
echo "<a href='homepage.php'>Back to homepage</a>";
} else {
echo "Data Not there. Try Again !<br>";
echo "<a href='deleteemp1.php'>Search again</a>";
}

最佳答案

echo“数据不在这里”不正确。 mysql_query 在失败时返回 bool 值 false。空结果(没有匹配的 ID)不是失败。这是一个成功的查询,但恰好有一个空结果集。

你的代码应该更像

$result = mysql_query($query) or die(mysql_error());
if (mysql_affected_rows($result) == 0) {
die("No rows deleted");
}

请注意,您很容易受到 sql injection attacks 的攻击,并使用过时/已弃用的数据库库。

关于PHP : Data which is of one digit is not deleted, 但两位数数据被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27173037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com