sql >> データベース >  >> RDS >> Mysql

switchステートメントにコンマで値を挿入する方法

    関数quiz_test()に1つのパラメーター$ connを追加する必要があります。そうすれば、機能します

    以下のコードを試してください

    <?php
    session_start();
    ?>
    <?php
    include('config1.php');
    $category_id = 1;
    $AnswerID = $_POST['AnswerID'];
    $questionid = $_POST['questionid'];
    $timetaken = $_POST['timetaken'];
    $limit = $_POST['limit'];
    
    echo "$limit";
    
    $bd = "$limit";
    switch ($bd) {
    case"1":
    $sql = "INSERT INTO results (id, user_id, category_id, q_id, answer_id, time_taken)
    VALUES (',', '".$_SESSION['id']."', '$category_id', '$questionid', '$AnswerID', '$timetaken')";
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
     $last_id = mysqli_insert_id($conn);
        echo "Last inserted ID is: " . $last_id;
    // Set session variables
    $_SESSION["last_id"] = "$last_id";
    break;
    case"2":
    quiz_test($conn);
    break;
    case"3":
    quiz_test($conn);
    break;
    case"4":
    quiz_test($conn);
    break;
    case"5":
    quiz_test($conn);
    unset($_SESSION['last_id']);
    break;
    default:
            echo "something is wrong";
    }
    function quiz_test($conn){
    $sql = "SELECT q_id, answer_id, time_taken FROM results WHERE id='" . $_SESSION["last_id"] . "'";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
      $qid=$rows['q_id'];
      $ans=$rows['answer_id'];
      $time=$rows['time_taken'];
    }
    }
    $conn->query("update results set q_id =('$questionid','$qid'),answer_id = ('$AnswerID','$ans'),time_taken=('$timetaken','$time') where id = '" . $_SESSION["last_id"] . "'");
    }
    ?>
    


    1. PDOを使用してテーブルに大量の変数を挿入します

    2. テーブル作成時のSQL整数範囲

    3. ClusterControl:新しいクエリモニターの概要

    4. 127.0.0.1でmysqlに接続できません