PHP MYSQL下一行和上一行

时间:2016-04-18 20:32:56

标签: php mysql

基本上我有一个包含数据的表格,我有一个带有框和内容的表单,我想要做的是当我点击下一个或上一个按钮时,它将从数据库中获取下一条记录。我可以做第一个和最后一个记录,但我无法设法介于两者之间。这就是我所拥有的。

$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 width="45">&nbsp;Name     </td> <td><input type="text" size="35" maxlength="256" name="UserName" value="<?php echo $Name_Field ?> "></td></tr>
      <tr><td width="45">&nbsp;E-mail   </td> <td><input type="text" size="35" maxlength="256" name="UserEmail" value="<?php echo $Email_Field ?>"></td></tr>
      <tr><td width="45">&nbsp;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

1 个答案:

答案 0 :(得分:0)

尝试向查询添加偏移量。在每次下一次单击时,在每个前一个上添加一个到$ offset,从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&gt; 1似乎很奇怪,但我不知道你的数据。

相关问题