繁体   English   中英

PHP脚本未返回查询,但MySql查询有效

[英]PHP script not returning query, but the MySql query works

我的最终目标是让js脚本将php查询的结果作为json返回,以便我可以使用它进行恶意操作。

我有要使用的MySql查询,当我在Workbench中对其进行测试时,它肯定可以正常工作,但是当我在PHP脚本中对其进行尝试时,什么也没有返回。 PHP中的当前查询只是一个占位符,用于测试数据交换。

我手动将一些东西放到数组(searchResults)中,并且得到返回,但是NOTHING来自执行该脚本的PHP脚本。

我也知道这可能不是最安全或最有效的代码,我只是想让它在此时工作。

<?php
    // Include your database creds and login to the db
    require_once 'login_karavites.php';
    $db = mysqli_connect($db_hostname, $db_username, $db_password);

    // Handle the input/request.
    $searchString_UNSAFE = $_POST['eName']; // change that, obviously

    // Bare minimum sanitation to prevent injection.
    $searchString = $db->escape_string($searchString_UNSAFE);

    // Construct the SQL query
    $sql = "SELECT * FROM `Halls` WHERE hall_name = 'Rose Ballroom'";

    // Do the database lookup.
    $result = $db->query($sql);

    // Create empty array to hold our results (to be sent back to the browser).
    $searchResults = array();
    $searchResults[]="wow";
    // If we had results, put them into that array
    if ($result->num_rows > 0) {

        // This loop will retrieve every row from that result set
        while ($row = $result->fetch_assoc()) {

            // From each row, just take the 'event_name' field.
            $searchResults[] = $row['hall_name'];

        }

    }

    // Done with the db, now we just have to send the results back to the browser.
    $db->close();

    // Send the correct content-type header.
    // This ensures that jQuery automatically converts the response into an 
    // array or object, rather than just treating it like a block of text.
    // Must be the FIRST thing the PHP script outputs, or it will choke.
    header('Content-type: application/json');

    // Output the data.
    echo json_encode($searchResults);

?>

js脚本。

$(document).ready(function() {
    // All this stuff runs as soon as the page is fully loaded

    // Attach a function to the Submit action on #eventForm
    $('#eventForm').submit(function() {

        // Submit the form via AJAX
        $(this).ajaxSubmit({

            // Attach a function to the "the PHP script returned some results" event
            success: function(response, status, xhr, $form){
                // I am assuming that this is your data format, for example:
                // { "searchResults": [ "result1", "result2", "result3" ] }
                // I am also assuming that you want your results in div#results
                $('div#results').html(""); // Clear it out of anything that's already there.
                console.log(response);
                for (i in response['searchResults']) {
                    $('div#results').append( response['searchResults'][i] );
                }
            },

            // Give up if PHP doesn't answer in 3 seconds
            timeout: 3000,

            // Path to the PHP file we want to send this to
            url: 'phpdata/eventsData.php'
        });

        // Make sure the browser does NOT proceed to submit the form again,
        // the old fashioned way (full page reload).
        return false;
    });
});

我的问题的哪一部分是:

  • 我愚蠢的是从未将表单方法设置为原始表单,因此POST实际上并没有通过。

  • 我在PHP文件中设置了错误的MySql连接。

我自己的笔记,请确保您检查所有设置。

暂无
暂无

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

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