www.webdeveloper.com
Results 1 to 4 of 4

Thread: Update db function

  1. #1
    Join Date
    Mar 2004
    Posts
    121

    Update db function

    Greetings.

    I'm writing my own banner management system. As a result, I will be updating a db to calculate impressions/clicks. I will need to do this several times throughout the site, therefore, I thought I would create a function to perform the update and just call the function.

    Below is my function and how I'm calling it. For some reason, it isn't executing the update. Can someone help?

    Code:
    <?php
    
    function banner_stats($banner_id, $banner_location) {
    	$update_banner = "INSERT INTO banner_stats (location, impressions, banner_id) VALUES('$banner_location',impressions + 1, $banner_id)"; 
    	mysql_query($update_banner, $connection) or die(mysql_error()); 
    	}
    ?>
    ...and here is the call...

    [code]
    <?php banner_stats('home page', '1234']); } ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,354
    For more debug info, try adding the following to the beginning of the script...
    PHP Code:
    <?php
    ini_set
    ("display_errors"1);
    error_reporting(E_ALL);
    ...and add some more error-checking to the mysql_query:
    PHP Code:
    function banner_stats($banner_id$banner_location) {
       
    $update_banner "INSERT INTO banner_stats (location, impressions, banner_id) VALUES('$banner_location',impressions + 1, $banner_id)"
       
    mysql_query($update_banner$connection) or die(mysql_error() . "<br>Query: $update_banner");
       if(
    mysql_affected_rows() == 0)
       {
          
    user_error("Now rows updated. Query: $update_banner"E_USER_WARNING);
       }

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2004
    Posts
    121
    Here is what the error reports:

    Notice: Undefined variable: .... on line 13.

    Line 13 is mysql_query($update_banner, $connection) or die(mysql_error())

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,354
    The $connection variable is not defined in your function, so you either need to (a) pass it as another function parameter, (b) declare it as global within the function, or (c) just leave it out of the mysql_query() call if you only have one connection established in your script (and thus can just let it use that one by default).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles