Jump to content

Database Code Problem


Shoe

Recommended Posts

Ok so I pretty much just started at .php, and am constructing a registration page/login page for my website that hooks up to MySQL database. All the other parts of my code work except for this file, the login.php file.

 

This is pretty much the whole code, leaving out my database login/password:

 

//Connect to database

 

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());

mysql_select_db($dbname) or die(mysql_error());

 

session_start();

$username = $_POST[‘username’];

$password = $_POST[‘password’];

 

$query = “select * from users where username=’$username’ and password=’$password’”;

 

$result = mysql_query($query);

 

if (mysql_num_rows($result) != 1) {

$error = “Bad Login”;

    include “login.html”;

 

} else {

    $_SESSION[‘username’] = “$username”;

    include “memberspage.php”;

}

 

?>

 

The error message I receive when running the .php script is:

Parse error: syntax error, unexpected T_STRING in /Applications/XAMPP/xamppfiles/htdocs/login.php on line 19

 

And line 19 is:

$query = “select * from users where username=’$username’ and password=’$password’”;

 

I've tried almost everything to fix this line of code and finally came to you guys. What is wrong with it?

 

Thanks.

Link to comment
Share on other sites

mysql_query missing

 

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

session_start();
$username = $_POST[‘username’];
$password = $_POST[‘password’];

$query = mysql_query(“select * from users where username=’$username’ and password=’$password’”);

$result = mysql_query($query);

if (mysql_num_rows($result) != 1) {
$error = “Bad Login”;
    include “login.html”;

} else {
    $_SESSION[‘username’] = “$username”;
    include “memberspage.php”;
}

?>

 

Link to comment
Share on other sites

Alright (really sorry to bother you guys with my lack of .php knowledge, im sure this stuff is easily repairable) so now the login screen is showing up (yay!) but I am getting two errors.

 

#1: Warning: mysql_query() expects parameter 1 to be string, resource given in /Applications/XAMPP/xamppfiles/htdocs/login.php on line 21

 

# 2: Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/login.php on line 23

 

Any idea what to do? Thanks guys, I appreciate it.

Link to comment
Share on other sites

You're attempting to execute a query twice. Once here:

$query = mysql_query("select * from users where username='$username' and password='$password'");

 

then again here, using the result resource from the first query as a query string:

$result = mysql_query($query);

 

So, that part needs to be rewritten as:

$query = "select * from users where username='$username' and password='$password'";
$result = mysql_query($query);

Link to comment
Share on other sites

Alright, thank you very much.

 

So now I got all of the error messages deleted, but when I run the login.php file, it links to the memberspage.php without having the user login first. In other words, it just displays "Welcome, !" instead of displaying a login form.

 

Here is the memberspage.php, although I'm not sure if it will be of use to you.

 

<?php

 

session_start();

 

echo "Welcome, ".$_SESSION['username']."!";

 

?>

 

Thanks guys.

Link to comment
Share on other sites

Alright, thank you very much.

 

So now I got all of the error messages deleted, but when I run the login.php file, it links to the memberspage.php without having the user login first. In other words, it just displays "Welcome, !" instead of displaying a login form.

 

Here is the memberspage.php, although I'm not sure if it will be of use to you.

 

<?php

 

session_start();

 

echo "Welcome, ".$_SESSION['username']."!";

 

?>

 

Thanks guys.

 

change it to this code

<?php

//starts session
session_start();

//check if logged-in if not echo login form
if(!session_is_registered(username)){

echo "<form action='login.php' method='post' name='login'>
Username:<input name='username' type='text' value=''>
Password:<input name='password' type='password' value=''><input name='Submit' type='submit' value='SUBMIT!' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'><input name='Register' type='button' value='Register' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'>
</form>";

}

// if user logged in, then echo welcome, username
else {

echo "<center> Welcome, ";
echo($_SESSION['username']);
echo "</center>"

}
?>

 

if you want to the user to be redirected to the previous page if not logged in, the

 

change

echo "<form action='login.php' method='post' name='login'>
Username:<input name='username' type='text' value=''>
Password:<input name='password' type='password' value=''><input name='Submit' type='submit' value='SUBMIT!' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'><input name='Register' type='button' value='Register' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'>
</form>";

with

header("location:index.php");

 

Link to comment
Share on other sites

<?php
//starts session -- no white spaces --
session_start();

//check if logged-in if not echo login form
if(!isset($_SESSION['username'])){

echo "<form action='login.php' method='post' name='login'>
Username:<input name='username' type='text' value=''>
Password:<input name='password' type='password' value=''><input name='Submit' type='submit' value='SUBMIT!' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'><input name='Register' type='button' value='Register' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'>
</form>";

}

// if user logged in, then echo welcome, username
else {
echo "<center>Welcome, <strong>".$_SESSION['username']."</strong></center>";
}
?>

Link to comment
Share on other sites

Alright, fixed the memberspage.php (I think) but now when I run the login.php, I get a random error saying:

 

Parse error: syntax error, unexpected '}', expecting ',' or ';' in /Applications/XAMPP/xamppfiles/htdocs/memberspage.php on line 23

 

Line 23: $error = "Bad Login"

 

 

Link to comment
Share on other sites

Oh wait, the error is in the memberspage.php

 

I'm stupid..

 

Ok so I used the code you guys gave me for the memberspage.php and I fixed the error I just received (semicolon missing) but now im getting a:

 

Deprecated: Function session_is_registered() is deprecated in /Applications/XAMPP/xamppfiles/htdocs/memberspage.php on line 7

 

And line 7: if(!session_is_registered(username)){

Link to comment
Share on other sites

The line of code you posted is missing the semi-colon ; at the end of the statement.

 

And that cannot be a random error because it is a fatal parse error and is due to incorrect syntax in the code.

 

Ah sorry, didn't know "random" was a technical term in .php.

 

I was just using it as an adjective  :D

 

Ok so now I've got all the errors out of the way, but when I run login.php I'm still just receiving: "Welcome,  "

Link to comment
Share on other sites

Programming is an exact science and we only see the information that you provide in your post. Random in programming or in a programming problem means something different than the same error always occur at the same point.

 

 

I'm still just receiving: "Welcome,  "

 

^^^ It would take seeing your current code that is setting the session variable and the code that is displaying the session variable to be able to help you with why it is not working.

 

Link to comment
Share on other sites

Programming is an exact science and we only see the information that you provide in your post. Random in programming or in a programming problem means something different than the same error always occur at the same point.

 

 

I'm still just receiving: "Welcome,  "

 

^^^ It would take seeing your current code that is setting the session variable and the code that is displaying the session variable to be able to help you with why it is not working.

 

Ok so I have four files, two of which deal with the registration phase, two of which deal with the login phase. Then I have the memberspage.php of course. The one login form is just an .html that the login.php links with. The other is the login.php where I think the session variable is set...

 

<?php

 

//Database Information

 

$dbhost = "localhost";

$dbname = "__________";

$dbuser = "__________";

$dbpass = "_______________";

 

//Connect to database

 

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());

mysql_select_db($dbname) or die(mysql_error());

 

session_start();

$username = $_POST['username'];

$password = $_POST['password'];

 

$query = "select * from users where username='$username' and password='$password'";

$result = mysql_query($query);

 

if (mysql_num_rows($result) != 1) {

$error = "Bad Login";

    include "login.html";

 

} else {

    $_SESSION['username'] = "$username";

    include "memberspage.php";

}

 

?>

 

Then if the login is correct, it displays memberspage.php which you guys gave me which now looks like this:

 

<?php

 

//starts session

session_start();

 

//check if logged-in if not echo login form

if(!isset($_SESSION['username'])){

 

echo "<form action='login.php' method='post' name='login'>

Username:<input name='username' type='text' value=''>

Password:<input name='password' type='password' value=''><input name='Submit' type='submit' value='SUBMIT!' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'><input name='Register' type='button' value='Register' style='width:100; text-align:center; border-color:#F00; background-color:#FFF'>

</form>";

 

}

 

// if user logged in, then echo welcome, username

else {

 

echo "<center> Welcome, ";

echo($_SESSION['username']);

echo "</center>";

 

}

?>

Link to comment
Share on other sites

Your code works for me, other than a notice error concerning using two session_start() statements.

 

About the only way the posted code would NOT display the username is if you have a row in your user table with a blank username and either your form is not submitting data using the fields with those names or you entered nothing in the username field in the form.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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