将选择保存在数据库中

时间:2018-08-13 09:20:27

标签: php html mysql database

我正在做一个程序,每次用户按下'System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "") '时,我都会尝试保存数据。我已经设法在表'submit'中保存以下列的数据:answersexercise_id_fkstudent_id,但是我无法保存该列中的数据: difficulty_student。每次尝试保存它时,它都会阻止我保存其他列。我正在尝试在数据库中存储多项选择答案。你能帮我看看有什么问题吗?

这是我的程序,我试图从多项选择答案中将它们存储在列“ choice_answer”中:

choice_anser

2 个答案:

答案 0 :(得分:3)

<?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();
?>

<h1><?php echo $row["exercise_id"] ?></h1><br/>
<p><?php echo $row["text"] ?></p>
<img width="603" height="auto" src="<?php $row["image_path"]?>"><br/><br/>
<form action='' method='post'>
    <input type="radio" name="choice" value= "1" /><img src="<?php echo $row["image_path_A"]; ?>"/><br>
    <input type="radio" name="choice" value= "2" /><img src="<?php echo $row["image_path_B"] ; ?>"><br>
    <input type="radio" name="choice" value= "3" /><img src="<?php echo $row["image_path_C"]; ?>"><br>


<!-- var_dump($id) -->


    <br><br><br>

    <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);
    }
}
}
?> 

答案 1 :(得分:2)

如果要在单击“提交”时保存所有内容,则必须使用单个表格。

<?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>




/*var_dump($id)*/
?>

    <br><br><br><!--- Select difficulty --->

    <p2>Select difficulty level:</p2>

    <form action='' method='post'>
 <input type="radio" name="choice" value= "1" /><img src="<?php $row["image_path_A"];?>"/><br>
    <input type="radio" name="choice" value= "2" /><img src="<?php $row["image_path_B"];?>"><br>
    <input type="radio" name="choice" value= "3" /><img src="<?php $row["image_path_C"];?>"><br>

    <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);
    }
}
}
?>