第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 JSON 將數(shù)據(jù)上傳到數(shù)據(jù)庫

使用 JSON 將數(shù)據(jù)上傳到數(shù)據(jù)庫

PHP
慕萊塢森 2022-12-30 17:05:29
如何使用 AJAX、JSON 和 PHP 將數(shù)據(jù)上傳到數(shù)據(jù)庫?這是代碼。阿賈克斯    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);            }        });    }PHPif($_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);}我做錯了什么?有人可以幫我解決這個問題嗎?非常感謝你!
查看完整描述

1 回答

?
揚帆大魚

TA貢獻1799條經(jīng)驗 獲得超9個贊

由于響應(yīng)點你有語法錯誤,我在這里重構(gòu)了你的代碼以便工作。


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."')")) {

            // implementation if query is successful

        } 

    }


    echo json_encode($rate);

}

JFYI:避免直接將輸入變量放入查詢中,您應(yīng)該使用Prepared Statements。



查看完整回答
反對 回復(fù) 2022-12-30
  • 1 回答
  • 0 關(guān)注
  • 77 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號