The example in this article describes the implementation method of the PHP question answering application interface. Share it with everyone for your reference. The specific implementation method is as follows:
session_cache_expire(60);
session_start();
if(!isset($_SESSION['zaszh_user_id'])){
echo json_encode(array('status'=>'error','msg'=>'Connection timed out, please reopen the page.'));
exit;
}
$user_id = $_SESSION['zaszh_user_id'];
// $user_id = 1; // For testing
// Get 5 random questions
$question_id = array();
while(sizeof($question_id)<5){
$num_rand = mt_rand(1,114);
If(!in_array($num_rand, $question_id))
$question_id[] = $num_rand;
}
require('connect_database.php');
//Number of answers
$mysqli->query("update zaszh_user set answer_surplus=answer_surplus-1 where id={$user_id} and answer_surplus>0");
if($mysqli->affected_rows){
// There are remaining times
}else{
// No remaining times
echo json_encode(array('status'=>'error','msg'=>'The remaining number of times to answer today’s questions has been used up, come back tomorrow~'));
$mysqli->close();
exit;
}
// Title
if($stmt = $mysqli->prepare("select question,A,B,C,D,answer from zaszh_question where id in(?,?,?,?,?)")){
$stmt->bind_param('iiiii',$question_id[0],$question_id[1],$question_id[2],$question_id[3],$question_id[4]);
$stmt->execute();
$stmt->bind_result($question,$A,$B,$C,$D,$answer);
$rows = array();
While($stmt->fetch()){
$rows[] = array(
'question'=>$question,
'A'=>$A,
‘B’=>$B,
'C'=>$C,
'D'=>$D,
‘answer’=>$answer
);
}
// Answer record
if($stmt = $mysqli->prepare("insert into zaszh_answer(user_id,question1,question2,question3,question4,question5,create_date) values(?,?,?,?,?,?,unix_timestamp(now() ))")){
$stmt->bind_param('iiiiii',$user_id,$question_id[0],$question_id[1],$question_id[2],$question_id[3],$question_id[4]);
$stmt->execute();
If($answer_id = $stmt->insert_id){
$param = array(
‘answer_id’=>$answer_id
);
echo json_encode(array_merge($rows,$param));
}else{
echo json_encode(array('status'=>'error','msg'=>'System error.'));
}
$stmt->close();
}
$mysqli->close();
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/955398.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955398.htmlTechArticlePHP question-answering application interface example, php question-answering interface example This article describes the implementation method of PHP question-answering application interface. Share it with everyone for your reference. The specific implementation method is as follows: q...