** BOTH ANSWERS ARE CORRECT **
my problem is that we have a user which can add data in a table in my database which we call it Education and we want to give them the ability to edit this information. Our problem is that we cannot find a way to give them the ability while they click on the next button to load the next data that might the user have. For example and as you can see in the image below the user can have more than one entry in the table: and we have this form:
In this form we achieved to get the first data (id=1) but how we can get the next data for this user. It is important to note that a user can have more than one entry for example 2,3,4 or more. So we want when they click on next button to update the current values and then shows them the next data Here is the code that we have:
<?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>
My php code for updating the content is the below:
<?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>";
}
?>
This should get you what you need:
Update: In order to achieve what you want, you need to mix the 2 scripts together. So, to the update script you need to add the logic of the first script. Try this:
In your form, you can send a hidden field with the last id updated:
This Id can be set like this in the above php code:
Then your query must be modified as below, to fetch only the next record:
Here is your full code: