i using switch statements 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 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 wrong"; } function quiz_test(){ $sql = "select q_id, answer_id, time_taken results 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') id = '" . $_session["last_id"] . "'"); } ?>
in case 1 insert values , insert id,and set session.
case 2 select,update statement 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 mysqli, null given in c:\xampp\htdocs\n\exam\exam\docs\insert.php on line 62 warning: mysqli_num_rows() expects parameter 1 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 member function query() on non-object in c:\xampp\htdocs\n\exam\exam\docs\insert.php on line 70
you need add 1 parameter $conn in function quiz_test() works
try below code
<?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 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 wrong"; } function quiz_test($conn){ $sql = "select q_id, answer_id, time_taken results 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') id = '" . $_session["last_id"] . "'"); } ?>
Comments
Post a Comment