weasel8 Posted December 23, 2008 Share Posted December 23, 2008 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: Quote 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: Quote 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. Quote Cannot load mcrypt extension. Please check your PHP configuration. Quote Link to comment https://forums.phpfreaks.com/topic/138136-phpmysql-simple-login-script-wont-work/ Share on other sites More sharing options...
ngreenwood6 Posted December 23, 2008 Share Posted December 23, 2008 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: Quote 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'"; Quote Link to comment https://forums.phpfreaks.com/topic/138136-phpmysql-simple-login-script-wont-work/#findComment-722313 Share on other sites More sharing options...
weasel8 Posted December 23, 2008 Author Share Posted December 23, 2008 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: Quote 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? Quote Link to comment https://forums.phpfreaks.com/topic/138136-phpmysql-simple-login-script-wont-work/#findComment-722622 Share on other sites More sharing options...
fenway Posted December 23, 2008 Share Posted December 23, 2008 You're missing single quotes around your string literals in your query string. Quote Link to comment https://forums.phpfreaks.com/topic/138136-phpmysql-simple-login-script-wont-work/#findComment-722636 Share on other sites More sharing options...
ngreenwood6 Posted December 23, 2008 Share Posted December 23, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138136-phpmysql-simple-login-script-wont-work/#findComment-722650 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.