简体   繁体   English

使用AJAX将SQL WHERE查询获取到javascript中

[英]Using AJAX to get an SQL WHERE query into javascript

I am trying to pass a javascript variable into an SQL WHERE query and I keep getting null in return. 我试图将javascript变量传递到SQL WHERE查询中,但我不断得到null。

On-click of a button, the buttonClick function is ran: 单击按钮后,将运行buttonClick函数:

<script>
    var var1;
    function buttonClick(elem){
        var1 = elem.src              //this gets the url from the element
        var path = var1.slice(48);   //this cuts the url to img/art/9/1.jpg
        ajax = theAjax(path);
        ajax.done(processData);
        ajax.fail(function(){alert("Failure");});
    }

    function theAjax(path){
        return $.ajax({
            url: 'info.php',
            type: 'POST',
            data: {path: path},
        });
    }

    function processData(response_in){
        var response = JSON.parse(response_in);
        console.log(response);
    }
</script>

Here is the code stored in the info.php file: 这是存储在info.php文件中的代码:

<?php
    $path = $_POST['path'];

    $result3 = mysqli_query("SELECT itemName from images WHERE imgPath='$path'");
    $json = json_encode($result3);

    echo $json
?>

As you can see, once I click the button, the buttonClick() function is ran and a variable stores the image path or src. 如您所见,单击按钮后,将运行buttonClick()函数,并且变量存储图像路径或src。 That path variable is send to theAjax() function where it is passed to the info.php page. 路径变量将发送到theAjax()函数,并在该函数中传递给info.php页面。 In the info.php page, the SQL WHERE query is ran and returned to the processData() function to be parsed and printed in the developer console. info.php页面中,运行SQL WHERE查询并返回到processData()函数以在开发人员控制台中进行解析和打印。 The value printed shows null . 打印的值显示为null

Below is a picture of what I am trying to get from the database: 下面是我试图从数据库中获取的图片: 在此处输入图片说明

1.Check that path is correct or not? 1.检查path是否正确? you can check inside jquery using console.log(path); 您可以使用console.log(path);检查jquery内部console.log(path); or at PHP end by using print_r($_POST['path']); 或在PHP结束时使用print_r($_POST['path']);

2.Your Php code missed connection object as well as record fetching code. 2.您的Php代码缺少连接对象以及记录获取代码。

<?php

    if(isset($_POST['path'])){
        $path = $_POST['path'];

        $conn = mysqli_connect ('provide hostname here','provide username here','provide password here','provide dbname here') or die(mysqli_connect_error());

        $result3 = mysqli_query($conn,"SELECT itemName from images WHERE imgPath='$path'");

        $result = []; //create an array

        while($row = mysqli_fetch_assoc($result3)) {
             $result[] = $row; //assign records to array
        }
        $json = json_encode($result); //encode response
        echo $json; //send response to ajax
    }

?>

Note:- this PHP query code is wide-open for SQL INJECTION . 注意:-此PHP查询代码对于SQL INJECTION是完全开放的。 So try to use prepared statements of mysqli_* Or PDO . 因此,请尝试使用mysqli_*PDO prepared statements

mysqli_query()需要第一个参数作为连接对象。

$result3 = mysqli_query($conn,"SELECT itemName from images WHERE imgPath='$path'"); // pass your connection object here

I think your issue is that you're trying to encode a database resource. 我认为您的问题是您正在尝试对数据库资源进行编码。

Try adjusting your PHP to look like the following: 尝试将PHP调整为如下所示:

<?php
    $path = $_POST['path'];

    $result3 = mysqli_query("SELECT itemName from images WHERE imgPath='$path'");
    $return_data = [];
    while($row = mysqli_fetch_assoc($result3)) {
        $return_data[] = $row;
    }
    $json = json_encode($return_data);

    echo $json
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM