I 다음 스키마 있습니다JSONKit - MySQL 데이터베이스에 PHP로 JSON 문자열을 구문 분석 내 데이터베이스에서
답변 :
answerId(PK) auto_inc
answer
questionId
내 PHP 파일에 다음 JSON 문자열을 전달하고있다 :
을이
[{"answer":"bnk","questionId":"1"},{"answer":"1","questionId":"2"},{"answer":"b n","questionId":"3"},{"answer":"3","questionId":"4"},{"answer":"rgb","questionId":"5"},{"answer":"No","questionId":"6"},{"answer":"0","questionId":"7"},{"answer":"0","questionId":"8"},{"answer":"0","questionId":"9"},{"answer":"0","questionId":"10"},{"answer":"0","questionId":"11"},{"answer":"0","questionId":"12"},{"answer":"0","questionId":"13"},{"answer":"0","questionId":"14"},{"answer":"3","questionId":"18"},{"answer":"nko","questionId":"19"},{"answer":"hhkl","questionId":"15"},{"answer":"2","questionId":"16"},{"answer":"vnlf hugg","questionId":"17"}]
은 $ _POST [ '답변']에 게시물 요청을 통해 캡처 :
if(isset($_POST['submitanswer'])){
$dbh = connect();
$user = $_POST['user'];
$entry = $_POST['entryId'];
$answers = $_POST['answers'];
$answers = json_decode($answers); //decode JSON answers
//for loop to iterate through answers ans insert new row into database
}
,536,
답변 배열을 반복하고 답변 테이블에 새 행을 삽입하려면 어떻게해야합니까? 같은
뭔가 :이 코드는 제대로 작동되지 않으면
foreach($answers as $row){
$query = "INSERT INTO Answers (answer, questionId) VALUES ($row['answer'], $row['questionId'])";
mysql_query($query);
}