0

创建了一个用户登录页面。我可以在哪里看到哪个用户已登录。但我无法从数据库中删除用户。我正在使用 PDO 连接数据库。这是我删除用户的方法:

 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;
    }

这是我的 HTML 代码:

if(isset($_POST['submit'])){
$user_name = isset($_POST['user_name']);
    print_r($user_name);
    //call delete method to delete the user from database
$database->delete($user_name);
}

<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]' >
        <input type='text' value=' $time '>
        <input type='hidden' value='$row[user_name]' name='user_name'>
        <input type='submit' value='delete' name='submit'><br>";
    }
    ?>
</form>
4

2 回答 2

2

$user_name是一个布尔值,因为您将其设置为isset()

也许您打算执行以下操作:

if(isset($_POST['submit'])){
  $user_name = isset($_POST['user_name']) ? $_POST['user_name'] : false;
  if($user_name) {
    print_r($user_name);
    //call delete method to delete the user from database
    $database->delete($user_name);
  } else {
    echo "No user name found!";
  }
}
于 2013-06-27T14:44:33.397 回答
0

尝试使用这个 -

if (isset($_POST['user_name'])) {
    $user_name = $_POST['user_name'];
    $database->delete($user_name);
}

你应该学习如何使用isset() - http://php.net/manual/en/function.isset.php

于 2013-06-27T15:11:11.220 回答