CodeTalk
CodeTalk

Reputation: 3667

Error with Array getting Empty Set

Updated Problem: print_r($_POST); in the php file outputs after I type something in the box.

Array
(
    [q] => running
)

print json_encode($jsonArray); however is printing []

The php which queries Interest table for the bound parameter

    $interestValue = $_POST['interestVal']; //////
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
    $sth = $dbh->prepare(
'SELECT interestID, interestVal FROM Interest WHERE interestVal = ?');
    $sth->bindParam(1, $interestValue);
    $sth->execute();
    $jsonArray = array();
    while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
     $jsonArray[] = array(
      'ID' => $result['interestID'], 
          'Value' => $result['interestVal']); 
    }

    print_r($_POST);

    print json_encode($jsonArray); // json encode that array 

HTML

<input id="interest" name="interest" value="What are your interests?" />

JS

//Get interests
        $(document).ready(function() {
            $("input#interest").tokenInput("../../src/php/registration/interest/getInterest.php");
        });

The JS of the framework(http://loopj.com/jquery-tokeninput/)

(function ($) {
// Default settings
var DEFAULT_SETTINGS = {
    // Search settings
    method: "POST",
    contentType: "json",
    queryParam: "interestVal",
    searchDelay: 300,
    minChars: 1,
    propertyToSearch: "name",
    jsonContainer: null,

    // Display settings
    hintText: "Type in a search term",
    noResultsText: "No results",
    searchingText: "Searching...",
    deleteText: "&times;",
    animateDropdown: true,

    // Tokenization settings
    tokenLimit: null,
    tokenDelimiter: ",",
    preventDuplicates: false,

    // Output settings
    tokenValue: "name", // orig "id"
}

Why is this array empty??

Upvotes: 0

Views: 124

Answers (1)

kitti
kitti

Reputation: 14844

Don't use mysql_real_escape_string with prepared statements - binding parameters takes the user data out of the SQL code, so there are no SQL injection vulnerabilities. More importantly, mysql_real_escape_string only works AFTER you connect to the DB, not before, which is turning your value into false.

Upvotes: 1

Related Questions