duanchi19820419 2019-04-19 23:39
浏览 58

如何从php中的json文件中提取特定数据以在mysql中提交

My json file gets inserted into my php file from jquery

Then I want to use php to send the json contents to mysql

My json file consists of a question answer format/ the name of the question and the answer following it.

How can I pick specific data from the file to insert directly.

This is my jquery code

var surveyJSON = {pages:[{name:"Demographics",elements:[{type:"html",name:"question3",html:"Please answer the following questions about yourself. "},{type:"comment",name:"question4",title:"What is your age?",isRequired:true},{type:"checkbox",name:"Describe your race/ethnicity. Please check all that apply.",title:"Describe your race/ethnicity. Please check all that apply.",valueName:"Describe your race/ethnicity  Please check all that apply",isRequired:true,hasOther:true,choices:["Black/African","American White/Caucasian","Asian or Asian American","Hawaiian or Pacific Islander","Native American or Alaskan Native","Hispanic or Latino/a"],choicesOrder:"asc",otherText:"A race not listed here (describe)",colCount:2},{type:"radiogroup",name:"Are you an international student?",title:"Are you an international student?",isRequired:true,choices:["Yes","No"],colCount:2},{type:"radiogroup",name:"What is your sexual orientation?",title:"What is your sexual orientation?",isRequired:true,hasOther:true,choices:["Gay","Lesbian","Bisexual","Heterosexual/Straight"],choicesOrder:"asc",otherText:"A sexual orientation not listed here (describe)",colCount:2},{type:"radiogroup",name:"What year of school are you in?",title:"What year of school are you in?",isRequired:true,choices:["First year undergraduate","Second year undergraduate","Third year undergraduate","Fourth year undergraduate","Fifth or more year undergraduate","Graduate","Professional (e.g. law, medicine, veterinary, dentistry)"],colCount:2},{type:"checkbox",name:"Since you've been a student at ${e://Field/INSTITUTION}, have you been a member or participated in any of the following? Please check all that apply. ",title:"Since you've been a student at ${e://Field/INSTITUTION}, have you been a member or participated in any of the following? Please check all that apply. ",valueName:"Since you've been a student at ${e://Field/INSTITUTION}, have you been a member or participated in any of the following? Please check all that apply",isRequired:true,hasOther:true,choices:["Honor society or professional group related to your major, field of study","Student government","Fraternity or sorority (pledge or member)","Media organization (e.g., newspaper, radio, magazine)","Intercollegiate athletic team","ROTC/Cadet","Intramural or club athletic team","Political or social action group"],otherText:"Other student organization or group",colCount:2},{type:"radiogroup",name:"Which of the following best describes your living situation?",title:"Which of the following best describes your living situation?",isRequired:true,choices:["On-campus residence hall/dormitory","Off-campus housing non-university sponsored","Other on campus housing (apartment, house)","At home with parent(s) or guardian(s)","Fraternity or sorority house","Other off campus","Off-campus university-sponsored apartment/house"],colCount:2}]},{name:"Sexual Harrasment",elements:[{type:"matrix",name:"question8",startWithNewLine:false,title:"Since you enrolled at ${e://Field/INSTITUTION}, have you been in a situation in which a faculty member, instructor or staff member:",isRequired:true,columns:["Never","Once or twice","Sometimes","Often","Many times"],rows:["Treated you “differently” because of your sex?","Displayed, used, or distributed sexist or suggestive materials?","Made offensive sexist remarks?","Put you down or was condescending to you because of your sex?","Repeatedly told sexual stories or jokes that were offensive to you?","Made unwelcome attempts to draw you into a discussion of sexual matters?","Made offensive remarks about your appearance, body, or sexual activities?","Made gestures or used body language of a sexual nature which embarrassed or offended you?","Made unwanted attempts to establish a romantic sexual relationship with you despite your efforts to discourage it?","Touched you in a way that made you feel uncomfortable?","Made unwanted attempts to stroke, fondle, or kiss you?","Made you feel like you were being bribed with a reward to engage in sexual behavior?","Made you feel threatened with some sort of retaliation for not being sexually cooperative?","Treated you badly for refusing to have sex?","Implied better treatment if you were sexually cooperative?"]}],title:"Sexual Harrasment"},{name:"Sexual Harassment Follow Up",elements:[{type:"html",name:"question1",html:"Think about the situations that happened to you that involved the behaviors you marked on the last screen. Now think about the ONE SITUATION that had the greatest effect on you and answer the following questions. "},{type:"checkbox",name:"question9",title:"The situation involved (check all that apply):",isRequired:true,choices:["Sexist or sexually offensive language, gestures or pictures","Unwanted sexual attention","Unwanted touching","Subtle or explicit bribes or threats"]},{type:"radiogroup",name:"question10",title:"Please describe the gender of the person(s) who committed the behavior.",isRequired:true,hasOther:true,choices:["Man","Woman"],otherText:"Other "},{type:"radiogroup",name:"question11",title:"Please describe the status of the person(s) who committed the behavior.",isRequired:true,hasOther:true,choices:["Faculty member","Staff member","Graduate student instructor"],otherText:"Other "},{type:"radiogroup",name:"question12",title:"Did this happen on campus?",isRequired:true,choices:["Yes","No"],otherText:"Other "},{type:"checkbox",name:"question13",title:"Please tell us how you reacted to the situation (check all that apply). ",isRequired:true,choices:["I ignored the person and did nothing.","I avoided the person as much as possible.","I treated it like a joke.","I told the person to stop.","I reported the person.","I asked someone for advice and/or support."],otherText:"Other "}],visibleIf:"{question8.Displayed, used, or distributed sexist or suggestive materials?} <> \"Never\" or {question8.Implied better treatment if you were sexually cooperative?} <> \"Never\" or {question8.Made gestures or used body language of a sexual nature which embarrassed or offended you?} <> \"Never\" or {question8.Made offensive remarks about your appearance, body, or sexual activities?} <> \"Never\" or {question8.Made offensive sexist remarks?} <> \"Never\" or {question8.Made unwanted attempts to establish a romantic sexual relationship with you despite your efforts to discourage it?} <> \"Never\" or {question8.Made unwanted attempts to stroke, fondle, or kiss you?} <> \"Never\" or {question8.Made unwelcome attempts to draw you into a discussion of sexual matters?} <> \"Never\" or {question8.Made you feel like you were being bribed with a reward to engage in sexual behavior?} <> \"Never\" or {question8.Made you feel threatened with some sort of retaliation for not being sexually cooperative?} <> \"Never\" or {question8.Put you down or was condescending to you because of your sex?} <> \"Never\" or {question8.Repeatedly told sexual stories or jokes that were offensive to you?} <> \"Never\" or {question8.Touched you in a way that made you feel uncomfortable?} <> \"Never\" or {question8.Treated you “differently” because of your sex?} <> \"Never\" or {question8.Treated you badly for refusing to have sex?} <> \"Never\""}]}


function sendDataToServer(survey) {
    survey.sendResult('b48f3bcb-3978-4486-aa30-22cf94092850');
}

var survey = new Survey.Model(surveyJSON);
$("#surveyContainer").Survey({
    model: survey,
    onComplete: sendDataToServer
});


survey.onComplete.add(function (sender, options) {


   var dataString = JSON.stringify(survey.data);
   jQuery.support.cors = true;

    $.ajax({
            type: "GET",
           dataType: "json",
            url: "js/connect.php",
            data: {dataString},
         contentType: "application/json; charset=utf-8",
         success: function( data, textStatus, jQxhr ){
            $('#response pre').html( data );
        },
        error: function( jqXhr, textStatus, errorThrown ){
            console.log( errorThrown );
        }
    }); 


});

I want to be able to select a the answers from my json file and insert them into the proper positions inside the database. The "survey.data" code pulls all questions and answers from the user and gets called from ajax to my php file below "connect.php"

<?php

$json = json_decode(file_get_contents("php://input"));
$categories = $json->categories;

header("Content-Type: application/json; charset=UTF-8")


$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$sql = INSERT INTO `arc`.`results`
(`Result_ID`,
`Age`,
`Race_Ethnicity`,
`International_Student`,
`Sexual_Orientation`,
`Year_Of_School`,
`Participations`,
`Living_Situation`,
`Question1_SH`,
`Question2_SH`,
`Question3_SH`,
`Question4_SH`,
`Question5_SH`,
`Question6_SH`,
`Question7_SH`,
`Question8_SH`,
`Question9_SH`,
`Question10_SH`,
`Question11_SH`,
`Question12_SH`,
`Question13_SH`,
`Question14_SH`,
`Question15_SH`,
`Question16_SH`,
`Time_SH_1stQuestions`,
`F_Up_Question1_SH`,
`F_Up_Question2_SH`,
`F_Up_Question3_SH`,
`F_Up_Question4_SH`,
`F_Up_Question5_SH`,
`Student_ID`,
`Survey_ID`,
`Time_SH_2nQuestions`)


VALUES
(<{Result_ID: }>,
<{Age: }>,
<{Race_Ethnicity: }>,
<{International_Student: }>,
<{Sexual_Orientation: }>,
<{Year_Of_School: }>,
<{Participations: }>,
<{Living_Situation: }>,
<{Question1_SH: }>,
<{Question2_SH: }>,
<{Question3_SH: }>,
<{Question4_SH: }>,
<{Question5_SH: }>,
<{Question6_SH: }>,
<{Question7_SH: }>,
<{Question8_SH: }>,
<{Question9_SH: }>,
<{Question10_SH: }>,
<{Question11_SH: }>,
<{Question12_SH: }>,
<{Question13_SH: }>,
<{Question14_SH: }>,
<{Question15_SH: }>,
<{Question16_SH: }>,
<{Time_SH_1stQuestions: }>,
<{F_Up_Question1_SH: }>,
<{F_Up_Question2_SH: }>,
<{F_Up_Question3_SH: }>,
<{F_Up_Question4_SH: }>,
<{F_Up_Question5_SH: }>,
<{Student_ID: }>,
<{Survey_ID: }>,
<{Time_SH_2nQuestions: }>);



 ?>
'''
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?