我正在尝试使用pdo从表中删除用户名和密码。下面是我正在使用的代码。它插得很好,其他的都做得很完美。这是我从网上得到的剧本。我能找到的最正派的。但是我对php pdo很陌生,需要一些帮助来从表中删除用户名和密码。

<?php

function dbconnect()
{
global $pdo;

try {
    $pdo = new PDO('mysql:host=localhost;dbname=redgrace_staxapp', 'root', '');
} catch (PDOException $e) {
    die('MySQL connection fail! ' . $e->getMessage());
}
}

function insert_new_user($username, $password)
{
# checking username is already taken
if (username_exists($username))
return FALSE;

# insert new user info
global $pdo;
$stmt = $pdo->prepare('
     INSERT INTO users
    (username, password)
    values (:username, :password)');

$stmt->execute( array(':username' => $username, ':password' => md5($password)) );

if ($pdo->lastInsertId())
     return true;
else
    return false;
}

function delete_user($username, $password)
{

if (username_exists($username))
     return FALSE;


global $pdo;
$stmt   = "DELETE FROM users WHERE username = :username and password = :password";


$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':password', $password, PDO::PARAM_STR);
$stmt->execute();



   }

   function username_exists($username)
   {

global $pdo;

$stmt = $pdo->prepare('
    SELECT id
    FROM users
     WHERE username = :username
    LIMIT 1');

$stmt->execute( array('username' => $username) );
return $stmt->fetchColumn();
}

function attempt($username, $password)
{
global $pdo;

$stmt = $pdo->prepare('
    SELECT id, username
    FROM users
    WHERE username = :username AND password = :password
    LIMIT 1');

$stmt->execute(array(':username' => $username, 'password' => md5($password)));

if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
    # set session
    $_SESSION['username'] = $data->username;
    return true;
} else {
    return false;
}
}

function is_user()
{
if (isset($_SESSION['username']))
    return true;
}

function redirect($url)
{
header('Location: ' .$url);
exit;
}

function valid_username($str){
return preg_match('/^[a-z0-9_-]{3,16}$/', $str);
}

function valid_password($str){
return preg_match('/^[a-z0-9_-]{6,18}$/', $str);
}
?>

如果有人能帮我就好了。

最佳答案

$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);

一次执行一个(或者使用数组并执行)
http://us3.php.net/manual/en/pdostatement.bindparam.php
http://us3.php.net/manual/en/pdostatement.execute.php
$stmt->execute(['username'=>$username, 'password'=>$password]);

10-08 01:47