Jump to content

Login Code issues (mysql_fetch_assoc)


Foser

Recommended Posts

<?php
include("config.php");

$user = $_POST['user'];
$pw = md5(sha1(md5(md5($_POST['pw']))));

mysql_fetch_assoc(mysql_query("SELECT password FROM user_info WHERE username = $user"));

if ($pw == $user_info['password']){
mysql_fetch_assoc(mysql_query("SELECT rights FROM user_info WHERE username = $user")); 
echo "You are now logged in as a $user_info['rights']."; } // this is line 11
else {
echo "You have typed in an incorrect password or/and username. Please try again."; }

?>

 

The error I get is :

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\WAMP\www\Tutorials\PHP_MYSQL\Simple_MySQL\Login\login.php on line 11

 

 

thanks, im not sure if i am doing the mysql fetch assoc correctly.

Link to comment
https://forums.phpfreaks.com/topic/56674-login-code-issues-mysql_fetch_assoc/
Share on other sites

Ok i've done that but now I get

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\WAMP\www\Tutorials\PHP_MYSQL\Simple_MySQL\Login\login.php on line 7

 

also I have typed in correct password and user, and it says under the error that my info is not correct.

mysql_fetch_assoc(mysql_query("SELECT password FROM user_info WHERE username = $user"));
mysql_fetch_assoc(mysql_query("SELECT rights FROM user_info WHERE username = $user")); 

Replace with

mysql_fetch_assoc(mysql_query("SELECT password FROM user_info WHERE username = '$user'"));
mysql_fetch_assoc(mysql_query("SELECT rights FROM user_info WHERE username = '$user'")); 

better you use die() wherever it is appropriate.

 

 

Yes I have here is my registration code.

 

<?php
require("config.php");
//user data
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['mail']);
$password = md5(sha1(md5(md5($_POST['pw']))));
$rights = "user";

$check_user = mysql_query("SELECT user FROM user_info WHERE user = '$username'");
$check_email = mysql_query("SELECT email FROM user_info WHERE email = '$email'");



if ($username > 0 ) {
echo "This username has already been taken.<br>";}

if ($email > 0 ){
echo "This E-mail has already been registered.<br>.";}


else {
$qu = "INSERT INTO user_info (username, email, password, rights) VALUES('$username', '$email', '$password', '$rights')";
mysql_query($qu);
echo "You are now registered, you may now login.<br>";
echo "<a href=\"index.php\">Click Here To Go To The Login Page </a>";

}

?>

I checked with mysql_real_escape_string() and still does not work! Ill put both scripts in one page

 

<?php
// registration


require("config.php");
//user data
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['mail']);
$password = md5(sha1(md5(md5($_POST['pw']))));
$rights = "user";

$check_user = mysql_query("SELECT user FROM user_info WHERE user = '$username'");
$check_email = mysql_query("SELECT email FROM user_info WHERE email = '$email'");



if ($username > 0 ) {
echo "This username has already been taken.<br>";}

if ($email > 0 ){
echo "This E-mail has already been registered.<br>.";}


else {
$qu = "INSERT INTO user_info (username, email, password, rights) VALUES('$username', '$email', '$password', '$rights')";
mysql_query($qu);
echo "You are now registered, you may now login.<br>";
echo "<a href=\"index.php\">Click Here To Go To The Login Page </a>";

}

?>

 

//login
<?php
require("config.php");

$user = mysql_real_escape_string($_POST['user']);
$pw = md5(sha1(md5(md5($_POST['pw']))));

mysql_fetch_assoc(mysql_query("SELECT password FROM user_info WHERE username = '$user'")) or die(mysql_error());

if ($pw == $user_info['password']){
mysql_fetch_assoc(mysql_query("SELECT rights FROM user_info WHERE username = '$user'")); 
echo "You are now logged in as a $user_info[rights]."; }
else {
echo "You have typed in an incorrect password or/and username. Please try again."; }

?>

I checked with mysql_real_escape_string() and still does not work! Ill put both scripts in one page

 

<?php
// registration


require("config.php");
//user data
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['mail']);
$password = md5(sha1(md5(md5($_POST['pw']))));
$rights = "user";

$check_user = mysql_query("SELECT user FROM user_info WHERE user = '$username'");
$check_email = mysql_query("SELECT email FROM user_info WHERE email = '$email'");



if ($username > 0 ) {
echo "This username has already been taken.<br>";}

if ($email > 0 ){
echo "This E-mail has already been registered.<br>.";}


else {
$qu = "INSERT INTO user_info (username, email, password, rights) VALUES('$username', '$email', '$password', '$rights')";
mysql_query($qu);
echo "You are now registered, you may now login.<br>";
echo "<a href=\"index.php\">Click Here To Go To The Login Page </a>";

}

?>

 

//login
<?php
require("config.php");

$user = mysql_real_escape_string($_POST['user']);
$pw = md5(sha1(md5(md5($_POST['pw']))));

mysql_fetch_assoc(mysql_query("SELECT password FROM user_info WHERE username = '$user'")) or die(mysql_error());

if ($pw == $user_info['password']){
mysql_fetch_assoc(mysql_query("SELECT rights FROM user_info WHERE username = '$user'")); 
echo "You are now logged in as a $user_info[rights]."; }
else {
echo "You have typed in an incorrect password or/and username. Please try again."; }

?>

 

bump

I get my else statement.

 

//login
<?php
require("config.php");

$user = mysql_real_escape_string($_POST['user']);
$pw = md5(sha1(md5(md5($_POST['pw']))));

mysql_fetch_assoc(mysql_query("SELECT password FROM user_info WHERE username = '$user'")) or die(mysql_error());

if ($pw == $user_info['password']){
mysql_fetch_assoc(mysql_query("SELECT rights FROM user_info WHERE username = '$user'")); 
echo "You are now logged in as a $user_info[rights]."; }
else {
echo "You have typed in an incorrect password or/and username. Please try again."; }

?>

 

I get: You have typed in an incorrect password or/and username. Please try again.

 

 

<?php
// registration


require("config.php");
//user data
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['mail']);
$password = md5(sha1(md5(md5($_POST['pw']))));
$rights = "user";

$check_user = mysql_query("SELECT user FROM user_info WHERE user = '$username'");
$check_email = mysql_query("SELECT email FROM user_info WHERE email = '$email'");



if ($username > 0 ) {
echo "This username has already been taken.<br>";}

if ($email > 0 ){
echo "This E-mail has already been registered.<br>.";}


else {
$qu = "INSERT INTO user_info (username, email, password, rights) VALUES('$username', '$email', '$password', '$rights')";
mysql_query($qu);
echo "You are now registered, you may now login.<br>";
echo "<a href=\"index.php\">Click Here To Go To The Login Page </a>";

}

?>

 

This is my registration code...I believe it is suited for the login too.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.