如何使用 AJAX、JSON 和 PHP 将数据上传到数据库?这是代码。
阿贾克斯
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);
}
我做错了什么?有人可以帮我解决这个问题吗?非常感谢你!
慕莱坞森
扬帆大鱼
随时随地看视频慕课网APP