hello sir

hi friends
i am learning php from head first php book
i have written a login script code from book that is similar to it
but when i submit the user info
it display again this login script.
but when i run this book’s code it works fine.
the both script are similar.
the following is my code and next to it is book’s.
My code:-

<?php session_start(); $errormsg=""; if(!isset($_SESSION['user_id'])) { if(isset($_POST['submit'])) { $dbc=mysqli_connect('localhost','root','','mismatch') or die('Error connecting database'); $uname=mysqli_real_escape_string($dbc,trim($_POST['user_name'])); $pword=mysqli_real_escape_string($dbc,trim($_POST['pass_word'])); if((!empty($uname))&&(!empty($pword))) { $query="SELECT * FROM 'mismatch'.'mismatch_user' WHERE username='$uname' AND password=SHA('$pword')"; $result=mysqli_query($dbc,$query) or die('Error quering database'); if(mysqli_num_rows($result) == 1) { $row=mysqli_fetch_array($result); $_SESSION['user_id']=$row['user_id']; $_SESSION['username']=$row['username']; echo'You have successfully logged in as '.$_SESSION['username'].'.
'; echo'Edit your profile'; mysqli_close($dbc); } else { $errormsg='Sorry try different user name.'; } } else { $errormsg='Please enter all the information to log in.'; } } } ?> Log in

Welcome to Mismatch.com

Please enter your log in info to access your page.

<?php if(empty($_SESSION['user_id'])) { echo $errormsg; ?> Log in Username:

Password:

<?php

}
?>

and here is book’s code

<?php // Start the session session_start(); // Clear the error message $error_msg = ""; // If the user isn't logged in, try to log them in if (!isset($_SESSION['user_id'])) { if (isset($_POST['submit'])) { // Connect to the database $dbc = mysqli_connect('localhost','root','','mismatch'); // Grab the user-entered log-in data $user_username = mysqli_real_escape_string($dbc, trim($_POST['username'])); $user_password = mysqli_real_escape_string($dbc, trim($_POST['password'])); if (!empty($user_username) && !empty($user_password)) { // Look up the username and password in the database $query = "SELECT user_id, username FROM mismatch_user WHERE username = '$user_username' AND password = SHA('$user_password')"; $data = mysqli_query($dbc, $query); if (mysqli_num_rows($data) == 1) { // The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page $row = mysqli_fetch_array($data); $_SESSION['user_id'] = $row['user_id']; $_SESSION['username'] = $row['username']; echo'you have successfully logged in as '.$_SESSION['username'].'.
'; echo'Edit your profile'; } else { // The username/password are incorrect so set an error message $error_msg = 'Sorry, you must enter a valid username and password to log in.'; } } else { // The username/password weren't entered so set an error message $error_msg = 'Sorry, you must enter your username and password to log in.'; } } } ?> Mismatch - Log In

Mismatch - Log In

<?php // If the session var is empty, show any error message and the log-in form; otherwise confirm the log-in if (empty($_SESSION['user_id'])) { echo $error_msg; ?> Log In Username:
Password: <?php } ?>

please help me friends!!

Sponsor our Newsletter | Privacy Policy | Terms of Service