Jump to content

[SOLVED] What wrong with this? User ID seems to wreck it...


forumnz

Recommended Posts

you said before, its $_SESSION['userid']

if you look at the top of the code on the first page Greaser9780 you can see $userid is defined.

 

try this forumnz:

 

<?php
session_start();
$userid=$_SESSION['userid'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php

$database=mysql_connect("localhost","user", "pass");



mysql_select_db("test",$database);

$query = "SELECT * FROM members WHERE `userid`='{$userid}'";
$result=mysql_query($query) or die("Error in query: ".mysql_error());

if(mysql_num_rows($result) > 0){
echo "We are getting a result!";
}

while($img=mysql_fetch_assoc($result)){

$image_num=$img['stg1'];

if($image_num=="0"){

echo"<img src='images/not_completed.gif' />";

}elseif($image_num=="1"){

echo"<img src='images/completed.gif' />";

}else {

echo" sorry there is no images to show";

}
}
?>

</body>
</html>

 

tell me if anything outputs to the screen.

Link to comment
Share on other sites

  • Replies 92
  • Created
  • Last Reply

This is the output code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>


</body>
</html>

 

and this is require.php

 

<?php /// require.php
session_start();


if (!$_SESSION['username']){echo('<meta http-equiv="Refresh" content="0;url=clientlogin.php" />Please Log In');

};

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



?>

Link to comment
Share on other sites

Change this

<?php
$result = mysql_query($query) or die(mysql_error());
?>

to

<?php
$result = mysql_query($query) or die("Problem with the query <pre>$query</pre><br>" . mysql_error());
?>

 

Does it display anything?

 

Ken

Link to comment
Share on other sites

These one line answers without explanation don't help. Please post the current non-working code with an explanation of what isn't working. Somebody coming into this discussion now is not going to read through all 60+ responses to try to figure out what is wrong.

 

Ken

Link to comment
Share on other sites

Alright sorry.

 

Heres what I want to happen.

 

1. User log in

2. User clicks on link to progress.php

3. 5 images show up on this page. Each of the images are one of two and what they are depends on whether a 1 or a 0 is present in the database.

 

For each user there will be different ones showing - depending on what the numbers are (1 or 0). I am trying to get a users images to be displayed using their unique ID which should be set when the log in.

 

Here is the code which has the images (please note - for ease purpose, I have shortened it to one image).

 

<?php
session_start();
$userid=$_SESSION['userid'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php

$database=mysql_connect("localhost","user", "pass");



mysql_select_db("test",$database);

$query = "SELECT * FROM members WHERE `userid`='{$userid}'";
$result=mysql_query($query) or die("Error in query: ".mysql_error());

if(mysql_num_rows($result) > 0){
echo "We are getting a result!";
}

while($img=mysql_fetch_assoc($result)){

$image_num=$img['stg1'];

if($image_num=="0"){

echo"<img src='images/not_completed.gif' />";

}elseif($image_num=="1"){

echo"<img src='images/completed.gif' />";

}else {

echo" sorry there is no images to show";

}
}
?>

</body>
</html>

 

Here is the login:

 

<?php session_start(); ?>
<?php
/// connect to database
require("config.php");
$con=mysql_connect($mysql_hst,$mysql_us,$mysql_ps);
if (!$con)
  {
  die('Could not connect: ' . mysql_error() . '<br /><br />Check your config file or contact your server support team.');
  };
  
/// select DB
if (!mysql_select_db($mysql_db, $con)){die ("Could not connect to the database you specified. Make sure it exists and is correctly named within config.php");};
$_SESSION['username'] = str_replace(' ','/space',strtolower(mysql_real_escape_string($_POST[username])));
$_SESSION['password'] = base64_encode($_POST[password]);


/// connect to database
$con=mysql_connect($mysql_hst,$mysql_us,$mysql_ps);
if (!$con)
  {
  die('Could not connect: ' . mysql_error() . '<br /><br />Check your config file or contact your server support team.');
  };
  
/// select DB
if (!mysql_select_db($mysql_db, $con)){die ("Could not connect to the database you specified. Make sure it exists and is correctly named within config.php");};

if ($_POST["username"] == ""){die ('<meta http-equiv="Refresh" content="0;url=error.php?li=Please+enter+valid+details" />');};
if ($_POST["password"] == ""){die ('<meta http-equiv="Refresh" content="0;url=error.php?li=Please+enter+valid+details" />');};

$password = base64_encode($_POST[password]);
$username = str_replace(' ','/space/',mysql_real_escape_string(strtolower($_POST[username])));
$found = 0;
/// if exists
$ifexists = mysql_query("SELECT * FROM members");
while($row = mysql_fetch_array($ifexists))
  {
  if ($row[username] == $username){
  /// check password
  if ($row[password] == $password){
  ///check active
  if ($row[active] == "1") {$found = "1";$_SESSION['userid'] = $row[id];$_SESSION['userid'] = $row[id];} else {echo ("Account has not been activated - Check Your Email");};} else {echo('<meta http-equiv="Refresh" content="0;url=error.php?li=Please+enter+valid+details" />');};};};

if ($found == "0") {die('<meta http-equiv="Refresh" content="0;url=error.php?li=Please+enter+valid+details" />');};

if ($found == "1") {echo('<meta http-equiv="refresh" content="1; URL=index.php" />');};

?>

 

and if needed, here is require.php

 

<?php /// require.php
session_start();


if (!$_SESSION['username']){echo('<meta http-equiv="Refresh" content="0;url=clientlogin.php" />Please Log In');

};

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



?>

 

It currently displays this error:

 

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 '' at line 1

Link to comment
Share on other sites

Please don't bump the thread so quickly -- wait at least 3 or 4 hours.

 

You need to learn how to debug PHP scripts. Put echo statements at key places where you think the script is failing. Also, since it looks like you're having problems getting a value from the $_SESSION array, put this line:

<?php
echo '<pre>' . print_r($_SESSION,true) . '</pre>';
?>

immediately after the "session_start()" in the script that's failing.

 

If you don't see the value you're looking for in the $_SESSION array, look at the routine where you think you're setting it and put echo statements there too. If you use correct debugging techniques, you should be able to determine what is going wrong.

 

Ken

Link to comment
Share on other sites

Excellent I have something now.

 

I did that and out popped this:

 

Array
(
    [username] => admin1122
    [password] => YWRtaW4=
    [userid] => 
)

 

This is a test username and password exactly like it is in the database. This users ID is 4 in the db but its not showing here. Does that mean when the user logs in its not setting the userid as well?

Link to comment
Share on other sites

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.