如何在开关语句中用逗号插入值? [英] how to insert values with comma?in switch statement

查看:57
本文介绍了如何在开关语句中用逗号插入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用switch语句插入逗号值:

i am using switch statements to insert comma values:

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

    case"3":
        quiz_test();
        break;

    case"4":
        quiz_test();
        break;

    case"5":
        quiz_test();
        unset($_SESSION['last_id']);
        break;

    default:
        echo "something is wrong";
}
function quiz_test(){
    $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中,插入值并获取插入ID,然后将其设置为会话.

In case 1 insert values and get insert id,and set into session.

案例2的选择,更新语句不起作用.我遇到以下错误:

Case 2 select,update statement are not working.i got following errors:

Notice: Undefined variable: conn in C:\xampp\htdocs\N\exam\exam\DOCS\Insert.php on line 62

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\N\exam\exam\DOCS\Insert.php on line 62

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\N\exam\exam\DOCS\Insert.php on line 63

Notice: Undefined variable: conn in C:\xampp\htdocs\N\exam\exam\DOCS\Insert.php on line 70

Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\N\exam\exam\DOCS\Insert.php on line 70

推荐答案

您需要在函数quiz_test()中添加一个参数$ conn,然后它将起作用

You need to add one parameter $conn in your function quiz_test() then it will works

尝试下面的代码

<?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"] . "'");
}
?>

这篇关于如何在开关语句中用逗号插入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆