vetman Posted April 25, 2008 Share Posted April 25, 2008 I get this error message- Connected to Database Wrong Username or Password from this script. Can anyone help? Thanks <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Link to comment https://forums.phpfreaks.com/topic/102948-i-need-some-help-i-get-this-error-message-connected-to-database-wrong-username/ Share on other sites More sharing options...
dptr1988 Posted April 25, 2008 Share Posted April 25, 2008 You shouldn't be using session_register, it depends on register globals which is bad. Use session_start() at the begining of your page and the $_SESSION global variable instead. Example: <?php $_SESSION['myusername'] =$myusername; ?> Have you checked the username and password right before they go into the SQL query to check if they are still correct? IF you still cant figure it out, please echo your SQL query and post it here. Link to comment https://forums.phpfreaks.com/topic/102948-i-need-some-help-i-get-this-error-message-connected-to-database-wrong-username/#findComment-527391 Share on other sites More sharing options...
vetman Posted April 25, 2008 Author Share Posted April 25, 2008 Thanks for the reply, I'm totally lost now. I guess this is out of my ability. Link to comment https://forums.phpfreaks.com/topic/102948-i-need-some-help-i-get-this-error-message-connected-to-database-wrong-username/#findComment-527468 Share on other sites More sharing options...
teng84 Posted April 25, 2008 Share Posted April 25, 2008 try and see whats gonna happen <?php session_start(); $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect($host, $username, $password)or die("cannot connect"); mysql_select_db($db_name)or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql) or die(mysql_error()); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['myusername'] =$myusername; $_SESSION['mypassword'] =$mypassword; header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Link to comment https://forums.phpfreaks.com/topic/102948-i-need-some-help-i-get-this-error-message-connected-to-database-wrong-username/#findComment-527475 Share on other sites More sharing options...
vetman Posted April 25, 2008 Author Share Posted April 25, 2008 This is the error I get now: Unknown column 'username' in 'where clause' Link to comment https://forums.phpfreaks.com/topic/102948-i-need-some-help-i-get-this-error-message-connected-to-database-wrong-username/#findComment-527489 Share on other sites More sharing options...
teng84 Posted April 25, 2008 Share Posted April 25, 2008 check your table and see if you have that field... im suspecting you don't have that table or there's a typo error Link to comment https://forums.phpfreaks.com/topic/102948-i-need-some-help-i-get-this-error-message-connected-to-database-wrong-username/#findComment-527491 Share on other sites More sharing options...
vetman Posted April 25, 2008 Author Share Posted April 25, 2008 Yea, My table was members , not where. New error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'username='john' and password='1234'' at line 1 Link to comment https://forums.phpfreaks.com/topic/102948-i-need-some-help-i-get-this-error-message-connected-to-database-wrong-username/#findComment-527495 Share on other sites More sharing options...
teng84 Posted April 26, 2008 Share Posted April 26, 2008 please post your code now and try to echo the query and paste here Link to comment https://forums.phpfreaks.com/topic/102948-i-need-some-help-i-get-this-error-message-connected-to-database-wrong-username/#findComment-527500 Share on other sites More sharing options...
vetman Posted April 26, 2008 Author Share Posted April 26, 2008 I get this message now: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'username='' and password=''' at line 1 <?php session_start(); $host=""; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect($host, $username, $password)or die("cannot connect"); mysql_select_db($db_name)or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name MEMBERS username='$myusername' and password='$mypassword'"; $result=mysql_query($sql) or die(mysql_error()); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['myusername'] =$myusername; $_SESSION['mypassword'] =$mypassword; header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Link to comment https://forums.phpfreaks.com/topic/102948-i-need-some-help-i-get-this-error-message-connected-to-database-wrong-username/#findComment-527684 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.