我是一名新的PHP程序员。我创建了一个用户登录页面,在那里可以看到登录用户的列表。我正在使用PDO连接数据库。问题是如果我想从列表中删除用户,它只删除最后插入的用户。我做错了什么?有人可以帮助我......
这是我的HTML代码:
<form action="" method="post">
<?php
foreach($rows as $row){
$time = $row['time_out'];
echo "
<input type='text' value='$row[user_name]' name='user_name'>
<input type='text' value=' $row[course]' name='course'>
<input type='text' value=' $time'>
<input type='submit' value='submit' name='submit'>
<br>";
}
?>
</form>
if(isset($_POST['submit'])){
//get course
$course = $_POST['course'];
//get user_name
$user_name = $_POST['user_name'];
//deleting user from the database
$database->delete($user_name);
//Redirect to current page
header('Location: tutor.php');
exit;
}
以下是我登录用户的方法:
public function selectFromOnline()
{
$sql = $this->connection()->prepare("SELECT * FROM online");
$sql->execute();
return $sql->fetchAll();
}
以下是删除用户的方法:
public function delete($user_name)
{
$sql = $this->connection()->prepare("DELETE FROM online WHERE user_name = :user_name");
$sql->bindValue(':user_name', $user_name, PDO::PARAM_STR);
$sql->execute();
return $sql;
}
答案 0 :(得分:0)
问题是每个输入都具有相同的名称,因此当您尝试获取POST值时,您什么也得不到。尝试不同的方法。也许您可以添加一个执行重定向操作的新字段(超链接),并使用GET发送删除操作的参数。
示例:
echo "
<input type='text' value='$row[user_name]' name='user_name'>
<input type='text' value=' $row[course]' name='course'>
<input type='text' value=' $time'>
<a href='Test.php?username=$row[user_name]&course=$row[course]'>delete</a>
<br>";
}
Test.php是您的php页面的名称。 然后,您可以使用GET值查询数据库:
$_GET["username"]
答案 1 :(得分:0)
抱歉但是我不明白你的代码,但我理解你的问题,这是我的解决方案“mysqli_query”....
<?php
//Please set these veriables according to your values....
$host_name = "YOUR_DB_HOST_NAME"; //Normally 'localhost'
$password = "YOUR_PASSWORD_FOR_MYSQL"; //your password
$username = "YOUR_USERNAME_FOR_MYSQL"; //Normally Root
$database_name = "NAME_OF_YOUR_DATABASE";
$connect = mysqli_connect($host_name, $username, $password, $database_name);
if(!$connect){
echo "Something is Wrong Please Check your host name, user name, password or database name";
}
?>
<!--YOUR FORM STARTS----------------------------------------------------->
<!--DON'T FORGET TO SET ACTION TO #(ON THE SAME PAGE)------------------------------------>
<form action="#" method="post">
<?php
foreach($rows as $row){
$time = $row['time_out'];
//Any Two Input Fields Cant have same Name So...
echo "
<input type='text' value='$row[user_name]' name='user_name_display'>
<input type='text' value=' $row[course]' name='course'>
<input type='text' value=' $time'>
<input type='hidden' value='$row[user_name]' name='user_name'>
<input type='submit' value='submit' name='submit'>
<br>";
}
?>
</form>
<!--YOUR FORM ENDS----------------------------------------------------->
<?PHP
if(isset($_POST['submit'])){
//get course
$course = $_POST['course'];
//get user_name
$user_name = $_POST['user_name'];
//Creating and running mysqli query
$delete = "DELETE FROM online WHERE user_name='$user_name'";
$query = mysqli_query($connect, $delete);
if($query){
//Code to run when user is deleted
header("Location: tutor.php")
}else{
//Error to show when can't delete user
echo "Sorry Can't delete the user";
}
}
?>
忘记一些分号和一些小错误(如果有的话),它会有效地工作.... 如果有效,请不要忘记投票....