WordPress.org

Ready to get started?Download WordPress

Forums

Passing get_current_user_id from one table to another... (2 posts)

  1. Jim R
    Member
    Posted 1 year ago #

    I'd like my Users to have the ability to enter the names of basketball players into my custom data table, where I already have about 1,500 that are otherwise entered by me. They will have to be registered and logged in to use this form.

    My Form is on a Page via a php INCLUDE (I use a lot of Includes so I can more easily manipulate code that I bring into my WP site). The Form posts to a php to handle the database entry. Everything works until I try to include the User_ID.

    I get this error:

    Fatal error: Call to undefined function get_current_user_id() in /home/jwrbloom/public_html/resources/playerEntry/dbEnter.php on line 6

    Other than the php tags, below the entire code from the page.

    $wp_userID = get_current_user_id();          <=   Line 6
    
    if(!$con = mysql_connect("localhost","###","###")) {
       die("Could not connect to database: ".mysql_error());
    }
    
    mysql_select_db("jwrbloom_hhr", $con);
    
    $nameFirst = $_POST['nameFirst'];
    $nameLast = $_POST['nameLast'];
    $cityHome = $_POST['cityHome'];
    $school = $_POST['school'];
    $year = $_POST['year'];
    $position = $_POST['position'];
    $feet = $_POST['feet'];
    $inches = $_POST['inches'];
    $level = $_POST['level'];
    
    /*
    search for existing row
    */
    $sql = "SELECT id FROM a_playerRank WHERE nameFirst='".mysql_real_escape_string($nameFirst)."' AND nameLast='".mysql_real_escape_string($nameLast)."' AND school='".mysql_real_escape_string($school)."'";
    if(!$result = mysql_query($sql)) {
       die(mysql_error()."<br />Query: ".$sql);
    }
    if(mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      /*
      This tells us we already have him.
      */
    {echo 'This player is already in the database.  Hit back button if you need to enter another player.';  
    
       }
    }
    else {
      /*
      insert new row
      */
      $sql = "INSERT INTO a_playerRank SET
                  grouping='4', 
    
    			  nameFirst='".mysql_real_escape_string($nameFirst)."',
                  nameLast='".mysql_real_escape_string($nameLast)."',
    			  city='".mysql_real_escape_string($cityHome)."',
    			  school='".mysql_real_escape_string($school)."',
    			  year='".mysql_real_escape_string($year)."',
                  position='".mysql_real_escape_string($position)."',
                  feet='".mysql_real_escape_string($feet)."',
                  inches='".mysql_real_escape_string($inches)."',
    			  level='".mysql_real_escape_string($level)."'";
       if(!$result = mysql_query($sql)) {
          die(mysql_error()."<br />Query: ".$sql);   
    
    // Email to me
    $message =  $_POST['nameFirst'] . " " . $_POST['nameLast'] ." was added to the database.\n";
    $message .=  $_POST['feet'] . "'" . $_POST['inches'] ."\", " . $_POST['year'] . "; " .  $_POST['school'] ."\n";
    $message .= $_POST['position']."\n";
    $message .=  $_POST['email'];
    
    // In case any of our lines are larger than 70 characters, we should use wordwrap()
    $message = wordwrap($message, 70);
    
    $headers = "From: HHR Database Entry <noreply@hoosierhoopsreport.com>";
    
    // Send
    mail('basketball@hoosierhoopsreport.com', 'Player database addition', $message, $headers);
    
    /*
    redirect user
    */
    header("Location:http://hoosierhoopsreport.com/dbpe");
    
    }
    }

    I tried using a Global call. I've tried a require_once at the beginning. I've tried moving get_current_user_id to a few different places.

  2. gicolek
    Member
    Posted 1 year ago #

    Hey Jim R,

    Could you please outline your site architecture? You're getting that call because WordPress built in functions do not exsit within that file's scope.

    This is most probably caused by the fact, that you're running the function outside WordPress.

    There are two options

    1. Would you like to use that code within a WordPress Theme?
    2. Would you like to use that code outside of a WordPress Theme, having an access to the current user? (although it makes not much sense as get_current_user_id(); works for the currently logged in user)

    My bet is that your posting a form to the php file lying outside WordPress.

    The simplest solution is to post the form to the currently browsed page and have it call a function running your code, lying within the functions.php

    A page could look like that:

    get_header();
    handle_mysql(); ?>
    
    <!-- content -->
    
    <?php get_sidebar(); ?>
    <?php get_footer() ;?>

    Where handle_mysql() would be a function found within functions.php (or included within that file)

    Anyway your code can be simplified::

    - use WP $wpdb class instead of custom SQL queries http://codex.wordpress.org/Class_Reference/wpdb
    - use WordPress' data validation functions http://codex.wordpress.org/Data_Validation

    From the headers sent I see that the code wasn't designed to be used with an ajax call.

    Regards,
    Rafal

Topic Closed

This topic has been closed to new replies.

About this Topic