javascript fetch()返回状态代码500,并从mysql表进行搜索
我正在尝试使用javascriptjavascript fetch()返回状态代码500,并从mysql表进行搜索,javascript,php,mysql,post,fetch,Javascript,Php,Mysql,Post,Fetch,我正在尝试使用javascriptfetch从MySQL数据库中获取一些元素,如下所示: function fetchPrices(){ fetch('getPricingPlans.php', { method:'POST', body: new URLSearchParams('size=' + size) }) .then(res => res.json()) .then(res => viewPrices
fetch
从MySQL数据库中获取一些元素,如下所示:
function fetchPrices(){
fetch('getPricingPlans.php', {
method:'POST',
body: new URLSearchParams('size=' + size)
})
.then(res => res.json())
.then(res => viewPrices(res))
.catch(e => console.error('Error: ' + e))
}
下面是getPricingPlans.php
,它正在调用另一个php文件livesearch
,它是一个类,它的构造函数连接到数据库,是加载页面时首先调用的
<?php
require_once('livesearch.php');
$size = $_POST['size'];
$con = new livesearch();
$data = $con->getPrices($size);
echo json_encode($data);
?>
PricePlans
是数据库中的我的表,其中包含一个名为price
的列
问题是在执行fetch
时,我遇到以下错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
一些调查结果:
如果我将fetch()
更改为GET
,并在getPricingPlans.php
中回显一个简单字符串(而不进入livesearch.php
),则错误消失
第二,我已经使用不同的
fetch()
将其应用到同一PHP页面中具有相同格式的不同表中。是否有可能第二次调用(实例)livesearch.php就是问题所在 建议参数使用:param格式
$query = "SELECT price FROM PricePlans WHERE size = :size";
$stmt = $this->con->prepare($query);
$stmt->execute([":size" => $size]);
WHERE size=size
?认为应该是WHERE size=:size
第一个大小是表中的列名。第二个大小是在execute()上填充的变量名。你认为我遗漏了什么吗?将查询更改为selectprice FROM PricePlans WHERE size=:size
Typo,更正。是的,这是一个输入错误,成功了!如果添加到SQL中的:
有效,则该问题应以打字错误结束!
$query = "SELECT price FROM PricePlans WHERE size = :size";
$stmt = $this->con->prepare($query);
$stmt->execute([":size" => $size]);