通过程序将数据保存或存储在数据库中

时间:2018-08-10 10:21:19

标签: php mysql database store

我正在编写一个代码,用户必须在其中注册才能执行一些练习。每个练习都有一个select t1.ID, min(t1.Date1) as date1 ,min(t2.Date2) as date2 ,t2.Remarks from Table1 t1 inner join Table2 t2 on t1.ID=t2.ID where t2.Remarks='Accepted' group by t1.ID,t2.Remarks ,用户可以选择该练习的难度级别。

我想将'id''exercise_id''user_id'保存在数据库表中。到目前为止,我已经设法将'difficulty''answers'保存在名为'exercise_id'的表中,但是我无法保存'difficulty'

在选择练习和难度级别之前,用户必须使用其用户名进行注册,并且每次注册时,'user_id'都会创建并增加(我已经设法将其保存在{{1}中) }表)。我想做的是保存表'user_id'中当前记录的'users'。 这就是我到目前为止所拥有的...您能帮我吗?

我试图获取从表“ 'user_id'”记录的“ 'answers'”并将其存储在用户按下user_id时的表“ users”中

这是文件:ChooseExercise.php

answers

这是文件:server.php

submit

数据库表

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


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


/*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']; /*ERROR: Notice: Undefined index: user_id */
   $user_check_query = "SELECT * FROM users WHERE id='$user_id'";


    if(isset($_POST['choose'])){
        $difficulty=$_POST['choose'];
//      */$user_id = $_SESSION['user_id'];*/
        $query = "INSERT INTO answers (exercise_id_fk, student_id, difficulty_student) VALUES ('$id','$user_id', '$difficulty')";
        $sql=mysqli_query($conn,$query);

    }
}

?>

<div id="centered_C" class="header">
    <!--- Solution --->

        <button onclick="solutionFunction()" class="button_Solution" >Solution</button>



    <div id="solution" style="display: none;">

        <img src="<?php echo $row["solution_path"]; ?>" >

    </div>


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

<!--------- Solution -------------->
<script>
    function solutionFunction() {
        var x = document.getElementById("solution");
        if (x.style.display === "none") {
            x.style.display = "block";
        } else {
            x.style.display = "none";
        }
    }
</script>

1 个答案:

答案 0 :(得分:1)

编辑:对于更新的问题

header('location: index.php');上进行注册和登录之前:

1)在 REGISTER USER 上,将用户ID保存在SESSION中:

// Finally, register user if there are no errors in the form
if (count($errors) == 0) {

    $query = "INSERT INTO users (username) VALUES('$username')";
    mysqli_query($db, $query);
    $_SESSION['username'] = $username;
    $_SESSION['user_id'] = mysqli_insert_id($db); //returns id from last query

    header('location: index.php');
}

}

2)在登录用户上,执行相同的操作,但从数据库中检索:

if (count($errors) == 0) {
    $password = MD5($password); /*security reasons*/
    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
        $_SESSION['username'] = $username;
        $_SESSION['user_id'] = $results->fetch_object()->id;
        header('location: index.php');
    }else {
        array_push($errors, "Wrong username/password combination");
    }
}