+6 votes
9.7k views
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.
 
 
<?php
// database connection
require_once("DB.php");
DB::setup("pggmb","root","");
$error=array();
session_start();
 
 
if(isset($_POST["fullname"],$_POST["username"],$_POST["pass"])){
    if($_POST["fullname"]&&$_POST["pass"]){
 
        DB::query("INSERT INTO users(fullname,username,pass) values (?,?,?)",array($_POST["fullname"],$_POST["username"],$_POST["pass"]));
    }else {
 
    }
    echo "Yay";
}
 
?>
 
<html>
    <head>
    <link rel="stylesheet" type="text/css" href="style2.css">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <body>
    <center><h1>Register</h2></center>
 
<form name='registration' action="please_work.php" method="post">
     <label>Fullname:</label><br>
     <input type="text" name="fullname" />
     <br>
     <label>Username:</label><br>
     <input type="text" name="username" />
     <br>
      <label>Password:</label><br>
     <input type="password" name="pass" />
     <br>
        <input type="submit" name="submit" value="submit">
 </form>
 </body>
</html>
 
closed

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.

<?php
session_start();
$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);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
if($rows > 0) {
header("location: home.php");
}
else{
$errmsg_arr[] = 'Username and Password are not found';
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: index.php");
exit();
}
 
?>
Enjoy! :)
0
by (410 points)
Ahhh this is the one I've been looking for thanks man
0
by Expert (5.9k points)
pleasure of mine dear :)
0
by (2.9k 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: 

 

<?php
error_reporting(0);
$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
    $count=mysqli_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
        // Register $myusername, $mypassword and redirect to file "admin.php"
        session_register("hi");
        session_register("password");
        $_SESSION['name']= $myusername;
        header("location:home.php");
}
else
{?>
<script type="text/javascript" language="javascript">
alert("User name or password not corroct");
window.location.href='index.php';
</script>
<?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.
0
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: 

   <?php
    function dbConnect(){
       try{
        $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.
0
by Expert (5.9k points)
@Hafizul does it answer your question?
0
by
This is not a log in script. This just shows how to connect to a database using PDO
0
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