gpt4 book ai didi

php - 试图从数据库中删除一个成员

转载 作者:行者123 更新时间:2023-11-29 01:56:37 25 4
gpt4 key购买 nike

我在尝试从我正在使用的数据库中删除成员时遇到了一些问题,我认为它没有正确获取用户名。这是我用于 HTML 的表单

deleteForm.php

<?php
//begin our session
session_start();
?>
<html>
<head>
<title>Welcome</title>
</head>
<form action="deleteUser.php">
<p>
<center><label for="Username">Enter username to delete</center></label>
<center><input type="text" id="Username" name="Username" value="" maxlength="20" /></center>
<center><input type="submit" value="Delete Member"></center>
</p>
</form>
</body>
</html>

这是处理删除本身的代码:

删除用户.php

<?php
//begin our session
session_start();

//Check if username, password have been sent
if((!filter_input(INPUT_POST, 'Username')))
{
echo 'Please enter a valid username';
}
else
{
//Enter the valid data into the database
$memberUsername = filter_input(INPUT_POST, 'Username', FILTER_SANITIZE_STRING);

echo $memberUsername;

$SQLhostname = "****";
$SQLusername = "****";
$SQLpassword = "****";
$databaseName = "****";

try
{
echo "in the try block";
// Create connection
$conn = mysqli_connect($SQLhostname, $SQLusername, $SQLpassword)
or die("Unable to connect MySQL");

$db_selected = mysqli_select_db($conn, $databaseName)
or die("Could not select database");

$deleteMember = "DELETE FROM customers
WHERE name =
'$memberUsername'";

$result = $conn->query($deleteMember);

if(! $result ){
die('Could not delete member: ' . $conn->error);}
else{
echo "Member deleted <br/>";
}

mysqli_close($conn);
}
catch (Exception $ex)
{
//To be added

}
}
?>

问题是它总是进入 if 语句并要求输入有效的用户名,我假设没有设置。

最佳答案

method 属性添加到您的 form

<form action="deleteUser.php" method="post">
<!--^^^^^^^^^^-->
<p>
<center><label for="Username">Enter username to delete</center></label>
<center><input type="text" id="Username" name="Username" value="" maxlength="20" /></center>
<center><input type="submit" value="Delete Member"></center>
</p>

关于php - 试图从数据库中删除一个成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193594/

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