[英]How to fetch next row from database on each button click in PHP
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. 当我使用
mt_rand
函数时,有时会复制获取的行。 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>
使用AJAX并将响应附加到变量并显示。
First of all that onclick="<?php $number=$number+1;?>"
does nothing. 首先,
onclick="<?php $number=$number+1;?>"
无效。 You cannot use a js event handler to execute php code. 您不能使用js事件处理程序执行php代码。
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: 并像这样更改php部分:
$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="";
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;
但是如果有必要,至少,如果您知道该值将是一个数字,可以通过执行$ number =(int)$ number;将其强制转换为int。 Other functions you could look into are is_numeric, and $number > 0, etc.
您可能要查看的其他函数是is_numeric和$ number> 0,依此类推。
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_*.
查看准备好的语句,PDO即开即用,比使用mysqli_ *安全得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.