编辑表单无法获取同一用户的下一个数据

Edit form cann't get the next data for the same user

** 两个答案都正确 **

我的问题是我们有一个用户可以在我的数据库中的 table 中添加数据,我们称之为 Education 我们想赋予他们能力编辑此信息。我们的问题是我们无法找到一种方法让他们能够在他们单击下一步按钮时加载用户可能拥有的下一个数据。 例如,正如您在下图中所见,用户可以在 table 中拥有多个条目: 我们有这个表格:

在这种形式下,我们获得了第一个数据(id=1),但是我们如何获得该用户的下一个数据。请务必注意,一位用户可以拥有多个条目,例如 2、3、4 或更多。 所以我们希望当他们单击下一步按钮时更新当前值,然后向他们显示下一个数据 这是我们的代码:

<?php
    include("../include/session.php");
    $username = $_SESSION['username'];


if($query = mysql_query("SELECT school,degree,website,start_date,end_date,start_year,end_year,degree_description FROM education WHERE id='1' AND username='$username'") or die(mysql_error()))
{
    if(mysql_num_rows($query)>=1){
        while($row = mysql_fetch_array($query)) {
        $school = $row['school'];
        $degree = $row['degree'];
        $website = $row['website'];
        $start_date = $row['start_date'];
        $end_date = $row['end_date'];
        $start_year = $row['start_year'];
        $end_year = $row['end_year'];
        $degree_description = $row['degree_description'];
        }
    }
    else{
        echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
    }
}

?>
<title>CV Education Form</title>
<form method="post" action="education_update.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
    <legend>Education</legend>
    <label>School <input type="text" name="school" value=<?=$school?> required="required" /> </label>
    <br /><br />
    <label>Degree <input type="text" name="degree" value=<?=$degree?> required="required"/> </label>
    <br /><br />
    <label>Website <input type="text" name="website" value=<?=$website?> /> </label>
    <br /><br />
    <label>Start Date</label>
        <select name="start_date">
        <option value=<?=$start_date?>><?=$start_date?></option>
        <option value="January">January</option>
        <option value="February">February</option>
        <option value="March">March</option>
        <option value="April">April</option>
        <option value="May">May</option>
        <option value="June">June</option>
        <option value="July">July</option>
        <option value="August">August</option>
        <option value="September">September</option>
        <option value="October">October</option>
        <option value="November">Noember</option>
        <option value="December">December</option>
        </select>
    <br /><br />
    <label>End Date</label>
        <select name="end_date">
        <option value=<?=$end_date?>><?=$end_date?></option>
        <option value="January">January</option>
        <option value="February">February</option>
        <option value="March">March</option>
        <option value="April">April</option>
        <option value="May">May</option>
        <option value="June">June</option>
        <option value="July">July</option>
        <option value="August">August</option>
        <option value="September">September</option>
        <option value="October">October</option>
        <option value="November">Noember</option>
        <option value="December">December</option>
        </select>
    <br /><br />
        <label> Start Year</label>
        <select name="start_year" >
        <option value=<?=$start_year?>><?=$start_year?></option>
        <option value="1979">1979</option>
        <option value="1980">1980</option>
        <option value="1981">1981</option>
        <option value="1982">1982</option>
        <option value="1983">1983</option>
        <option value="1984">1984</option>
        <option value="1985">1985</option>
        <option value="1986">1986</option>
        <option value="1987">1987</option>
        <option value="1988">1988</option>
        <option value="1989">1989</option>
        <option value="1990">1990</option>
        <option value="1991">1991</option>
        <option value="1992">1992</option>
        <option value="1993">1993</option>
        <option value="1994">1994</option>
        <option value="1995">1995</option>
        <option value="1996">1996</option>
        <option value="1997">1997</option>
        <option value="1998">1998</option>
        <option value="1999">1999</option>
        <option value="2000">2000</option>
        </select>
        <label>End Year</label>
        <select name="end_year">
        <option value=<?=$end_year?>><?=$end_year?></option>
        <option value="1979">1979</option>
        <option value="1980">1980</option>
        <option value="1981">1981</option>
        <option value="1982">1982</option>
        <option value="1983">1983</option>
        <option value="1984">1984</option>
        <option value="1985">1985</option>
        <option value="1986">1986</option>
        <option value="1987">1987</option>
        <option value="1988">1988</option>
        <option value="1989">1989</option>
        <option value="1990">1990</option>
        <option value="1991">1991</option>
        <option value="1992">1992</option>
        <option value="1993">1993</option>
        <option value="1994">1994</option>
        <option value="1995">1995</option>
        <option value="1996">1996</option>
        <option value="1997">1997</option>
        <option value="1998">1998</option>
        <option value="1999">1999</option>
        <option value="2000">2000</option>
        </select>      
    <br /><br />
<label>Degree Description</label>
    <br />
    <textarea rows="4" cols="50" name="degree_description" required><?=$degree_description?></textarea> </label>
</fieldset>
<input type="submit" value="Update" name="submit"/>
<input type="submit" value="Next" name="next"/>
</form> 

我的php更新内容的代码如下:

<?php
    mysql_connect('localhost', 'root', 'smogi') or die(mysql_error());
    mysql_select_db("cvtool") or die(mysql_error());
    include("../include/session.php");
    $username = $_SESSION["username"];

    $school = mysql_real_escape_string($_POST["school"]);
    $degree = mysql_real_escape_string($_POST["degree"]);
    $website = mysql_real_escape_string($_POST["website"]);
    $start_date = mysql_real_escape_string($_POST["start_date"]);
    $end_date = mysql_real_escape_string($_POST["end_date"]);
    $start_year = mysql_real_escape_string($_POST["start_year"]);
    $end_year = mysql_real_escape_string($_POST["end_year"]);
    $degree_description = mysql_real_escape_string($_POST["degree_description"]);

    $query="UPDATE education
            SET school = '$school', degree = '$degree', website = '$website', start_date='$start_date', end_date='$end_date', start_year='$start_year', end_year='$end_year', degree_description='$degree_description'
            WHERE username='$username'";

mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
    echo "<p>($username) Record Updated<p>";
}else{
    echo "<p>($username) Not Updated<p>";
}

?>

更新:为了实现您想要的效果,您需要将这两个脚本混合在一起。因此,您需要在更新脚本中添加第一个脚本的逻辑。试试这个:

在您的表单中,您可以发送一个隐藏字段,其中包含最后更新的 ID:

    <input type="hidden" name="id" value="<?php echo $id;?>" />

这个Id在上面php代码中可以这样设置:

    if(isset($_POST['id']))
      $id = $_POST['id'];
    else
      //first time, initialize as you wish. Probably need to get the first id for this user, using another query
      $id = 1;

那么您的查询必须如下修改,以仅获取下一条记录:

    if($query = mysql_query("SELECT school,degree,website,start_date,end_date,start_year,end_year,degree_description FROM education WHERE id>'$id' AND username='$username' order by id asc limit 1") or die(mysql_error()))

这是您的完整代码:

 <?php
        mysql_connect('localhost', 'root', 'smogi') or die(mysql_error());
        mysql_select_db("cvtool") or die(mysql_error());
        include("../include/session.php");
        $username = $_SESSION["username"];

    if(isset($_POST['id']))
          $id = $_POST['id'];
        else
          //first time, initialize as you wish. Probably need to get the first id for this user, using another query
          $id = 1;

        $school = mysql_real_escape_string($_POST["school"]);
        $degree = mysql_real_escape_string($_POST["degree"]);
        $website = mysql_real_escape_string($_POST["website"]);
        $start_date = mysql_real_escape_string($_POST["start_date"]);
        $end_date = mysql_real_escape_string($_POST["end_date"]);
        $start_year = mysql_real_escape_string($_POST["start_year"]);
        $end_year = mysql_real_escape_string($_POST["end_year"]);
        $degree_description = mysql_real_escape_string($_POST["degree_description"]);

        $query="UPDATE education
                SET school = '$school', degree = '$degree', website = '$website', start_date='$start_date', end_date='$end_date', start_year='$start_year', end_year='$end_year', degree_description='$degree_description'
                WHERE id='$id'";

    mysql_query($query)or die(mysql_error());
    if(mysql_affected_rows()>=1){
        echo "<p>($username) Record Updated<p>";
    }else{
        echo "<p>($username) Not Updated<p>";
    }

    if($query = mysql_query("SELECT school,degree,website,start_date,end_date,start_year,end_year,degree_description FROM education WHERE id>'$id' AND username='$username' order by id asc limit 1") or die(mysql_error()))
    {
        if(mysql_num_rows($query)>=1){
            while($row = mysql_fetch_array($query)) {
            $school = $row['school'];
            $degree = $row['degree'];
            $website = $row['website'];
            $start_date = $row['start_date'];
            $end_date = $row['end_date'];
            $start_year = $row['start_year'];
            $end_year = $row['end_year'];
            $degree_description = $row['degree_description'];
            }
        }
        else{
            echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
        }
    }

    ?>
    <title>CV Education Form</title>
    <form method="post" action="education_update.php">
    <input type="hidden" name="submitted" value="true" />
    <fieldset>
        <legend>Education</legend>
        <label>School <input type="text" name="school" value=<?=$school?> required="required" /> </label>
        <br /><br />
        <label>Degree <input type="text" name="degree" value=<?=$degree?> required="required"/> </label>
        <br /><br />
        <label>Website <input type="text" name="website" value=<?=$website?> /> </label>
        <br /><br />
        <label>Start Date</label>
            <select name="start_date">
            <option value=<?=$start_date?>><?=$start_date?></option>
            <option value="January">January</option>
            <option value="February">February</option>
            <option value="March">March</option>
            <option value="April">April</option>
            <option value="May">May</option>
            <option value="June">June</option>
            <option value="July">July</option>
            <option value="August">August</option>
            <option value="September">September</option>
            <option value="October">October</option>
            <option value="November">Noember</option>
            <option value="December">December</option>
            </select>
        <br /><br />
        <label>End Date</label>
            <select name="end_date">
            <option value=<?=$end_date?>><?=$end_date?></option>
            <option value="January">January</option>
            <option value="February">February</option>
            <option value="March">March</option>
            <option value="April">April</option>
            <option value="May">May</option>
            <option value="June">June</option>
            <option value="July">July</option>
            <option value="August">August</option>
            <option value="September">September</option>
            <option value="October">October</option>
            <option value="November">Noember</option>
            <option value="December">December</option>
            </select>
        <br /><br />
            <label> Start Year</label>
            <select name="start_year" >
            <option value=<?=$start_year?>><?=$start_year?></option>
            <option value="1979">1979</option>
            <option value="1980">1980</option>
            <option value="1981">1981</option>
            <option value="1982">1982</option>
            <option value="1983">1983</option>
            <option value="1984">1984</option>
            <option value="1985">1985</option>
            <option value="1986">1986</option>
            <option value="1987">1987</option>
            <option value="1988">1988</option>
            <option value="1989">1989</option>
            <option value="1990">1990</option>
            <option value="1991">1991</option>
            <option value="1992">1992</option>
            <option value="1993">1993</option>
            <option value="1994">1994</option>
            <option value="1995">1995</option>
            <option value="1996">1996</option>
            <option value="1997">1997</option>
            <option value="1998">1998</option>
            <option value="1999">1999</option>
            <option value="2000">2000</option>
            </select>
            <label>End Year</label>
            <select name="end_year">
            <option value=<?=$end_year?>><?=$end_year?></option>
            <option value="1979">1979</option>
            <option value="1980">1980</option>
            <option value="1981">1981</option>
            <option value="1982">1982</option>
            <option value="1983">1983</option>
            <option value="1984">1984</option>
            <option value="1985">1985</option>
            <option value="1986">1986</option>
            <option value="1987">1987</option>
            <option value="1988">1988</option>
            <option value="1989">1989</option>
            <option value="1990">1990</option>
            <option value="1991">1991</option>
            <option value="1992">1992</option>
            <option value="1993">1993</option>
            <option value="1994">1994</option>
            <option value="1995">1995</option>
            <option value="1996">1996</option>
            <option value="1997">1997</option>
            <option value="1998">1998</option>
            <option value="1999">1999</option>
            <option value="2000">2000</option>
            </select>      
        <br /><br />
    <label>Degree Description</label>
        <br />
        <textarea rows="4" cols="50" name="degree_description" required><?=$degree_description?></textarea> </label>
    </fieldset>
    <input type="hidden" name="id" value="<?php echo $id;?>" />
    <input type="submit" value="Update" name="submit"/>
    <input type="submit" value="Next" name="next"/>
    </form> 

这应该能满足您的需求:

<?php
            mysql_connect('localhost', 'root', 'smogi') or die(mysql_error());
            mysql_select_db("cvtool") or die(mysql_error());
            include("../include/session.php");
            $username = $_SESSION["username"];

        if(isset($_POST['id'])){
            // $_POST['id'] is set so the user got here by submiting the form below
            // update the row using the id passed in by the hidden field on the form 
            $id = mysql_real_escape_string($_POST['id']);
            $school = mysql_real_escape_string($_POST["school"]);
            $degree = mysql_real_escape_string($_POST["degree"]);
            $website = mysql_real_escape_string($_POST["website"]);
            $start_date = mysql_real_escape_string($_POST["start_date"]);
            $end_date = mysql_real_escape_string($_POST["end_date"]);
            $start_year = mysql_real_escape_string($_POST["start_year"]);
            $end_year = mysql_real_escape_string($_POST["end_year"]);
            $degree_description = mysql_real_escape_string($_POST["degree_description"]);

            $query="UPDATE education
                    SET school = '$school', degree = '$degree', website = '$website', start_date='$start_date', end_date='$end_date', start_year='$start_year', end_year='$end_year', degree_description='$degree_description'
                    WHERE id='$id'";

            mysql_query($query)or die(mysql_error());
            if(mysql_affected_rows()>=0){
                echo "<p>($username) Record Updated<p>";
            }else{
                echo "<p>($username) Not Updated<p>";
            }
            // after updating the row, get the next row for this user
            if($query = mysql_query("SELECT id,school,degree,website,start_date,end_date,start_year,end_year,degree_description FROM education WHERE id>'$id' AND username='$username' order by id asc limit 1") or die(mysql_error())){

                if(mysql_num_rows($query)>=1){
                    while($row = mysql_fetch_array($query)) {
                    $school = $row['school'];
                    $degree = $row['degree'];
                    $website = $row['website'];
                    $start_date = $row['start_date'];
                    $end_date = $row['end_date'];
                    $start_year = $row['start_year'];
                    $end_year = $row['end_year'];
                    $degree_description = $row['degree_description'];
                    $id = $row['id'];
                    }
                }
                else{
                    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
                }
            }


        }

        else{
          // user came to the page for the first time, 
          // not by submiting the form, get the 
          // first id for this user, using another query
          if($query = mysql_query("SELECT id,school,degree,website,start_date,end_date,start_year,end_year,degree_description FROM education WHERE username='$username' order by id asc limit 1") or die(mysql_error())){

              if(mysql_num_rows($query)>=1){
                  while($row = mysql_fetch_array($query)) {
                  $school = $row['school'];
                  $degree = $row['degree'];
                  $website = $row['website'];
                  $start_date = $row['start_date'];
                  $end_date = $row['end_date'];
                  $start_year = $row['start_year'];
                  $end_year = $row['end_year'];
                  $degree_description = $row['degree_description'];
                  $id = $row['id'];
                  }
              }
              else{
                  echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
              }
          }
        }



        ?>
        <title>CV Education Form</title>
        <form method="post" action="edit_education.php">
        <input type="hidden" name="submitted" value="true" />
        <fieldset>
            <legend>Education</legend>
            <label>School <input type="text" name="school" value=<?=$school?> required="required" /> </label>
            <br /><br />
            <label>Degree <input type="text" name="degree" value=<?=$degree?> required="required"/> </label>
            <br /><br />
            <label>Website <input type="text" name="website" value=<?=$website?> /> </label>
            <br /><br />
            <label>Start Date</label>
                <select name="start_date">
                <option value=<?=$start_date?>><?=$start_date?></option>
                <option value="January">January</option>
                <option value="February">February</option>
                <option value="March">March</option>
                <option value="April">April</option>
                <option value="May">May</option>
                <option value="June">June</option>
                <option value="July">July</option>
                <option value="August">August</option>
                <option value="September">September</option>
                <option value="October">October</option>
                <option value="November">Noember</option>
                <option value="December">December</option>
                </select>
            <br /><br />
            <label>End Date</label>
                <select name="end_date">
                <option value=<?=$end_date?>><?=$end_date?></option>
                <option value="January">January</option>
                <option value="February">February</option>
                <option value="March">March</option>
                <option value="April">April</option>
                <option value="May">May</option>
                <option value="June">June</option>
                <option value="July">July</option>
                <option value="August">August</option>
                <option value="September">September</option>
                <option value="October">October</option>
                <option value="November">Noember</option>
                <option value="December">December</option>
                </select>
            <br /><br />
                <label> Start Year</label>
                <select name="start_year" >
                <option value=<?=$start_year?>><?=$start_year?></option>
                <option value="1979">1979</option>
                <option value="1980">1980</option>
                <option value="1981">1981</option>
                <option value="1982">1982</option>
                <option value="1983">1983</option>
                <option value="1984">1984</option>
                <option value="1985">1985</option>
                <option value="1986">1986</option>
                <option value="1987">1987</option>
                <option value="1988">1988</option>
                <option value="1989">1989</option>
                <option value="1990">1990</option>
                <option value="1991">1991</option>
                <option value="1992">1992</option>
                <option value="1993">1993</option>
                <option value="1994">1994</option>
                <option value="1995">1995</option>
                <option value="1996">1996</option>
                <option value="1997">1997</option>
                <option value="1998">1998</option>
                <option value="1999">1999</option>
                <option value="2000">2000</option>
                </select>
                <label>End Year</label>
                <select name="end_year">
                <option value=<?=$end_year?>><?=$end_year?></option>
                <option value="1979">1979</option>
                <option value="1980">1980</option>
                <option value="1981">1981</option>
                <option value="1982">1982</option>
                <option value="1983">1983</option>
                <option value="1984">1984</option>
                <option value="1985">1985</option>
                <option value="1986">1986</option>
                <option value="1987">1987</option>
                <option value="1988">1988</option>
                <option value="1989">1989</option>
                <option value="1990">1990</option>
                <option value="1991">1991</option>
                <option value="1992">1992</option>
                <option value="1993">1993</option>
                <option value="1994">1994</option>
                <option value="1995">1995</option>
                <option value="1996">1996</option>
                <option value="1997">1997</option>
                <option value="1998">1998</option>
                <option value="1999">1999</option>
                <option value="2000">2000</option>
                </select>      
            <br /><br />
        <label>Degree Description</label>
            <br />
            <textarea rows="4" cols="50" name="degree_description" required><?=$degree_description?></textarea> </label>
        </fieldset>
        <input type="text" name="id" value="<?php echo $id;?>" />
        <input type="submit" value="Update" name="submit"/>
        <input type="submit" value="Next" name="next"/>
        </form>