Hello people,
I directly come to the point and ask my question..I was trying to do something using php and mysql.Its just a game type thing in which a random question is picked from database using rand(min,max) and answer given by user is matched with the answer of the database.
So, table is quest containing columns qid,question,answer
PS: I tried with my stupid approach and trying to explore more ways but none of them are working.I am learning so any guidance will be appreciated..
Thanks,
Regards,
Jayant.
So that means some of the questions would have same answers as that of what's user have written ?
Can you try to give an example for more clearity ?
Thanks,
No, the question and answers are not repeated..Each question has an id,question and answer..I was just making a thing by which I don't have to send user an OTP or activation email,my aim is to ask a random question to a newly registered user then,If he answer the question correctly (i.e his answer matches with the answer of asked question from database),then I will update a column and mark the user as active.Hope it clears what I want to achieve,but If not,then I post a code here so that It will make you clear about my question.
Assume,something like this:
qid question answer
1 2 + 2 4
2 4 / 2 2
3 2 x 2 4
My select query:
PHP Code:
$random_id = rand(1,3);
$qry = SELECT * from quest_table WHERE qid = $random_id;
$result = mysql_query($qry,$con);//$con is a php file containing connection details
while ($row = mysql_fetch_assoc($result))
{
echo $row['question];//A random question is visible in form
}
Suppose, question is 2 + 2,so user must answer 4(so it gets matched with the answer of the table). Waiting for reply.
ohk tried something.
for example here are a bunch of questions with there answers defined at question_list table by the admin/you at the db.
here is he code:
PHP Code:
$sql = "SELECT * FROM question_list order by RAND() LIMIT 1";
$result = $connect_db->query($sql);
$row = $result->fetch_assoc();
$question = $row['question'];
$answer = $row['answer'];
echo "<b>Question: </b>".$question."<br><b>Answer: </b>".$answer;
and here is the live review what the code does:
And if you want do implement a checking type system then you can try this:
index.php:
PHP Code:
include 'config.php';
$sql = "SELECT * FROM question_list order by RAND() LIMIT 1";
$result = $connect_db->query($sql);
$row = $result->fetch_assoc();
$question = $row['question'];
$answer = $row['answer'];
if($row != null) {
echo "<b>Question: </b>".$question."<br>";
echo "<form method='POST' action='check.php'>
<input type='hidden' name='question' value='".$question."'/>
<input type='text' placeholder='Type Answer' name='answer'/>
<button type='submit'>SUBMIT</button>
</form>";
//echo "<b>Question: </b>".$question."<br><b>Answer: </b>".$answer;
}
here i have used config.php for logging the php file to the system..
and the check.php file can be like this:
PHP Code:
include 'config.php';
$question = $_POST['question'];
$sql = "SELECT * FROM question_list WHERE question='$question'";
$result = $connect_db->query($sql);
$row = $result->fetch_assoc();
$question = $row['question'];
$answer = $row['answer'];
if($_POST['answer'] == $answer) {
echo 'Correct!';
} else {
echo 'Wrong!';
}
and here is its live demo:
if you want config.php also then here it is:
PHP Code:
$connect_db = mysqli_connect("hostname here", "username of account of mysql", "accountt password", "databasename");
if(!$connect_db) {
die("<b>Warning!</b> Connection Failed: ".mysqli_connect_error());
}
Thank you so much for help..Loved your explaination,it was just a few things but I made things critical,but your code made things so easier for me and helped me alot.I think I to have develop my logics and should practise more..Will post more here if I will need help.
Closing this topic since the Problem has been resolved.
Use report button if you wants to reopen the thread.