Whats wrong with my code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Whats wrong with my code?

Post by like_duh44 »

Ok, I have a site (chatroom) that has an included file. This file looks for the user prefs that are saved in a mysql database and puts them into array $mainprefs. No matter what, i'm not getting any values for these even though I tell them to put values if none are present. Why???

Code: Select all

<?php

if ($_GET&#1111;theme] == "")
    &#123;
        $theme = $mainprefs&#1111;1];
    &#125;
  else
    &#123;
        $theme = "$_GET&#1111;theme]";
    &#125;

if ($_GET&#1111;fontcolor] == "")
    &#123;
        $fontcolor = $mainprefs&#1111;2];
    &#125;
  else
    &#123; 
        $fontcolor = $_GET&#1111;fontcolor];
    &#125;

if ($_GET&#1111;fontbg] == "")
    &#123;
        $fontbg = $mainprefs&#1111;3];
    &#125;
  else
    &#123; 
        $fontbg = $_GET&#1111;fontbg];
    &#125;

if ($_GET&#1111;bold] == "")
    &#123;
        $bold = $mainprefs&#1111;4];
    &#125; 
  else
    &#123; 
        $bold = $_GET&#1111;bold];
    &#125;

if ($_GET&#1111;italic] == "")
    &#123;
        $italic = $mainprefs&#1111;5];
    &#125;
  else 
    &#123; 
        $italic = $_GET&#1111;italic];
    &#125;

if ($_GET&#1111;underline] == "")
    &#123;
        $underline = $mainprefs&#1111;6];
    &#125;
  else 
    &#123; 
        $underline = $_GET&#1111;underline];
    &#125;
?>
User avatar
Seth_[php.pl]
Forum Commoner
Posts: 30
Joined: Sun Aug 10, 2003 5:25 am
Location: Warsaw / Poland

Post by Seth_[php.pl] »

First of all use array like this:
$_GET['fontcolor']
Replace $_GET['fontcolor'] == "" with empty( $_GET['fontcolor'] )

do this with other values


P.S. PHP is not a Client-Side script
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

i did that but to no avail, just get an error about unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

and why did you think I thought it was client side?
User avatar
Seth_[php.pl]
Forum Commoner
Posts: 30
Joined: Sun Aug 10, 2003 5:25 am
Location: Warsaw / Poland

Post by Seth_[php.pl] »

it works for me:

Code: Select all

if ( empty( $_GET['theme'] ) ) 
{ 
  $theme = $mainprefs[1]; 
} 
else 
{ 
  $theme = $_GET['theme']; 
} 


if ( empty( $_GET['fontcolor'] ) ) 
{ 
  $fontcolor = $mainprefs[2]; 
} 
else 
{ 
  $fontcolor = $_GET['fontcolor']; 
} 


if ( empty( $_GET['fontbg'] ) ) 
{ 
  $fontbg = $mainprefs[3]; 
} 
else 
{ 
  $fontbg = $_GET['fontbg']; 
} 


if ( empty( $_GET['bold'] ) ) 
{ 
  $bold = $mainprefs[4]; 
} 
else 
{ 
  $bold = $_GET['bold']; 
} 


if ( empty( $_GET[italic] ) ) 
{ 
  $italic = $mainprefs[5]; 
} 
else 
{ 
  $italic = $_GET['italic']; 
} 


if ( empty( $_GET['underline'] ) ) 
{ 
  $underline = $mainprefs[6]; 
} 
else 
{ 
  $underline = $_GET['underline']; 
}
like_duh44 wrote:and why did you think I thought it was client side?
Becouse this is a Client-side forum :P
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

LOL, well, i copy-pasted (I know, i hate doing it, but I want to make sure its right. At least I know what the code is doing :)) and still came up with this error:

Fatal error: input in flex scanner failed in /home/virtual/site138/fst/var/www/html/chat/chat_themes on line 1

which means its still now getting the string because its supposed to be
/home/virtual/site138/fst/var/www/html/chat/chat_themes/default_theme.php
or whatever theme the person has in the database
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

Heres the exact (except for secret data) that is in the file:

Code: Select all

<?php

    $server = "localhost";
    $database = "vgapws1_myhost24_com";
    $user = "symbiosis";
    $password = "not shown for security reasons";

    $link = mysql_connect($server, $user, $password) or die("Could not connect");
    mysql_select_db($database, $link) or die("Could not select database");

$mainquery = "SELECT * FROM `chat_prefs` WHERE `username` = '$_GET[username]'";
$mainresults = mysql_query($mainquery, $link) or die("Main Prefs Query Failed!");

while ($mainrow=mysql_fetch_row($mainresults))
{
$mainprefs[] = $mainrow;
}

if (!eregi("aatachat.php", $_SERVER['PHP_SELF'])) {

if ( empty( $_GET['theme'] ) ) 
{ 
$theme = $mainprefs[1]; 
} 
else 
{ 
$theme = $_GET['theme']; 
} 


if ( empty( $_GET['fontcolor'] ) ) 
{ 
$fontcolor = $mainprefs[2]; 
} 
else 
{ 
$fontcolor = $_GET['fontcolor']; 
} 


if ( empty( $_GET['fontbg'] ) ) 
{ 
$fontbg = $mainprefs[3]; 
} 
else 
{ 
$fontbg = $_GET['fontbg']; 
} 


if ( empty( $_GET['bold'] ) ) 
{ 
$bold = $mainprefs[4]; 
} 
else 
{ 
$bold = $_GET['bold']; 
} 


if ( empty( $_GET[italic] ) ) 
{ 
$italic = $mainprefs[5]; 
} 
else 
{ 
$italic = $_GET['italic']; 
} 


if ( empty( $_GET['underline'] ) ) 
{ 
$underline = $mainprefs[6]; 
} 
else 
{ 
$underline = $_GET['underline']; 
}
}

if (!eregi("chat_posts.php", $_SERVER['PHP_SELF'])) {

include ("chat_themes/$theme");
echo "<style>";
echo "\n<!--\n";
echo "body	{ color: $color[6]; background-color: $color[3]}\n";
echo "a	{ color: $color[8]}\n";
echo "--!>\n";
echo "</style>";

} else {

include ("chat_themes/$theme");
echo "<style>";
echo "\n<!--\n";
echo "body	{ color: $color[7]; background-color: $color[4]}\n";
echo "a	{ color: $color[8]}\n";
echo "--!>\n";
echo "</style>";

}
User avatar
Seth_[php.pl]
Forum Commoner
Posts: 30
Joined: Sun Aug 10, 2003 5:25 am
Location: Warsaw / Poland

Post by Seth_[php.pl] »

The code I've posted is good so i supose it must be other reason why it dosen't work :?
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

I did a test, to see if it was even getting any values from the query. Heres the code:

Code: Select all

<?php
$mainname = $_GET[username];

    $server = "localhost";
    $database = "vgapws1_myhost24_com";
    $user = "symbiosis";
    $password = "not shown";

    $link = mysql_connect($server, $user, $password) or die("Could not connect");
    mysql_select_db($database, $link) or die("Could not select database");

$mainquery = "SELECT * FROM `chat_prefs` WHERE `username` = '$mainname'";
$mainresults = mysql_query($mainquery, $link) or die("Main Prefs Query Failed!");

$mainprefs[] = mysql_fetch_array($mainresults);

mysql_free_result($mainresults);

$theme = $mainprefs[1];

$title = "?username=$mainname&theme=$theme&fontcolor=$mainprefs[2]&fontbg=$mainprefs[3]&bold=$mainprefs[4]&italic=$mainprefs[5]&underline=$mainprefs[6]";

echo "$title";

?>
And all it echoes is :
?username=Doom&theme=&fontcolor=&fontbg=&bold=&italic=&underline=
User avatar
Seth_[php.pl]
Forum Commoner
Posts: 30
Joined: Sun Aug 10, 2003 5:25 am
Location: Warsaw / Poland

Post by Seth_[php.pl] »

I think the problem is in:
$mainname = $_GET[username];
Should be:
$mainname = $_GET['username'];
Alsow change SQL query:
SELECT * FROM chat_prefs WHERE username = '$mainname'
And check what is the output of your query.
Post Reply