/    Sign up×
Community /Pin to ProfileBookmark

Form problem..

Hi guys, can someone assist me – I don’t understand why every time I run this code the error msg “Please enter a username and password! ” appear before I type anything.

login.php

[code=php]
<?php

$username = (isset($_POST[‘username’])) ? $_POST[‘username’] : ”;
$password = (isset($_POST[‘password’])) ? $_POST[‘password’] : ”;

if($username && $password) {

$connect = mysql_connect(“localhost”, “root”, “”) or die (“Couldn’t connect!”);
mysql_select_db(“friendsdb”) or die (“Couldn’t find the DB”);

$query = mysql_query (“SELECT * FROM `user` WHERE username = ‘$username'”);

$numrows = mysql_num_rows($query);

if ($numrows != 0){

while ($row = mysql_fetch_array($query)) {

$dbusername = $row[‘username’];
$dbpassword = $row[‘password’];
}

//Check to see if they are match!
if ($username == $dbusername && md5($password) == $dbpassword) {
echo “You are in! <a href = ‘member.php’> Click </a> here to enter the member page!”;

$_SESSION[‘username’] = $username;
}
else
echo “Incorrect password!”;
//code of login

}else
echo “That user does not exist!”;

//echo $numrows;
}
else
echo “Please enter a username and password!”;
?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
</head>

<body>
<form action=”<?php echo $_SERVER[‘PHP_SELF’];?>” method=”post”>
<!–form action=”login.php” method=”post”–>
Username: <input type=”text” name=”username” /><br />
Password: <input type=”password” name=”password” /><br />

<input type=”submit” value=”Log in” />
</form> <p>

<a href=”register.php”>Register?</a>
</body>
</html>

[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@iahneFeb 17.2011 — That just mean that the username and password is empty. Try to echo it and find for yourself. You said everytime you run the code. But that code is supposed to be called by another page who has the form and has the objects username and password, or else (as stated on your code) the two variables will be empty.
Copy linkTweet thisAlerts:
@siabanieauthorFeb 17.2011 — Well of course the password and username is empty as I have not put anything yet but shouldn't it be showing that unless if I clicked the button?

And in what way I could put this form together so it will work as a form login validation..?
Copy linkTweet thisAlerts:
@tirnaFeb 17.2011 — It's because

[code=php]
if ($username && $password) {
[/code]


is evaluating to false. The reason is obvious.
Copy linkTweet thisAlerts:
@siabanieauthorFeb 17.2011 — Thanks tirna, so how can I solve it? I am new about PHP would you please suggest me..thanks.
Copy linkTweet thisAlerts:
@codeM0nK3YFeb 17.2011 — You need to wrap the PHP in some code that checks if the form has been submitted.

[code=php]<?php
if (isset ($_POST['submit'])) {
// Your code here
}
?>
<!-- HTML Stuff Here -->[/code]


And change your submit button to this:
[code=html]<input type="submit" value="Log in" name="submit" />[/code]
Copy linkTweet thisAlerts:
@siabanieauthorFeb 17.2011 — Thanks codeM0nK3Y,

It certainly solved that problem - I have some questions to ask you: I am creating forms:

My DB FRIENDSDB are:

Table USER: id, username, password, fullname.

Table DEPT: id, dept_name

Note: dept_name (is a select option which user can select either: IT, ISP, Creative)

Table PERSON: id, surname, firstname, address, mobile, dept_id

  • 1. Admin (Where admin allow to edit, delete, modify the details)

    Set username: admin password: 123456


  • 2. User / Guest (Which user need to register first then can log in, once they are log in they can create they own details. They are only allow to edit and view all the data at the same department but not allow to view other departments e.g. If they are in IT department they can view and edit all the employees in that department and they can edit them but they not allow to delete them (only admin can do this).


  • I have created the register.php and the login.php back at post#1 but I don't know how we can add admin and user at the same form but once they click the log in button they will have a different page. (Admin can view all, User only can view their own department)

    Can anyone suggest me an idea? So far I have forms:

  • 1. register.php (for new user)

  • 2. create.php

  • 3. edit.php

  • 4. delete.php

  • 5. logout.php

  • 6. index.php


  • Any inputs would be appreciated, thanks.
    Copy linkTweet thisAlerts:
    @siabanieauthorFeb 17.2011 — Here is my create code: I think the admin create and user create almost the same. I am still a bit confused how this can be created but any suggestion would be appreciated.

    Thanks.

    create page.
    [code=php]
    <?php
    require 'includes/application_top.php';

    $data = mysql_query ("SELECT persons.id, persons.name, persons.surname, persons.address, persons.mobile,
    dept.dept_name FROM persons LEFT JOIN dept ON persons.dept_id = dept.id ORDER BY persons.id DESC") or die (mysql_error());


    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Friends Document</title>
    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
    <table width="1000" height="60" border="0" cellpadding="3">
    <tr>

    <td><a href="newPerson.php?page=create">Create User </a></td>
    <td></td>
    <td><a href="logout.php?page=log_out">Log Out</a></td>
    </tr>

    <table width="808" border="1" cellpadding="3">

    <tr>
    <th width="17">Id</th>
    <th width="89">First Name</th>
    <th width="95">Surname</th>
    <th width="160">Address</th>
    <th width="84">Department</th>
    <th width="105">Phone</th>
    <th width="88">Edit Link</th>
    <th width="86">Delete Link</th>
    </tr>
    <?php

    while($row = mysql_fetch_array( $data ))

    {
    ?>
    <tr>
    <td><?php echo $row['id'];?></td>


    <td>
    <?php echo $row['name'];?>


    <?php
    if(isset($_GET['id']) && (isset($_GET['page']))) {

    if($_GET['id'] == $row['id'] && $_GET['page'] == "create"){
    echo "<div style=color:red;> Created</div>";
    //echo '<div style="color: red;"> Created</div>';
    }
    if($_GET['id'] == $row['id'] && $_GET['page'] == "modify"){
    //echo "Modified";
    echo "<div style=color:red;> Modified</div>";
    }
    }
    ?>
    </td>
    <!--Printing and pulling all the data-->
    <td><?php echo $row['surname'];?></td>
    <td><?php echo $row['address'];?></td>
    <td><?php echo $row['dept_name'];?></td>
    <td><?php echo $row['mobile'];?></td>

    <!--modify.php?= (showing the id)-->
    <!--td><a href = "newForm.php?id=<?php echo $row['id'];?>">Modify User </a></td-->
    <td><a href = "newUser.php?id=<?php echo $row['id'];?>">Modify User </a></td>

    <td>


    <a href= "deleteConfirm.php?id= <?php echo $row['id'];?>">Delete User</a></td>


    </tr>

    <?php
    }
    ?>
    </table>
    </form>
    </body>
    </html>

    [/code]


    As for the login page as back on post #1 I would like to add something like this but Im bit confused...please assist me.

    [code=php]
    if (($_POST['user'] == 'admin') && ($_POST['pass'] == '123456'))

    {

    }
    [/code]
    ×

    Success!

    Help @siabanie spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...