如何在switch语句中使用逗号插入值?

时间:2016-01-29 07:11:40

标签: php mysql

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

<?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,并设置为会话。

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

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

2 个答案:

答案 0 :(得分:2)

您需要在函数quiz_test()中添加一个参数$ 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 :(得分:0)

您似乎将两种不同风格的PHP API函数混合在一起。您也可以在致电getResources().getString(R.string.home_page_title); 时,如果它是自动增量列,则不应指定mysqli_insert_id()。我相信你打算沿着这些方向做点什么:

id

@Krish指出的另一个问题是你需要将你的连接变量$sql = "INSERT INTO results (user_id, category_id, q_id, answer_id, time_taken) VALUES ('".$_SESSION['id']."', '$category_id', '$questionid', '$AnswerID', '$timetaken')"; mysqli_query($conn, $sql); $last_id = mysqli_insert_id($conn); echo "Last inserted ID is: " . $last_id; 传递给$conn函数,例如

quiz_test()