** 两种答案都是正确的 **
我的问题是我们有一个用户可以在我的数据库中的表格中添加数据,我们将其称为教育,我们希望他们能够编辑此信息。我们的问题是,当他们点击下一个按钮加载用户可能拥有的下一个数据时,我们找不到一种方法来赋予他们能力。 例如,如下图所示,用户可以在表格中有多个条目: 我们有这样的形式:
在这种形式下,我们实现了获取第一个数据( 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>";
}
?>
答案 0 :(得分:2)
更新:为了达到你想要的效果,你需要将两个脚本混合在一起。因此,对于更新脚本,您需要添加第一个脚本的逻辑。试试这个:
在您的表单中,您可以发送隐藏字段,并更新最后一个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>
答案 1 :(得分:0)
这可以满足您的需求:
<?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>