PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
// error reporting temporarily disabled
error_reporting (E_ALL ^ E_NOTICE);
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password)
{
$connect = mysql_connect("localhost","root","") or die("Could not connect");
mysql_select_db("tracker") or die("Could not find database");
}
else
die("Incorrect information");
?>
I followed a tutorial but something is wrong. If I type in an incorrect database name or any incorrect MySQL information, it says "Incorrect information", which is my else for if the password/user is incorrect. And when I do type in the correct pass/username, it still says Incorrect information. Can you see anything I've blatantly done wrong?
You aren't checking if the form has been submitted. When it hasn't, $username and $password contain no value, so your if statement fails and the else is executed.
Perhaps more importantly, any tutorial advocating the use of mysql_connect et al. isn't worth following.
It's designed for MySQL 4, is missing features, is no longer developed, and was replaced by MySQLi way back in 2004. That people still use it is mind-boggling. MySQLi is a better option if you'll only be working with MySQL databases. PDO is better still as it offers portability across multiple DBMS without having to rewrite any code.
Celauran wrote:It's designed for MySQL 4, is missing features, is no longer developed, and was replaced by MySQLi way back in 2004. That people still use it is mind-boggling. MySQLi is a better option if you'll only be working with MySQL databases. PDO is better still as it offers portability across multiple DBMS without having to rewrite any code.
Well, looks like I'll be reading for a while.
In response to your first reply where you said it was not checking to verify submission, I Google'd it and found:
<?php
/*
* Turn error reporting off in production code. Turning it off in a development
* environment just makes debugging harder and provides no benefit. You really
* ought to set this in your php.ini
*/
error_reporting (E_ALL | E_STRICT);
// This serves no purpose.
// $username = $_POST['username'];
// $password = $_POST['password'];
if (!empty($_POST))
{
if ($_POST['username'] && $_POST['password'])
{
// Better to abstract this away and use an include
$connect = mysql_connect("localhost","root","") or die("Could not connect");
mysql_select_db("tracker") or die("Could not find database");
}
// Move this inside the $_POST check
else
die("Incorrect information");
}
?>
<?php
if (!empty($_POST))
{
if ($_POST['username'] && $_POST['password'])
{
$connect = mysql_connect("localhost","root","") or die("Could not connect");
mysql_select_db("tracker") or die("Could not find database");
}
else
die("Incorrect information");
}
?>
I didn't change the mysql_connect because I have not yet read up on includes, so I'll do that in a little while. Other than that, have I placed my code correctly?
I'll also note at this point that or die() is fine for development code and debugging, but is completely unacceptable in production code. Be sure to take the time to read up on error and exception handling.
Would I use the same code as I used with my existing mysql_connect or would it be a different bit of code? From what I've seen on Google, the difference is basically placing the data into a separate file. If I'm wrong, certainly correct me.
In regards to my file, I am now seeing:
[text]Notice: Undefined index: username in C:\xampp\htdocs\Tracker\login.php on line 5
Incorrect information[/text]
TheHappyPeanut wrote:Would I use the same code as I used with my existing mysql_connect or would it be a different bit of code? From what I've seen on Google, the difference is basically placing the data into a separate file. If I'm wrong, certainly correct me.
That's right. Straight cut and paste. Again, you don't want to use mysql_ functions, but you can read up on MySQLi/PDO when you have the time.
TheHappyPeanut wrote:In regards to my file, I am now seeing:
[text]Notice: Undefined index: username in C:\xampp\htdocs\Tracker\login.php on line 5
Incorrect information[/text]