View Single Post
Old 12-03-2008, 06:34 PM   #1 (permalink)
9three
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default Checking empty field then connect

I'm having issues figuring out what the problem is. I have a log in form that checks to see if the fields are empty, if they are, then it throws an error out. If they put something in both fields, it checks to see if they are a valid in the database.

PHP Code:
function db_connect() {
    
$dbconn = @mysql_connect('localhost''username''password') or die('Unable to connect to database at this time, please try again later.');
    
$select = @mysql_select_db('dbname') or die('Unable to select database, please try again later.');
}

function 
login($username$password){
    
$error = array();
    if (
count($_POST) > 0) { // assume the form has been submitted
    
if (empty($_POST['username']) || empty($_POST['password'])) $error[] = 'A valid username and password is needed';
        if (
count($errors)) {
    echo 
"$errors";
    }
    
    }else { 
//Check username and password
    
$conn db_connect();
    
$result $conn->query("SELECT * FROM admin WHERE username='$username' AND password=sha1('$password')");
    if (!
$result){
        die(
'Unable to connect to database at this time, please try again later.');
    }
    }

Whats happening is that my script is automatically trying to connect to my database when I load the page. I want it to run when the user hits "login".

In my HTML I have
Code:
<?php
login('username', 'password')
?>
What could I possibly be doing wrong ?
9three is offline  
Reply With Quote