Click here to Skip to main content
15,746,534 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

// Start the session
$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">

$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>';


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

    <br><br><br><!--- Button --->

<!--        <button class="buttonSubmit" >Submit</button>-->
        <input type="submit" name="submit" value="Submit">
        <button class="buttonNext" >Next Question</button>

</div><!--- end of centered_B div --->


if (isset($_POST['submit'])) {
    $user_id = $_SESSION['user_id'];
   $user_check_query = "SELECT * FROM users WHERE id='$user_id'";
//      */$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')";
Updated 12-Aug-18 23:59pm

1 solution

$query = "INSERT INTO answers (exercise_id_fk, student_id, difficulty_student, choice_answer) VALUES ('$id','$user_id', '$difficulty', '$choice_answer')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
Share this answer
Member 13933133 13-Aug-18 6:35am    
Okey, I'll take it into account. Can you help me with my question?
Patrice T 13-Aug-18 6:41am    
Use Improve question to update your question.
So that everyone can pay attention to your new code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900