• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

php mysql login script problem

hi,

i created the following php login authentication page but it's not working.  i cannot output the querry even when connected.

can anyone point out what i am doing wrong and how to fix it?

thanks in advance  
<?php
function get_field($fld)
{
    if(!isset($_POST[$fld])) return '';         # field not posted
        $value = $_POST[$fld];
    if(get_magic_quotes_gpc())                  # did php add slashes?
        $value = stripslashes($value);            # yes, remove them
    $value = mysql_real_escape_string($value);  # add mysql slashes
  return $value;
}
    //connection and selection variables for mysql connection
    $dbuser = get_field('username');
    $dbpass = get_field('password');
    $dbhost = 'localhost';
    $dbname = 'ShopFloorDB';


if($_POST['login_now']=='Login')
{
    $con = mysql_connect($dbhost, $dbuser, $dbpass);
    
    if (!$con)
    {
        echo "<script>alert('not connected')</script>";
        die('Could not connect: ' . mysql_error());
    }
    else
    {
        //select the mysql db
        mysql_select_db($dbname, $con);
        $result = mysql_query("SELECT * FROM jobinfo");
        echo $result;
    }
    mysql_close($con);
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SFDB Login</title>
</head>
<body>
<form action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="post">
Username: <input type="text" name="username" value="<?php echo $dbuser; ?>" size="15"> <br>
Password: <input type="text" name="password" value="<?php echo $dbpass; ?>" size="15"> <br>
<input type="submit" name="login_now" value="Login">
</form>
</body>
</html>

Open in new window

0
Wayne88
Asked:
Wayne88
1 Solution
 
sshah254Commented:
Basic question -

1.   Does the joninfo have any data?
2.  What's the error message?  Use the try/catch and log the error message.

Ss
0
 
Dave BaldwinFixer of ProblemsCommented:
"mysql_real_escape_string()" requires an open database connection run.  You need to connect to your database before your use that statement.  http://us.php.net/manual/en/function.mysql-real-escape-string.php
0
 
Wayne88Author Commented:
hi,

jobinfo table does have data in it and it's working.  the problem is with the login.  what i am trying to do is simply use the credentials for users in mysql as the login credentials for the page.  if the user succesfully authenticated him/herself with the mysql server then it will let them in.

this way i don't have to create a separate table to store user's credentials.  if this is too much trouble then i guess i will just go with this option but i would rather use the mysql credentials in the login page.

there is no error message, when i type in a bad credentials then it does tell me that i failed to login.  the problem is when i typed in the correct user credentials then try to query.  it doesn't seem to work.

thanks.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
0
 
Wayne88Author Commented:
hi all,

i appreciate everyone trying to help but after thinking about the my code it won't work because it wouldnt make sense for me to include that php into other php which will get he prompt being recalled each time.

ray's article was very well done but it was far more thorough then what i need it for so decided to go with this solution instead: http://www.9lessons.info/2009/09/php-login-page-example.html

thanks again
0
 
Wayne88Author Commented:
hi all,

i appreciate everyone trying to help but after thinking about the my code it won't work because it wouldnt make sense for me to include that php into other php which will get he prompt being recalled each time.

ray's article was very well done but it was far more thorough then what i need it for so decided to go with this solution instead: http://www.9lessons.info/2009/09/php-login-page-example.html

thanks again
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now