【问题标题】:Save choice in database在数据库中保存选择
【发布时间】:2019-01-19 23:46:49
【问题描述】:

我正在做一个程序,每次用户按下“submit”时,我都会尝试保存数据。我已经设法在我的表“answers”中保存了以下列的数据:exercise_id_fkstudent_iddifficulty_student,但我无法保存列中的数据:choice_answer。每次我尝试保存它时,它都会阻止我保存其他列。我正在尝试将多项选择答案存储在数据库中。你能帮我看看有什么问题吗?

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

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

【问题讨论】:

    标签: php html mysql database


    【解决方案1】:
    <?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);
        }
    }
    }
    ?> 
    

    【讨论】:

    • 我不知道为什么它没有像以前那样使用这个新代码显示图像。但是,它可以根据需要将选择部分存储在数据库中
    • 您在图像中遗漏了一个错误。显示它们
    • 现在它也会显示图像,我对其进行了编辑。如果它有效,那么您可以将其标记为已批准
    • 是的!现在可以了!非常感谢:D 我会标记你的答案。
    • 我很高兴!谢谢!
    【解决方案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);
        }
    }
    }
    ?>
    

    【讨论】:

    • 我正在尝试您的代码,但在从 中删除 3 个“选择”答案时遇到问题,您知道问题出在哪里吗?
    • 我遇到的错误是这个:解析错误:syntax error, unexpected '' method='' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'
    • 你要删除的把你要删除的代码发给我,可能是PHP代码的结尾或开头
    • &lt;?php $row = $result-&gt;fetch_assoc(); echo '&lt;h1&gt;' . $row["exercise_id"]. ". " . $row["title"] . '&lt;/h1&gt;' . "&lt;br&gt;" . '&lt;p&gt;' . $row["text"] . '&lt;/p&gt; &lt;img width="603" height="auto" src="' . $row["image_path"] . '"&gt;&lt;br&gt;&lt;br&gt;; /*var_dump($id)*/ ?&gt;
    • 我发布了您的代码以在 PHPSTORM 中尝试它,它有一些错误,我不知道如何解决它们。你能帮帮我吗?
    猜你喜欢
    • 2016-04-08
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    相关资源
    最近更新 更多