如何使用PHP从MySql数据库中删除用户

时间:2013-06-27 14:39:37

标签: php mysql

创建了一个用户登录页面。我可以看到哪个用户登录。但我无法从数据库中删除用户。我正在使用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>

2 个答案:

答案 0 :(得分: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!";
  }
}

答案 1 :(得分:0)

尝试使用此 -

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

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