0

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.

Community
  • 1
  • 1
Giant
  • 1,619
  • 7
  • 33
  • 67
  • are you referring to the users of the mysql, or users of rows inside the table? – Kevin Sep 25 '14 at 03:10
  • user inside table sir..what is the user in that answer referring too? – Giant Sep 25 '14 at 03:11
  • then that answer does not have a concern on your question, those are granting privileges on mysql users, not users inside that table, anyway, just set up a pre defined insert statement after the db creation is done. – Kevin Sep 25 '14 at 03:15
  • i tried it but it think there is something wrong with when i try to do change password it does not change the password can you check my entire code i will post it – Giant Sep 25 '14 at 03:16
  • Punctuation please. Hard to get through your question. – Mike Brant Sep 25 '14 at 03:25
  • @MikeBrant please check..and `i tried it but it think there is something wrong with it when i try to do change password it does not change the password can you check my entire code i will post it` – Giant Sep 25 '14 at 03:27

1 Answers1

1

When $stmt->execute() returns true it does not mean the password was updated. It just means that the query executed properly.

In order to find out you need to check how many rows the UPDATE query has affected, use 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;
}
meda
  • 45,103
  • 14
  • 92
  • 122
  • i see now i am echoing this `'Password not Changed'` now i have a problem to fix.you have any idea where i went wrong? – Giant Sep 25 '14 at 06:30
  • because the id is correct what else can possibly be wrong here? – Giant Sep 25 '14 at 06:45