This answer provides a user and password when database is created. I just want to know if this is the answer I am looking for.
I am creating several tables in php pdo and one table is specifically for member. In this table I want to create a limited number of users and specified already (no registration needed). I want to make it in a way that when the database is created the member table will be populated and it will not be populated again to avoid double entry.
I the answer he has user privilege it will be a good function if I have that but I am not familiar with it yet unfortunately he did not provide any explanation on how to make it. I am wondering if there are any others who can provide a tutorial or suggestion on my problem. Any suggestion is much appreciated.
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) {
}
?>
This is config.php where I create my database and table:
<?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;
}
}
?>
This is the crud file where I create the entry but the changepassword is not working. When I run it, it goes to if($stmt->execute())
and it prompts the password changed
but in the database it does not change.