0

created a user login page. Where I can see which user is logged in. But I can’t delete user from database. I am using PDO for connecting database. Here is my method for deleting user:

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

here is my HTML code:

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>
1
  • 2
    Maybe you should get familiar how isset() works : php.net/manual/en/function.isset.php ... "Returns TRUE if var exists and has value other than NULL, FALSE otherwise." . Passing a variable to a function return, applies the returned value to the variable $var = function($var) in this case if function returns false/true, $var will become either false or true, but not the value you want Commented Jun 27, 2013 at 14:47

2 Answers 2

2

$user_name is a boolean because you're setting it to the result of isset()

Perhaps you meant to do something like:

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!";
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try using this -

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

And you should study how to use isset()- http://php.net/manual/en/function.isset.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.