[英]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.