Jump to content

Whats wrong with this code snippet?


jumpenjuhosaphat

Recommended Posts

  [code]
  else
  {
    $result = mysql_query("SELECT * FROM user");

    while($row = mysql_fetch_array($result))
    {
      if($row['id']==$_COOKIE["user"])
        {
          $username=$row['username'];
        }
    }
    echo $username.' Cookie is set, logout is not set';
    include("body.php");
  }[/code]

For some reason, the variable $username isn't being assigned any value.  Is it a misuse of the $_COOKIE?
Link to comment
https://forums.phpfreaks.com/topic/26129-whats-wrong-with-this-code-snippet/
Share on other sites

Yes, it is part of a code that first determines if the cookie is set, then if other properties are true.  Here is the entire code:

[code]<?
$username='';
//*****************************************************************************************
//Open the database
//*****************************************************************************************

$con = mysql_connect("private","info","here");

if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

mysql_select_db("more_private", $con);


//*****************************************************************************************
//Check if a cookie is set, and if the logout submit button has been set
//*****************************************************************************************

if(isset($_COOKIE["user"]))
{
  if(isset($_POST["logout"]))
  {
    setcookie("user","", time()-3600);
    include("refresh.php");
  }
 
  else
  {
    $result = mysql_query("SELECT * FROM user");

    while($row = mysql_fetch_array($result))
    {
      if($row['id']==$_COOKIE["user"])
        {
          $username=$row['username'];
        }
    }
    include("body.php");
  }
}


//*****************************************************************************************
//if the cookie isn't set, check if the signin submit button has been set
//*****************************************************************************************

else
{
  if(isset($_POST["signin"]))
  {
   
    $result = mysql_query("SELECT * FROM user");

    while($row = mysql_fetch_array($result))
    {
      if($row['username']==$username&&$row['password']==$password)
      setcookie("user", $row["id"], time()+3600, "/");
    }
    include("refresh.php");
  }
 
  else
  {
    $username='guest';
    include("body.php");
  }
}

?>[/code]
I believe that "user" is a MySQL reserved word. So, change the table name or use backtick marks around `user`.

One should check if the SQL query worked before doing the fetch.

Since you're comparing the row id to what's saved in the cookie, then why not just read that row directly from the table?

Example:
[code=php:0]

  {
    $result = mysql_query("SELECT `username` FROM `user` WHERE id = '{$_COOKIE['user']}'");

    if (!$result) {
        // your error logic
    } else {
        $row = mysql_fetch_assoc($result);

        if ($row) {  // did we get data back?
              $username = $row['username'];
        } else {
              $username = '';  // no data matched search criteria - handle scenario
        }
    }

    include("body.php");
  }

[/code]

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.