SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    this php error killing me .............

    Hai folks,

    do you see any syntax error in this piece of code?

    Code:
    <?php 
    
    include("../includes/connection.php");
    include("../includes/set_time_zone.php");
    
    $log_username=$_SESSION['username']; //correctly pirints the username currently loged in when echo
    $log_dt=date("Y/m/d h:i:s A"); //correctly prints the time and date when echo
    
    function reglog($event){
    	$query = "INSERT into logs VALUES ('','$event','$log_username','$log_dt')"; 
    	if ($result=mysql_query($query) or die (mysql_error()));
    }
    ?>
    it says

    Undefined variable: log_username....
    Undefined variable: log_dt

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Your error is in the function. $log_username and $log_dt are NOT part of the function scope. You passed in $event but you forgot to pass in $log_username and $log_dt.

    http://www.php.net/manual/en/languag...bles.scope.php
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Your error is in the function. $log_username and $log_dt are NOT part of the function scope. You passed in $event but you forgot to pass in $log_username and $log_dt.

    http://www.php.net/manual/en/languag...bles.scope.php
    Done!!!! Thanks a bunch logic_earth!!!!

    Code:
    function reglog($event){
        $log_username=$_SESSION['username'];
        $log_dt=date("Y/m/d h:i:s A");
    	$query = "INSERT into logs VALUES ('','$event','$log_username','$log_dt')"; 
    	if ($result=mysql_query($query) or die (mysql_error()));
    }

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to include session_start() and if $_SESSION['username'] is not set you have to set it.

    Example:
    PHP Code:
    <?php
       session_start
    ();
       include(
    "../includes/connection.php");
       include(
    "../includes/set_time_zone.php");

       if (!isset(
    $_SESSION['username']))
       {
           
    $_SESSION['username'] = "log_username";
       }
       
    $log_username=$_SESSION['username']; //correctly pirints the username currently loged in when echo
       
    $log_dt=date("Y/m/d h:i:s A"); //correctly prints the time and date when echo

       
    function reglog($event){
           
    $query "INSERT into logs VALUES ('','$event','$log_username','$log_dt')"
           if (
    $result=mysql_query($query) or die (mysql_error()));
       }
    ?>
    I'm not sure why you getting error for $log_dt.

    EDIT: If reglog() was called it would generate those errors as logic_earth stated.

  5. #5
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tom8 View Post
    You need to include session_start() and if $_SESSION['username'] is not set you have to set it.

    Example:
    PHP Code:
    <?php
       session_start
    ();
       include(
    "../includes/connection.php");
       include(
    "../includes/set_time_zone.php");

       if (!isset(
    $_SESSION['username']))
       {
           
    $_SESSION['username'] = "log_username";
       }
       
    $log_username=$_SESSION['username']; //correctly pirints the username currently loged in when echo
       
    $log_dt=date("Y/m/d h:i:s A"); //correctly prints the time and date when echo

       
    function reglog($event){
           
    $query "INSERT into logs VALUES ('','$event','$log_username','$log_dt')"
           if (
    $result=mysql_query($query) or die (mysql_error()));
       }
    ?>
    I'm not sure why you getting error for $log_dt.

    EDIT: If reglog() was called it would generate those errors as logic_earth stated.
    Thanks tom for the additional help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •