gpt4 book ai didi

PHP PDO 未从用户表中删除 2 个字段

转载 作者:可可西里 更新时间:2023-11-01 08:48:31 25 4
gpt4 key购买 nike

我正在尝试使用 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]);

关于PHP PDO 未从用户表中删除 2 个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20979287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com