0

** 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: enter image description here and we have this form:

enter image description here

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>";
}

?>
Waaaaat
  • 634
  • 3
  • 14
  • 29
  • you already limited the query by `WHERE id='1'`, if you need more than 1 row, use a different query statement. – Alex Jan 12 '15 at 15:53
  • yes that was a typo thanks – Waaaaat Jan 12 '15 at 15:55
  • I'll suggest you make a table of data to show for the user, than modify the data by getting the id's of rows in a table. – Alex Jan 12 '15 at 15:57
  • You say "In this form we achieved to get the first data (id=1) " can we see the code that does this? – Wesley Smith Jan 13 '15 at 00:59
  • @DelightedD0D that was a typo becauce in my select i had WHERE id='1' AND username='$username' which is wrong and i delete that. – Waaaaat Jan 13 '15 at 01:00
  • Ok, if I'm not mistaken, your wanting to get all records for the current username. The first row of data should fill in the form. When the user clicks the next button, you want that user's next row of data to fill the form. Is that correct? – Wesley Smith Jan 13 '15 at 01:07
  • yes exactly. I want when the click on Next button to automatically update the database and then it will show the next data for this user . PS: we can delete the Update Button and keep only the NEXT – Waaaaat Jan 13 '15 at 01:09
  • if($query = mysql_query("SELECT school,degree,website,start_date,end_date,start_year,end_year,degree_description FROM education WHERE username='$username'") or die(mysql_error())) ... You can find this line of code in the first code and in line 6 – Waaaaat Jan 13 '15 at 01:14
  • @DelightedD0D do you have any suggestions my friend? – Waaaaat Jan 13 '15 at 01:49
  • Actually, Aris has you covered below, see the edit he made. That is exactly what I would have suggested :) – Wesley Smith Jan 13 '15 at 06:00

2 Answers2

2

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:

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

This Id can be set like this 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 must be modified as below, to fetch only the next record:

    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 full 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> 
Aris
  • 4,643
  • 1
  • 41
  • 38
  • @Aris, looks like you left some of your old answer at the top – Wesley Smith Jan 13 '15 at 05:57
  • I copy paste your code but then it gives this error .... Undefined index for school, degree, website, start_date, end_date, start_year, end_year and degree_descriptipn – Waaaaat Jan 13 '15 at 06:01
  • @niklakis did you start copying at ` – Wesley Smith Jan 13 '15 at 06:05
  • @niklakis show me a print screen of your actual php please, the top of the file – Wesley Smith Jan 13 '15 at 06:09
  • @DelightedD0D here is the edit_education file http://prntscr.com/5rvcdy and this is the the education_update http://prntscr.com/5rvciv – Waaaaat Jan 13 '15 at 06:11
  • @DelightedD0D I notice that in the given solution our friend aris has the code of education_update in the edit_education and I have it in two different php. Do you believe that it might be this the mistake? – Waaaaat Jan 13 '15 at 06:26
  • I threw up an answer below to explain what I think the issue is, and see my comment there about have two pages – Wesley Smith Jan 13 '15 at 06:28
  • @DelightedD0D First since we have it now in one file . i need to change this ...
    to this ...
    ... right? But when I have this code and run it for the first time I'm also getting the same errors and the data is updated with some default empty values
    – Waaaaat Jan 13 '15 at 06:40
  • @niklakis don't just copy paste. You need to add some small logic yourself. For example, how do you get the first record? I see you get id=1. But what about other users? Just fill the gaps. – Aris Jan 13 '15 at 06:51
  • also i changed your 'update' query to use id instead of username. – Aris Jan 13 '15 at 06:53
0

This should get you what you need:

<?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> 
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • I saw that we have this ... this might be our mistake because every time it updates for id = 1 – Waaaaat Jan 13 '15 at 07:21
  • I saw that we have this ... this might be our mistake because every time it updates for id = 1 and also Notice: Undefined index: id in E:\xampp\htdocs\industrial\CVTool\cv\edit_education.php on line 51 which line 51 is this $id = $row['id']; – Waaaaat Jan 13 '15 at 07:23
  • Yeah, that line should be `` – Wesley Smith Jan 13 '15 at 07:26
  • i edit what u said but in order to check it I added one more data id=3 and in the begining it starts by showing the first data , we can update the first data then it shows the id=2 if we click on update then it updates id=1 and it doesnt go to id=3 – Waaaaat Jan 13 '15 at 07:29
  • make the name="id" field not be hidden, what value is set to that input after you update the first data then it shows the id=2 ? – Wesley Smith Jan 13 '15 at 07:37
  • How do I make it not hidden and I dont understand your second question sorry :( – Waaaaat Jan 13 '15 at 07:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68711/discussion-between-delightedd0d-and-niklakis). – Wesley Smith Jan 13 '15 at 07:43
  • hello again my friend, I have a big problem (again) and you are one of my best user of the stackoverflow can you take please a look on my new problem http://stackoverflow.com/questions/28930809/load-data-from-database-according-to-the-number-that-i-clicked – Waaaaat Mar 11 '15 at 18:06