【发布时间】:2020-08-02 00:08:00
【问题描述】:
如何使用 AJAX、JSON 和 PHP 将数据上传到数据库? 这是代码。
AJAX
function saveToTheDB(ratedIndex) {
$.ajax({
url: 'fetch.php',
method: 'POST',
cache: 'false',
dataType: 'json',
data: {
ratedIndex: ratedIndex
},
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error);
}
});
}
PHP
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
require_once 'includes\dbh.inc.php';
$rate = $_POST['ratedIndex'];
if(isset($_GET['userid'])){
if($db->query(" INSERT INTO `recipes_ratings` (`recipe_rating_id`, `recipe_id`, `user_id`, `rating`)
VALUES (null, 3 , 8, '".$rate."')
"))
}
echo json_encode($rate);
}
我做错了什么? 有人可以帮我解决这个问题吗?非常感谢!
编辑
【问题讨论】:
-
如果您提供更具体的错误详细信息会很好。
-
对不起,我加了。