This答案在創建數據庫時提供用戶和密碼。我只想知道這是否是我正在尋找的答案。在創建表時創建成員表中的條目並且不會再創建php pdo
我在php pdo中創建了幾個表格,並且一個表格是專門用於成員的。在這張表中,我想創建有限數量的用戶並已經指定(不需要註冊)。我想這樣做,當數據庫創建時,成員表將被填充,並且不會再次填充以避免重複輸入。
我有他的用戶權限的答案,如果我有這個功能,它將是一個很好的功能,但我不熟悉它,但不幸的是,他沒有提供任何解釋如何做到這一點。我想知道是否有其他人可以提供關於我的問題的教程或建議。任何建議,非常感謝。
UPDATE:
<?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;
}
}
?>
這是我創建條目渣滓文件,但changepassword不工作。當我運行它時,它會跳轉到if($stmt->execute())
並提示password changed
,但在數據庫中它不會更改。
你指到MySQL的用戶,或在表中的行的用戶? – Ghost 2014-09-25 03:10:16
表內的用戶先生。該答案中的用戶也指的是什麼? – Giant 2014-09-25 03:11:34
那麼這個答案對你的問題沒有任何顧慮,那些對mysql用戶授予特權,而不是對該表內的用戶授予特權,無論如何,只要在數據庫創建完成後設置一個預先定義的插入語句。 – Ghost 2014-09-25 03:15:06