Reputation: 105
Whenever I click the button, the page got refreshed and lost the previous values in variables. When I use mt_rand
function, the fetched rows are sometimes duplicated. What to do to get next row on each button click?
Fetch random row is not necessary, at least I want to get the next row in order.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
//Database initialization
$StudName ="";
$Name="";
$Task="";
$connection=mysqli_connect("localhost","APB","12345678","apb");
$number=1;
if(isset( $_POST["button1"]))
{
$number = mt_rand(1,10);
$s="SELECT * FROM student_list WHERE NUM='$number'";
$r= mysqli_query($connection,$s);
$row = mysqli_fetch_array($r);
$StudName = $row["NAME"];
$Name="";
}
if(isset($_POST["Submit5"]))
{
$numb = mt_rand(1,4);
$s="SELECT * FROM computer WHERE rn='$numb'";
$r= mysqli_query($connection,$s);
$row=mysqli_fetch_array($r);
$Task = $row["task"];
$StudName ="";
$Name ="";
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form" action="" name="form1" method="POST" >
<h1>MPTC FRESHER'S DAY CELEBRATION - 2017</h1>
<h5>This time the lucky one is ...</h5>
<p>
<input type="hidden" name="process" value="1";>
<input onclick="<?php $number=$number+1;?>" name="button1" type="submit" id="button1" value="GENERATE" />
<br/>
<input id="t1" name="textfield" type="text" value="<?php echo $StudName; ?>" />
</p>
<p>
<h5>Be Ready !! </h5>
<input type="submit" value="TASK" name="Submit5" value="COMPUTER" />
<br/>
<input name="textfield3" type="text" value="<?php echo $Task; ?>" />
</p>
</form>
</body>
</html>
Upvotes: 1
Views: 2272
Reputation: 9692
To get the previous or next row (number) in the database you can do this query:
SELECT * FROM student_list WHERE num = (SELECT min(num) FROM student_list WHERE num > '$number') LIMIT 1;
And for the previous its the same but use smaller than (<);
Another tip I would like to give you is to not inject variables directly into the query. But if you have to, at least if you know the value is going to be a number you can cast it to int by doing $number = (int) $number; Other functions you could look into are is_numeric, and $number > 0, etc.
But rather avoid placing these variables directly into the query. Look into prepared statements, PDO works out of the box and is much safer than using mysqli_*.
Upvotes: 0
Reputation: 1948
First of all that onclick="<?php $number=$number+1;?>"
does nothing. You cannot use a js event handler to execute php code.
What you can do, and you're actually very close to the solution, is this:
<p>
<input type="hidden" name="process" value="<?php echo $number;?>";>
<input name="button1" type="submit" id="button1" value="GENERATE" />
<br/>
<input id="t1" name="textfield" type="text" value="<?php echo $StudName; ?>" />
</p>
and change the php part like this:
$number= $_POST['process'];
if(isset( $_POST["button1"]))
{
$number = $number + 1;
}
$s="SELECT * FROM student_list WHERE NUM='$number'";
$r= mysqli_query($connection,$s);
$row = mysqli_fetch_array($r);
$StudName = $row["NAME"];
$Name="";
Upvotes: 2