Variable error

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

NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Well I hade that if the user just went to profile.php they came to their own profile. And then i got the error i've got now.
And now i'm doing some things diffrent. If they go to profile.php an headers come and they get redirected to profile.php?=_username_

But I get some errors now that i can't get away, don't know why i get them though. Seems correct for me.


Notice: Undefined variable: age in /srv/www/htdocs/profile2.php on line 46

Notice: Undefined offset: 2 in /srv/www/htdocs/profile2.php on line 23

Notice: Undefined offset: 1 in /srv/www/htdocs/profile2.php on line 23


Code: Select all

<?php 
session_start(); // Alltid överst på sidan 
error_reporting(E_ALL);
include "configs.php"; // Databasanslutningen 
 
// Kolla om inloggad = sessionen satt
if (!isset($_SESSION['sess_user'])) {
  header("Location: index.php");
  exit;
}
?>
<?php 
$person = substr($_SERVER['QUERY_STRING'], 1);

if($person == $_SESSION['sess_user']) {

$person2 = $_SESSION['sess_user'];
$sql2 = "SELECT * FROM members WHERE username = '{$_SESSION['sess_user']}'"; 
$result2 = mysql_query($sql2) or die(mysql_error());

function birthday ($age){ 
     // assumes $birthdate is in YYYY-MM-DD format 
   list($dob_year, $dob_month, $dob_day) = explode('-', $age); 
   // determine current year, month, and day 
   $cur_year  = date('Y'); 
   $cur_month = date('m'); 
   $cur_day  = date('d'); 
   // either past or on the birthday 
   if($cur_month >= $dob_month && $cur_day >= $dob_day) { 
       $age1 = $cur_year - $dob_year; 
   } 
   // before the birthday 
   else { 
       $age1 = $cur_year - $dob_year; 
   } 
   // and your done 
   return $age1; 
  } 
     
 $content = "<table>"; 	 
while($rad = mysql_fetch_array($result2)) 
{ 
    $header = $person2;
   $header .= "&nbsp"; 
   $header .= $rad['sex']; 
   $header .= birthday($age); 
$age = $rad['age']; 
$kon = $rad['sex']; 


$content .= "<tr>";
$content .= "<td>";
$content .= "<br>";
   if(empty($rad['picture'])) 
   { 
      $content .= "<img src='images/inget-foto.jpg' width='50' height='50'>"; 
   } 
   else 
   { 
      $content .= "<img src='images/". $rad['picture']."'>"; 
   } 
$content .= "<br>";
$content .= "</td></tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Användarnamn: </b>".$rad['username']." </td><td width=20></td><td width=125><b>Civilstånd:</b> </td><td>".$rad['civil']."</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Län:</b> ".$rad['lan']."</td><td width=20></td><td width=125><b>Sexuell läggning:</b></td><td>".$rad['sexuel']."</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Längd:</b> ".$rad['height']."</td><td width=20></td><td width=125><b>Vikt:</b></td><td>".$rad['weight']."</td>";
$content .= "</tr>";

$content .= "<tr>";
$content .= "<td width=220><b>Ögonfärg:</b> ".$rad['eye_color']."</td><td width=20></td><td width=125><b>Hårfärg:</b></td><td>".$rad['hair_color']."</td>";
$content .= "</tr>";

$content .= "</table>";
$content .= "<table>";
$content .= "<td><br><br></td>";
$content .= "<td width=100><b>Gästbok</b></td><td width=100><b>Film Galleri</b></td><td width=100><b>Bild Galleri</b></td><td width=100><b>Gör favorit</b></td><td width=100><b>Blog</b></td>";
$content .= "</tr>";
$content .= "</table>";
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Undefined variables are vars that you are trying to use for comparisons (like if statements) but they have not been given a value yet. Before you use the var $age, set it to something. I would bet this step may take care of your other errors as well.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Okey, so before the function birthday($age) i'd just take for example $age = "age";
And that would fix my other errors as well?

EDIT: Well i googled why i got the other errors and then i fixed it but it's not displaying anything now :/
Not any content at all and the designs gets wierd, and that only happens if anything is wrong.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No, don't set $age = 'age'. Set it to the null value of the type you expect it to be. If $age will be an int, set $age to 0. If it is a string, set it to ''.

Another thing you can do is check if it is set using isset(). If it is not set, then skip doing what you are doing.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Code: Select all

if(isset($age)) {
$age = '';
}
function birthday ($age){ 
     // assumes $birthdate is in YYYY-MM-DD format 
   list($dob_year, $dob_month, $dob_day) = explode('-', $age); 
   // determine current year, month, and day 
   $cur_year  = date('Y'); 
   $cur_month = date('m'); 
   $cur_day  = date('d'); 
   // either past or on the birthday 
   if($cur_month >= $dob_month && $cur_day >= $dob_day) { 
       $age1 = $cur_year - $dob_year; 
   } 
   // before the birthday 
   else { 
       $age1 = $cur_year - $dob_year; 
   } 
   // and your done 
   return $age1; 
  }
And got back my errors again
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Before i set it to the persons real age, and then i didn't get any errors. But nothing was showed.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
$age = '';

// Do whatever it is that your script does 
// to determine the value of age here

function birthday($age) {
    if(empty($age) || !strstr($age, '-'))
    {
        return false;
    }

    // assumes $birthdate is in YYYY-MM-DD format
    list($dob_year, $dob_month, $dob_day) = explode('-', $age);
    // determine current year, month, and day
    $cur_year  = date('Y');
    $cur_month = date('m');
    $cur_day  = date('d');
    /**
     * This entire routine does absolutely nothing with the if
     * I say you scrap it
     **/
    /* either past or on the birthday
    if($cur_month >= $dob_month && $cur_day >= $dob_day) {
        $age1 = $cur_year - $dob_year;
    }
    // before the birthday
    else {
        $age1 = $cur_year - $dob_year;
    }
    */
    // and your done
    $new_age = $cur_year - $dob_year;
    return $new_age;
}
?>
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Yeah i fixed that now but still, nothing is being displayed on the website :/
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

I fixed it.
Thanks for your help Everah
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Whatdid you do to fix it? Post the solution so we can all benefit from it. And don't forget to add '[Solved] - ' to the beginning of your original post title.
Post Reply