This answer在创建数据库时提供用户和密码。我只是想知道这是否是我要找的答案。
我在php pdo中创建了几个表,一个表专门用于成员。在此表中,我想创建有限数量的用户并已指定(无需注册)。我想以这样的方式创建,即在创建数据库时,将填充成员表,并且不会再次填充它以避免双重输入。
我得到用户权限的答案如果我有这个功能它将是一个很好的功能,但我不熟悉它但不幸的是他没有提供任何解释如何制作它。我想知道是否有其他人可以就我的问题提供指导或建议。任何建议都非常感谢。
更新:
<?php
global $dbh;
$dbname = 'memberdb';
$member = "member";
try {
$dbh = new PDO("mysql:host=localhost", "root", "");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbname = "`".str_replace("`","``",$dbname)."`";
$dbh->query("CREATE DATABASE IF NOT EXISTS $dbname");
$dbh->query("use $dbname");
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql ="CREATE TABLE IF NOT EXISTS $member (
mem_id int(40) NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(40) NOT NULL,
password VARCHAR(40) NOT NULL);" ;
$dbh->exec($sql);
} catch(PDOException $e) {
}
?>
这是config.php,我在其中创建了我的数据库和表:
<?php
include('config.php');
session_start();
$stmt = $dbh->prepare("SELECT * FROM member") ;
$stmt->execute();
$count = $stmt -> rowCount();
if( $count == 00 ){
$stmt = $dbh->prepare("INSERT INTO member (username, password) VALUES (?,?)")or die($db->errorInfo());
$stmt->bindValue(1,"admin1",PDO::PARAM_STR);
$stmt->bindValue(2,"password1",PDO::PARAM_STR);
$stmt->execute();
$stmt->bindValue(1,"admin2",PDO::PARAM_STR);
$stmt->bindValue(2,"password2",PDO::PARAM_STR);
$stmt->execute();
$stmt->bindValue(1,"admin3",PDO::PARAM_STR);
$stmt->bindValue(2,"password3",PDO::PARAM_STR);
$stmt->execute();
}
function ChangePassword() {
global $dbh;
$oldpass = trim($_POST['oldpass']);
$newpass = trim($_POST['newpass']);
$cpass = trim($_POST['cpass']);
$user = $_SESSION['login_user'];
if(!empty($_POST['oldpass'])){
$oldpass = trim($_POST['oldpass']);
$newpass = trim($_POST['newpass']);
$cpass = trim($_POST['cpass']);
$user = $_SESSION['login_user'];
$stmt = $dbh->prepare("SELECT * FROM member WHERE username = ? ") ;
$stmt->bindValue(1,$user);
$stmt->execute();
$selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $selected_row['mem_id'];
echo $cpass;
if($selected_row['password']===$oldpass){
if($newpass===$cpass){
$oldpass = trim($_POST['oldpass']);
$newpass = trim($_POST['newpass']);
$cpass = trim($_POST['cpass']);
$user = $_SESSION['login_user'];
$stmt = $dbh->prepare("UPDATE member SET password=:NEWPASSWORD WHERE mem_id=:ID");
$selected_row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->bindValue(':ID',$selected_row['mem_id'],PDO::PARAM_STR);
$stmt->bindValue(':NEWPASSWORD',$cpass,PDO::PARAM_STR);
if($stmt->execute()){
echo "<script type='text/javascript'>alert('Password Changed');
</script>";
exit;
}else{
echo "<script type='text/javascript'>alert('Password not Changed');
window.location='dashboard.php';
</script>";
exit;
}
}else{
echo "<script type='text/javascript'>alert('Password new password and confirm password does not match');
window.location='dashboard.php';
</script>";
exit;
}
}else{
echo "<script type='text/javascript'>alert('Wrong old password');
window.location='dashboard.php';
</script>";
exit;
}
}else{
echo "<script type='text/javascript'>alert('Empty old password');
window.location='dashboard.php';
</script>";
exit;
}
}
?>
这是我创建条目的crud文件,但是changepassword不起作用。当我运行它时,它会转到if($stmt->execute())
并提示password changed
,但在数据库中它不会改变。
答案 0 :(得分:1)
当$stmt->execute()
返回true时,并不表示密码已更新。它只是意味着查询正确执行。
为了找出您需要检查UPDATE
查询影响了多少行,请使用rowCount
:
if($stmt->execute()){
if($stmt -> rowCount() >0){
echo "<script type='text/javascript'>alert('Password Changed');</script>";
exit;
}else{
echo "<script type='text/javascript'>alert('Password not Changed');
window.location='dashboard.php';
</script>";
exit;
}
}else{
echo "<script type='text/javascript'>alert('Error Occured');</script>";
var_dump($stmt->error());
exit;
}