+6 votes
in Programming by (410 points)
retagged by
Hello below is my register code. I am very new to PHP and PDO. I would very much like to learn it so if you could be so kind to answer my question which is how do you create a login form with what I got from the code below:

Your cooperation and help will be most appreciated.
// database connection
        DB::query("INSERT INTO users(fullname,username,pass) values (?,?,?)",array($_POST["fullname"],$_POST["username"],$_POST["pass"]));
    }else {
    echo "Yay";
    <link rel="stylesheet" type="text/css" href="style2.css">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form name='registration' action="please_work.php" method="post">
     <input type="text" name="fullname" />
     <input type="text" name="username" />
     <input type="password" name="pass" />
        <input type="submit" name="submit" value="submit">

3 Answers

+3 votes
by Expert (5.9k points)
selected by
Best answer

May be this code help you to complte log in script in PDO.

$errmsg_arr = array();
$errflag = false;
// configuration
$dbhost = "localhost";
$dbname = "pdo_ret";
$dbuser = "root";
$dbpass = "";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$user = $_POST['uname'];
$password = $_POST['pword'];
if($user == '') {
$errmsg_arr[] = 'You must enter your Username';
$errflag = true;
if($password == '') {
$errmsg_arr[] = 'You must enter your Password';
$errflag = true;
// query
$result = $conn->prepare("SELECT * FROM users WHERE username= :hjhjhjh AND password= :asas");
$result->bindParam(':hjhjhjh', $user);
$result->bindParam(':asas', $password);
$rows = $result->fetch(PDO::FETCH_NUM);
if($rows > 0) {
header("location: home.php");
$errmsg_arr[] = 'Username and Password are not found';
$errflag = true;
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
header("location: index.php");
Enjoy! :)
by (410 points)
Ahhh this is the one I've been looking for thanks man
by Expert (5.9k points)
pleasure of mine dear :)
by (2.8k points)
good work this script helped me also.
+2 votes
by (720 points)

If you are asking for log in code for this So it may help you see this code: 


$user = $_POST['user']; //Set UserName
$pswrd = $_POST['pswrd']; //Set Password
if(isset($user, $pswrd)) {
    include('/config.php'); //Initiate the MySQL connection
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($user);
    $mypassword = stripslashes($pswrd);
    $myusername = mysqli_real_escape_string($dbC, $myusername);
    $mypassword = mysqli_real_escape_string($dbC, $mypassword);
    $sql="SELECT * FROM studetail WHERE name='$myusername' and pass='$mypassword'";
    $result=mysqli_query($dbC, $sql);
    // Mysql_num_row is counting table row
    // If result matched $myusername and $mypassword, table row must be 1 row
        // Register $myusername, $mypassword and redirect to file "admin.php"
        $_SESSION['name']= $myusername;
<script type="text/javascript" language="javascript">
alert("User name or password not corroct");
<?php }
In this code we are matching the username and password from database and counting result , on the basis of result we are redirecting to home page or index page. I hope this will help you.
by (410 points)
Thank you for your time answering this question although it may not be PDO because  I can see mysqli in the code but thank you any PHP knowledge will do :D
+1 vote
by Expert (5.9k points)

Hello Hafizul I am giving u a sample code change it accordingly, It may help you in PDO. 

See the code: 

    function dbConnect(){
        $username = 'root';
        $password = '';
        $conn = new pdo("mysql:host=localhost;dbname=test;",$username,$password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         return $conn;
        }   catch(PDOException $e){
            echo 'ERROR', $e->getMessage();
you can change it accordingly . i hope you got this.
by Expert (5.9k points)
@Hafizul does it answer your question?
This is not a log in script. This just shows how to connect to a database using PDO
by (410 points)
not really bro but thanks for trying :D appreciate your effort now lets code :P

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated