hi this is my php code: PHP: <?php // Send variables for the MySQL database class. $database = mysql_connect('localhost', 'root', '123') or die('Could not connect: ' . mysql_error()); mysql_select_db('sproject01') or die('Could not select database'); $username = $_GET['username']; $password = $_GET['password']; $query = "select * FROM `users` WHERE username='$username' and password='$password' LIMIT 1"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); and this is error the I get : http://i.imgur.com/sdYrEbX.png how can i fix this?
PHP: <?php session_start(); // Send variables for the MySQL database class. $database = mysql_connect('localhost', 'root', '123') or die('Could not connect: ' . mysql_error()); mysql_select_db('fpsproject') or die('Could not select database'); if (isset($_GET['username']) and isset($_GET['password'])) { $username = $_GET['username']; $password = $_GET['password']; } else { echo "It throwed up an error"; } $query = "select * FROM `users` WHERE username='$username' and password='$password' LIMIT 1"; // mysql_query("SELECT * FROM `users` WHERE username = $_GET['username']")or die('Query failed: ' . mysql_error()); $result = mysql_query($query) or die('Query failed: ' . mysql_error()); $num_results = mysql_num_rows($result); for($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo $row['username'] . "\t" . $row['password'] . "\n"; } ?>
Try this: Code (csharp): <?php session_start(); // Send variables for the MySQL database class. $database = mysql_connect('localhost', 'root', '123') or die('Could not connect: ' . mysql_error()); mysql_select_db('fpsproject') or die('Could not select database'); if (isset($_GET['username']) and isset($_GET['password'])) { $username = $_GET['username']; $password = $_GET['password']; $query = "select * FROM `users` WHERE username='$username' and password='$password' LIMIT 1"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); $num_results = mysql_num_rows($result); for($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo $row['username'] . "\t" . $row['password'] . "\n"; } } else { echo "It throwed up an error"; } ?>
So another words your not actually setting the username and password so it's going into the else statement.
The problem is that if i change the query to insert into usres values (NULL, '$username', '$password'); there is no error
"It throwed up an error" mean the condition if (isset($_GET['username']) and isset($_GET['password'])) is not passed, db query not executed at all. You have to access the script by http with both variables in url, like http://yourwebsite/pathtoscript/login2.php?username=aaaa&password=bbbb. This is $_GET for.