0

我是一个新的 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;
    }
4

2 回答 2

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"]
于 2013-06-29T03:50:48.090 回答
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";
}
}
?>

只是忘记一些分号和一些小错误(如果有的话),它会绝对有效....如果有效,请不要忘记投票....

于 2013-06-29T03:51:33.637 回答