Jump to content

PHP+MySQL: Simple login script won't work


weasel8

Recommended Posts

I'm getting an odd error with a simple user login script I'm writing. Towards the beginning the script checks for $_POST['submit'] to see if the form has been filled out and submitted and needs to be processed.

 

But I keep seeing this error when I access the script in my browser:

 

Notice: Undefined index: submit in /srv/http/bm/index.php on line 4

 

Here's the script, index.php.

<?php
require("header.php");

if ($_POST['submit']) {
$sql = "SELECT * FROM users WHERE username = " . $_POST['username'] . " and password = " . $_POST['password']; 
$res = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($res); 
if ($res == 1) {
	// log in user...
}
else {
	$error = 1;  
}
}
?>

<h1>exmeamente.ws/bm</h1>
<p>Dead simple online bookmarks<br />
(<a href="about.html">more, for the curious</a>)</p>
<?php 
if (isset($error)) {
	echo "<p>Typo somewhere... have another go.</p>"; 
}
?>
<p><form action="index.php" method="post">
	<table>
		<tr>
			<td>Username</td>
			<td><input type="text" name="username" /></td>
		</tr>
		<tr>
			<td>Password</td>
			<td><input type="text" name="password" /></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" value="Log in" name="submit" /> or <a href="register.php">make one</a></td>
		</tr>
	</table>
</form></p>


<?
require("footer.php"); 
?>

 

Here's header.php and config.php, just in case.

<?php
require("config.php"); 
?>
<html>
<head>
<title><?php echo $config_sitename; ?></title>
<link rel="stylesheet" href="stylesheet.css" />
</head>
<body>

<?php

$dbhost = "localhost"; 
$dbuser = "root"; 
$dbpassword = "[removed]"; 
$dbdatabase = "bm"; 

$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die(mysql_error()); 
mysql_select_db($dbdatabase) or die("Unable to select database");

$config_sitename = "exmeamente.ws/bm";

?>

 

Additionally, when I try to use the form, say with username "sampleusername" and password "samplepassword", I get THIS error and nothing else:

sampleusernameUnknown column 'sampleusername' in 'where clause'

 

I've been over the scripts a thousand times and I still can't figure out what's wrong. Can better versed in PHP + MySQL identify my problems?

 

EDIT: I also just remembered this, for what it's worth: I consistently get the following error from phpMyAdmin, but haven't fixed it because according to php.net I need to recompile PHP with a certain flag to make it work, and so far it seems to have not broken anything.

Cannot load mcrypt extension. Please check your PHP configuration.
Link to comment
Share on other sites

If you don't have register_globals turned on then you will get that error try changing this:

 

if ($_POST['submit']) {

 

to this:

 

$submit = $_POST['submit'];
if($submit){

 

 

EDIT:

As for this:

sampleusernameUnknown column 'sampleusername' in 'where clause'

 

Same thing you need to define them first like this:

 

$username = $_POST['username'];
$password = $_POST['password'];

\\then change the query to this:
$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";

Link to comment
Share on other sites

Thanks, now I understand why I got the error. I'm used to Ubuntu's distribution of PHP, which is different from the one I'm using. The solution you offered didn't work (I got the same error), so I created a second file to process the form, login.php. It seems to be working pretty well, but now I get yet another error.

 

With login.php, I can easily print $_POST['username'] and $_POST['password'], but when I try to plug them into an SQL query, I get this:

Unknown column 'sampleuser' in 'where clause'

I've gone over my query and I can't figure out what I've done wrong. Here's login.php.

<?php
require("header.php");

$submit = $_POST['submit']; 
$username = $_POST['username']; 
$password = $_POST['password']; 

if ($submit) {
	echo $username;
	echo $password; 	
	$sql = "SELECT * FROM users WHERE username = $username and password = $password;"; 
	$res = mysql_query($sql) or die(mysql_error());
	if ($res == 1) {
		// log in user...
		echo "You have been logged in."; 
	}
	else {
		$error = 1;  
	}
}
require("footer.php"); 
?>

Anyone see what's the problem?

Link to comment
Share on other sites

try this:

 

<?php
require("header.php");

$submit = $_POST['submit']; 
$username = $_POST['username']; 
$password = $_POST['password']; 

if ($submit) {
	echo $username;
	echo $password; 	
	$sql = "SELECT * FROM users WHERE username = '$username' and password = '$password'"; 
	$res = mysql_query($sql) or die(mysql_error());
	if ($res == 1) {
		// log in user...
		echo "You have been logged in."; 
	}
	else {
		$error = 1;  
	}
}
require("footer.php"); 
?>

You were missing the single quotes and you had a semi-colon after password which was not needed

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.