The edit form cann't get the following data for the same user
** BOTH ANSWERS CORRECT **
my problem is that we have a user who can add data to a table in my database, which we call Education , and we want to give them the ability to edit that information. Our problem is that we cannot find a way to enable them while they click on the next button to load the next data that the user may have. For example, and as you can see in the image below, a user can have more than one record in a table: and we have the following form:
In this form, we managed to get the first data ( id = 1 ), but 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 them to click on the next button to update the current values, and then display the following data.Here is the code 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 to update the content is 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>";
}
?>
source to share
Update. To achieve what you want you need to mix 2 scripts together. So, to the script update, you need to add the logic of the first script. Try the following:
In your form, you can submit a hidden field with the last updated id:
<input type="hidden" name="id" value="<?php echo $id;?>" />
This id can be specified as follows in the above PHP code:
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;
Then your query should be modified as shown below to select only the following entry:
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()))
Here is your complete code:
<?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>
source to share
This should get what you want:
<?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>
source to share