1

I"m attempting to display some data I've sent from ajax to a php file, however for some reason its not displaying it on the page. The way it works it I enter a search term into a input field, and a ajax script post the value to a php script, which return the database value requested back.

error_reporting(E_ALL); 
ini_set('display_errors', '1');


if (isset($_POST['name']) === true && empty($_POST['name']) === false) {
    //require '../db/connect.php';

    $con = mysqli_connect("localhost","root","root","retail_management_db"); 
    $name = mysqli_real_escape_string($con,trim($_POST['name']));    
    $query = "SELECT `names`.`location` FROM `names` WHERE`names`.`name` = {$name}";
    $result = mysqli_query($con, $query);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_array($result)) {
            $loc = $row['location'];
             echo $loc;   
        }//close While loop                            
    } else {
        echo $name . "Name not Found";
    }
}

html form:

<!DOCTYPE html> 
<html>
    <head>
    <meta charset="utf-8">
    <title>Retail Management Application</title>
    </head> 
    <body>             
        Name: <input type="text" id="name">
         <input type="submit" id="name-submit" value="Grab">
            <div id="name-data"></div>
        <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
        <script src="js/global.js"></script>
    </body>
</html>
16
  • What are is the response you are getting from the AJAX call? Commented Apr 23, 2015 at 1:52
  • 2
    You have mysqli_error() in you query stiring. it should be - query = mysqli_query("SELECT `names`.`location` FROM `names` WHERE `names`.`name` = {$name}" ) or die(mysqli_error($con)); Commented Apr 23, 2015 at 1:53
  • Chip Dean, im getting Name not found! Commented Apr 23, 2015 at 1:55
  • and then you are doing a query on your previous query $result = mysqli_query($con, $query);, which is really $result = mysqli_query($con, mysqli_query("SELECT names.location` FROM names WHEREnames.name = {$name}" . mysqli_error($con)); );`. Commented Apr 23, 2015 at 1:55
  • 2
    $name is a string, so it needs to be quoted - ... WHERE `names`.`name` = '{$name}'" Commented Apr 23, 2015 at 2:28

1 Answer 1

2

You're appending a MySQL error result to your query, and you're trying to query a query result, try the following:

$query = "SELECT `names`.`location` FROM `names` WHERE`names`.`name` = '$name'";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {

Edit:

{$name} that is a string and should be quoted instead.

change it to '$name' in the where clause.

Using:

$result = mysqli_query($con, $query) or die(mysqli_error($con));

will provide you with the reason as to why your query failed.

Sign up to request clarification or add additional context in comments.

9 Comments

Hi scrowler, ive made the changes but it seems it still says Name not found even though the records exists in the database.
Have you debugged $_POST in your PHP file to ensure that the value for $name is being correctly passed through?
Yh it shows this: br /> <b>Warning</b>: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in <b>/Applications/MAMP/htdocs/ajax/name.php</b> on line <b>8</b><br /> <br /> <b>Warning</b>: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given in <b>/Applications/MAMP/htdocs/ajax/name.php</b> on line <b>12</b><br />
@YacubAli someone in comments noted about {$name} that should read as '{$name}' either that or just do '$name' or '".$name."' in your where clause. that's the most likely reason now.
@YacubAli you're welcome, glad to have been of help, cheers
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.