保存数据库中的选择 [英] Save choice in database
问题描述
我正在做一个程序,每次用户按提交时我都会尝试保存数据。我已设法在表格答案中保存列的数据: exercise_id_fk , student_id 和 difficulty_student ,但我无法保存列中的数据: choice_answer 。每次我尝试保存它,它都会阻止我保存其他列。我正试图在数据库中存储多项选择答案。你能帮我看看有什么不对吗?
我尝试了什么:
这是我的程序,我试图在多选答案的choice_anser列中存储:
<?php
//开始会话
session_start();
$ servername =localhost;
$ username =root;
$ password =;
$ dbname =project;
$ conn = new mysqli($ servername,$ username,$ password,$ dbname);
/ * echo * / $ id = $ _ GET ['id'];
$ sql =SELECT * FROM exercise其中exercise_id ='$ id';
$ result = $ conn-> query($ sql); / *检查连接* /
?>
< div id =centered_Bclass =header>
<?php
$ row = $ result-> fetch_assoc();
echo'< h1>'。 $行[ exercise_id。 。 $ row [title]。 '< / h1>'。 <峰; br> 中。 '< p>'。 $ row [text]。 < / p为H. < img width =603height =autosrc ='。$ row [image_path]。'>< br>< br>
< form method =post>
< input type =radioname =choicevalue =1/>< img src ='。$ row [image_path_A]。'/>< br>
< input type =radioname =choicevalue =2/>< img src ='。$ row [image_path_B]。'>< br>
< input type =radioname =choicevalue =3/>< img src ='。$ row [image_path_C]。'>< br>' ;
echo'< / form>';
/ * var_dump($ id)* /
?>
< br>< br>< br><!---选择难度--->
< p2>选择难度级别:< / p2>
< form action =''method ='post'>
< select name =chooseid =choose>>
< option value =1<?php if($ row [difficulty] ==1){echoselected; }?>大于1< /选项>
< option value =2<?php if($ row [difficulty] ==2){echoselected; }?>大于2< /选项>
< option value =3<?php if($ row [difficulty] ==3){echoselected; }?> →3< /选项>
< option value =4<?php if($ row [difficulty] ==4){echoselected; }?> →4< /选项>
< option value =5<?php if($ row [difficulty] ==5){echoselected; }?>大于5< /选项>
< / select>
< br>< br>< br><!---按钮--->
<! - < button class =buttonSubmit>提交< / button> - >
< input type =submitname =submitvalue =Submit>
< button class =buttonNext>下一个问题< / button>
< / form>
< / div><!--- cubic_B div的结尾--->
<?php
if(isset($ _ POST ['submit'])){
$ user_id = $ _SESSION [ 'USER_ID'];
$ user_check_query =SELECT * FROM users WHERE id ='$ user_id';
if(isset($ _ POST ['choice'])){
if(isset($ _ POST ['choose'])){
$ choice_answer = $ _ POST ['choice'] ;
$ difficulty = $ _ POST ['choose'];
// * / $ user_id = $ _SESSION ['user_id']; * /
$ query =INSERT INTO答案(exercise_id_fk,student_id,difficulty_student,choice_answer)VALUES('$ id','$ user_id','$ difficulty','$ choice_answer');
$ sql = mysqli_query($ conn,$ query);
}
}
}
?>
servername =localhost;
username =root;
password =;
I'm doing a program where I try to save data every time the user press 'submit'. I have managed to save in my table 'answers' the data of the columns: exercise_id_fk, student_id and difficulty_student, but I can not save the data from the column: choice_answer. Every time I try to save it, it stops me from saving the other columns. I'm trying to store in the database multiple choice answers. Can you help me see what is wrong?
What I have tried:
This is my program where im trying to store in the column 'choice_anser' from the multiple choice answers:
<?php // Start the session session_start(); $servername = "localhost"; $username = "root"; $password = ""; $dbname = "project"; $conn = new mysqli($servername, $username, $password, $dbname); /*echo*/ $id=$_GET['id']; $sql = "SELECT * FROM exercises where exercise_id='$id'"; $result = $conn->query($sql); /*Check connection*/ ?> <div id="centered_B" class="header"> <?php $row = $result->fetch_assoc(); echo '<h1>' . $row["exercise_id"]. ". " . $row["title"] . '</h1>' . "<br>" . '<p>' . $row["text"] . '</p> <img width="603" height="auto" src="' . $row["image_path"] . '"><br><br> <form method="post" > <input type="radio" name="choice" value= "1" /><img src="' . $row["image_path_A"] . '"/><br> <input type="radio" name="choice" value= "2" /><img src="' . $row["image_path_B"] . '"><br> <input type="radio" name="choice" value= "3" /><img src="' . $row["image_path_C"] . '"><br>'; echo '</form>'; /*var_dump($id)*/ ?> <br><br><br><!--- Select difficulty ---> <p2>Select difficulty level:</p2> <form action='' method='post'> <select name="choose" id="choose">> <option value="1" <?php if($row["difficulty"]=="1") { echo "selected"; } ?> >1</option> <option value="2" <?php if($row["difficulty"]=="2") { echo "selected"; } ?> >2</option> <option value="3" <?php if($row["difficulty"]=="3") { echo "selected"; } ?> >3</option> <option value="4" <?php if($row["difficulty"]=="4") { echo "selected"; } ?> >4</option> <option value="5" <?php if($row["difficulty"]=="5") { echo "selected"; } ?> >5</option> </select> <br><br><br><!--- Button ---> <!-- <button class="buttonSubmit" >Submit</button>--> <input type="submit" name="submit" value="Submit"> <button class="buttonNext" >Next Question</button> </form> </div><!--- end of centered_B div ---> <?php if (isset($_POST['submit'])) { $user_id = $_SESSION['user_id']; $user_check_query = "SELECT * FROM users WHERE id='$user_id'"; if(isset($_POST['choice'])){ if(isset($_POST['choose'])){ $choice_answer=$_POST['choice']; $difficulty=$_POST['choose']; // */$user_id = $_SESSION['user_id'];*/ $query = "INSERT INTO answers (exercise_id_fk, student_id, difficulty_student, choice_answer) VALUES ('$id','$user_id', '$difficulty', '$choice_answer')"; $sql=mysqli_query($conn,$query); } } } ?>
servername = "localhost";
username = "root";
password = "";
这篇关于保存数据库中的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!