Jump to content

Recommended Posts

Hi all,

 

well this is my first post in here i have had a read of the rules and think i get them all... i just hope im writing this in the right place.

 

on to my problem.

 

i have a mysql database containing information about customers that will login to my site... now without being too complicated i want to vairify if the username and password passed from my form are present in the database..

 

so i have used action = POST to send the values from the form and $_POST to get assign the values to variables $username and $password...

 

i can echo them without issue.

 

i can also carry out a query and echo the rows from the table... now what i want to do is to compare the results from the sql query and the values in $username and $password... i had a go and this below is what i came up with...

 

.....

if($connect_mysql)
{
echo "connection established to MYSQL";
}
else
{
die ("Connection could not be established with MYSQL");
}

$mysql_db = mysql_select_db("artsonlinermmo");
if($mysql_db)
{
echo "database mounted";
}
else 
{
die("could not mount AORMMO database");
}
$query = ("SELECT customer_name, customer_password FROM customer where customer_name like $username");
$results = mysql_query($query);
while($row = mysql_fetch_assoc($results))
if($username = $row['customer_name']);
{
echo "great your a user!";
}
else //(!$username == $row['customer_name']);

{
die("sorry not a user");
}
?>
.....

 

now when i browse the form and submit it... i get the following error message... i gather it is about the ELSE i have up there but i have gone over it a hundred times and cant see any problem with it...

 

any help would be greatly appreciated!

 

thanks in advanced

RMMO  ;D

 

PS this is only day 2 of using PHP for me so im trying to keep it simple... (this is supposed to be a login page)

One problems is that you're using a single "=" in this "if"

<?php
if($username = $row['customer_name']);
?>

Single "=" are for assignment. Use double equal "==" for comparison:

<?php
if($username == $row['customer_name']);
?>

 

Ken

im sorry about the comment... but that wasnt the trouble... i know that an else cant have any conditions attached. thats why the comment was there..... previously i tried to use a ELSEIF.. that didnt work either...

 

the == i didnt know about but sadly that hasnt solved it,.... the error is:

 

 

Parse error: syntax error, unexpected T_ELSE in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\artsonlinestorermmologin.php on line 52

 

thats the line with the ELSE on it... but you could guess that from the error :P

 

thanks for all the help!

 

still not solved any more help would be greatly appreciated

 

RMMO

 

 

<?php

        //      $username = mysql_real_escape_string($_POST['username']);

if($connect_mysql)
{
	echo 'connection established to MYSQL';
}
else
{
	die ('Connection could not be established with MYSQL');
}

$mysql_db = mysql_select_db('artsonlinermmo');

if($mysql_db)
{
	echo 'database mounted';
}
else 
{
	die('could not mount AORMMO database');
}

$query = ("SELECT `customer_name`, `customer_password` FROM `customer` WHERE `customer_name` = '$username'");
$results = mysql_query($query) or die(mysql_error());

	while($row = mysql_fetch_array($results))
	{
		if($username == $row['customer_name'])
		{
			$loggedin = true;
			echo 'great your a user!';
		}
		else
		{
			$loggedin = false;
			echo 'sorry not a user';
			exit();
		}
	}

?>

<?php
   session_start();

   $host = 'localhost'; // or your db host
   $dbuser = 'xxxxx'; // db username
   $dbpass = 'xxxxx'; // db password
   $dbname = 'xxxxx'; // db name

   // connect to and select db
   $conn = mysql_connect($host, $dbuser, $dbpass) or trigger_error("SQL", E_USER_ERROR);
   $db = mysql_select_db($dbname, $conn) or trigger_error("SQL", E_USER_ERROR);

   // if there are posted vars...
   if ($_POST['username'] &&  $_POST['password']) {
      // sanitize
      $username = mysql_real_escape_string($_POST['username']);
      $password = mysql_real_escape_string($_POST['password']);

      // see if they are in table
      $query = "SELECT `customer_name`, `customer_password` FROM `customer` WHERE `customer_name` = '$username' AND `customer_password` = '$password'";
      $results = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR);

      // find out how many rows returned. 
      $rows = mysql_num_rows($results);
      // if something returned...
      if ($rows > 0) {
         // user/pass exist, do something, like make a session var signifying user is logged in
         $_SESSION['loggedin'] = true;
      // if nothing returned...
      } else {
         //user/pass do not exist, do something
      } // end if..else $rows
   // if no username and/or pass posted...
   } else {
      // username and/or password not entered in form, do something
   } // end if..else username/password
?>

     

     

 

Wow thanks alot for all that help everyone the last post by Crayon Violent is the one i went with... it worked great!

 

no i know this is probably really bad but it threw an error for the session_start() saying that the headers were allready sent.. so i commented that bit out.... will i go to PHP hell for that? anyway im going to say that this is  <SOLVED>

 

thanks again to all that helped!

much appreciated!

RMMO

That means you had output before the session_start();.  Is this being included in some other file that already has output? Do you have any whitespace or lines before your opening <?php tag if you don't?  Without session_start(); you cannot make that session variable 'loggedin' so if you want to make use of session vars for data persistence from page to page, you need session_start(); at the top of every page that will use it.  If that script is being included in some other page, put it at the top of the page that's including it. 

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.