PHP MYSQL 下一个和上一行
Posted
技术标签:
【中文标题】PHP MYSQL 下一个和上一行【英文标题】:PHP MYSQL Next AND Previous row 【发布时间】:2016-04-18 20:32:56 【问题描述】:基本上我有一个带有数据的表格,我有一个带有框和东西的表格,我想要做的是当我点击下一个或上一个按钮时,它将从数据库中获取下一条记录。我可以做第一个和最后一个记录,但我无法弄清楚两者之间的关系。这就是我所拥有的。
$sql="SELECT * FROM Emails where ID > 1 ORDER BY UserEmail LIMIT 1";
if ($result=mysqli_query($connection,$sql))
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
while( $row = mysqli_fetch_array( $result ) )
$Email_Field = $row['UserEmail']; //primary key
$Name_Field = $row['UserName'];
$UserTel = $row['UserTel'];
$Drop_Down = $row['Drop_down'];
$MessageType = $row['MessageType'];
$Comments = $row['Comments'];
$SubjectOther = $row['SubjectOther'];
$Check = $row['Request'];
<form method="POST" action="Controller_leads.php">
<p><strong>What kind of comment would you like to send?</strong></p>
<input type="radio" <?php if ($MessageType == "Complaint") echo "checked"; ?> name="MessageType" value="Complaint">Complaint
<input type="radio" <?php if ($MessageType == "Problem") echo "checked"; ?> name="MessageType" value="Problem">Problem
<input type="radio" <?php if ($MessageType == "Suggestion") echo "checked"; ?> name="MessageType" value="Suggestion">Suggestion
<br>
<p><strong>What about us do you want to comment on?</strong></p>
<select name="Drop_Down" size="1">
<option value ="Web Site" <?php if ($Drop_Down == "Web Site") echo selected ?>>Web Site</option>
<option value ="Office Hours" <?php if ($Drop_Down == "Office hours") echo selected ?>>Office Hours</option>
<option value ="Pamphlet" <?php if ($Drop_Down == "Pamphlet") echo selected ?>>Pamphlet</option>
</select>
Other: <input type="text" size="26" maxlength="256" name="SubjectOther" value="<?php echo $SubjectOther ?>">
<p><strong>Enter your comments in the space provided below:</strong></p>
<textarea name="Comments" rows="5" cols="42"><?php echo $Comments;?></textarea><br><br>
<strong>Tell us how to get in touch with you:</strong><br><br>
<table>
<tr><td > Name </td> <td><input type="text" size="35" maxlength="256" name="UserName" value="<?php echo $Name_Field ?> "></td></tr>
<tr><td > E-mail </td> <td><input type="text" size="35" maxlength="256" name="UserEmail" value="<?php echo $Email_Field ?>"></td></tr>
<tr><td > Telephone</td> <td><input type="text" size="35" maxlength="256" name="UserTel" value="<?php echo $UserTel ?>"></td></tr>
</table>
<br>
<input type="checkbox" name="Check" <?php if ($Check == "Contact Requested") echo checked; ?> value="Contact Requested">Please contact me as soon as possible regarding this matter
<br><br>
<input type="submit" value="First" name="first">
<input type="submit" value="Next" name="next">
<input type="submit" value="Previous" name="previous">
<input type="submit" value="Last" name="last"> code here
【问题讨论】:
寻找php mysql分页 UserEmail 是一个数字?很棒 使用偏移量从数据库中获取下一条记录。 【参考方案1】:尝试向查询添加偏移量。在每次下一次单击时,在 $offset 上加一,在上一次单击时,从偏移量中减一。然后,像这样在查询中包含偏移量:
# get the current offset
# initial value
$offset = 1;
# if we have an offset from a previous or next click, use that
if (isset($_POST['offset']))
# validate this input to protect against sql injection
if (is_int($_POST['offset']))
$offset = $_POST['offset'];
# now that we have our current value, see if we need to get the next or previous
if ($_POST['submit']=="Next")
# next, add one offset
$offset++;
else if ($_POST['submit']=="Previous")
# previous, go back one if we are greater than one
if ($offset > 1)
$offset--;
# query time, give me one result (LIMIT 1), staring at record $offset
$sql = "select SELECT * FROM Emails where UserEmail > 1
ORDER BY UserEmail LIMIT 1, $offset";
在您的表单中添加:
<input type="hidden" name="offset" value="<?php echo $offset; ?>">
另一方面,UserEmail > 1 看起来很奇怪,但我不知道你的数据。
【讨论】:
我不明白偏移量在这种情况下有何帮助。我添加了 html 部分以查看我想要做什么。如果我单击第一个按钮,它会给我第一个数据,这有效,当我单击下一步时,它会显示下一行,但如果我再次单击它,它就坐在那里。我应该使用输入类型作为提交吗? 是的,您需要使用用户输入来更改偏移量。我在答案中添加了一个示例。 nm,偏移量被完全忽略,只执行sql查询,然后只执行相同的查询而不增加偏移量。以上是关于PHP MYSQL 下一个和上一行的主要内容,如果未能解决你的问题,请参考以下文章