-1

I am trying to create a form page that will allow the end-user the ability to update multiple entries in a table. The user is tagged by an ID_NUM and the entries by RECORD. I want to display each row in the form, with each row stacked on the page in separate instances. As below:

School Name:
School Type:
Degree:
Major:
Graduate:

School Name:
School Type:
Degree:
Major:
Graduate:

I want the submit to trigger an update to any changes in any row. Here is the code I have for the basic form. What do I need to do to integrate the foreach loop, if that is the best way to solve the problem?

<?php

// Start the session
require_once('startsession.php');

// Insert Page Header
$page_title = 'Edit Profile';
require_once('header.php');

// Make sure the user is logged in before going any further.
if (!isset($_SESSION['email'])) {
echo '<p class="login">Please <a href="login.php">log in</a> to access this page.</p>';
exit();
}

// Insert navmenu
require_once('navmenu.php');

require_once('vary.php');
require_once('appvars.php');
require_once('connectvars.php');

// Connect to the database using vary.php


if (isset($_POST['submit'])) 
{
// Grab the profile data from the POST
$record2 = $_POST['record'];
$school2 = $_POST['school'];
$type2 = $_POST['school_code'];
$degree2 = $_POST['degree_code'];
$desc2 = $_POST['desc'];
$grad2 = $_POST['grad']; 
$another2 = $_POST['another']; 

// Update the profile data in the database
if (!empty($school2)) {
$query3 = "UPDATE EDUCATION SET SCHOOL = '$school2' WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 . "'";
mysqli_query($dbc, $query3);
}
if (!empty($type2)) {
$query4 = "UPDATE EDUCATION SET TYPE = '$type2' WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 . "'";
mysqli_query($dbc, $query4);
}
if (!empty($degree2)) {
$query5 = "UPDATE EDUCATION SET DEGREE = '$degree2' WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 . "'";
mysqli_query($dbc, $query5);
}
if (!empty($desc2)) {
$query6 = "UPDATE EDUCATION SET MAJOR = '$desc2' WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 . "'";
mysqli_query($dbc, $query6);
}
if (!empty($grad2)) {
$query7 = "UPDATE EDUCATION SET GRAD = '$grad2' WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 ."'";
mysqli_query($dbc, $query7);
}
// Confirm success with the user
if ($another2=="Y")
{
// Clear the variables and reload the page for new submit
$record2 = "";
$school2 = "";
$type2 = "";
$degree2 = "";
$major2 = "";
$grad2 = ""; 
$another2 = ""; 
echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.avant.jobs/portal/addeducation.php">';
}
else
{
echo '<p>The education section of your profile has been successfully updated. Would you like to <a href="workcheck.php">continue?</a>?</p>';   
echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.avant.jobs/portal/workcheck.php">';
}
mysqli_close($dbc);
exit();
}
else 
{
echo '<p class="error">You must enter all of the profile data.</p>';
}

// End of check for form submission



// Grab the profile data from the database
$query8 = "SELECT * FROM EDUCATION WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "'";
$data = mysqli_query($dbc, $query8);
$row = mysqli_fetch_array($data);
if ($row != NULL) 
{
$record = $row['RECORD'];
$school = $row['SCHOOL'];
$type = $row['TYPE'];
$degree = $row['DEGREE'];
$desc = $row['MAJOR'];
$grad = $row['GRAD'];
}
else 
{
echo '<p class="error">There was a problem accessing your profile.</p>';
}
;

?>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<fieldset>
<legend>Education History </legend>
<?php
echo '<input type="hidden" id="record" name="record" value="' . $record . '">';

// Insert Listbox here
$queryschool = "SELECT * FROM SCHOOL";
$list = mysqli_query($dbc, $queryschool);
if($list) 
{
echo 'School Type? ';
echo '<select name="school_code">';
while($row = mysqli_fetch_assoc($list))
{
echo "<option value={$row['CODE']}>{$row['TYPE']}" ;
echo '</option>';
}
echo '</select>';
}

echo '<br />';
echo '<label for="school">School Name:</label>';
echo '<input type="text" id="school" name="school" size="40" maxlength="40" value="' . ((!empty($school)) ? $school : "") . '" /><br />';

// Insert Listbox here
$querydegree = "SELECT * FROM DEGREE";
$list = mysqli_query($dbc, $querydegree);
if($list) 
{
echo 'Degree Type? ';
echo '<select name="degree_code">';
while($row = mysqli_fetch_assoc($list))
{
echo "<option value={$row['CODE']}>{$row['DEGREE']}";
echo '</option>';
}
echo '</select>';
}
echo '<br />';
echo '<label for="desc">Field of study:</label>';
echo '<input type="text" id="desc" name="desc" size="40" maxlength="40" value="' . ( (!empty($desc)) ? $desc : "") . '" /><br />';
echo '<label for="grad">Did you graduate?:</label>';
echo '<input type="radio" id="grad" name="grad" value="Y" ' . ($grad == "Y" ? 'checked="checked"':'') . '/>Yes ';
echo '<input type="radio" id="grad" name="grad" value="N" ' . ($grad == "N" ? 'checked="checked"':'') . '/>No<br />';
?>
</fieldset>
<?php
echo '<label for="another">Do you need to enter more educational experience?:</label>';
echo '<input type="radio" id="another" name="another" value="Y" ' . ($another     == "Y" ? 'checked="checked"':'') . '/>Yes ';
echo '<input type="radio" id="another" name="another" value="N" ' . ($another     == "N" ? 'checked="checked"':'') . '/>No<br />';
?>

<input type="submit" value="Save Profile" name="submit" />
</form>

<?php
// Insert Page Footer
require_once('footer.php');
?>

As I am new to this and trying to teach my self, any help is appreciated! Thank you.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
KDG
  • 15
  • 1
  • 8
  • Your code lacks database escaping, look into [PDO and prepared statements](http://stackoverflow.com/questions/1457131/php-pdo-prepared-statements). To accomplish your goal, output the form rows with filed names of `name="desc[$NUMID]"` so you received them in a convenient list. Then `foreach` over those `$_POST[$desc][$id]` arrays to update the db table. – mario Nov 09 '12 at 20:00

1 Answers1

0

Instead of having multiple UPDATE queries, you can integrate them to 1 query,

 $comma = FALSE;
 $query = "UPDATE EDUCATION SET ";
// Update the profile data in the database
if (!empty($school2)) {
  $query .= "SCHOOL = '$school2'";
  $comma = TRUE;  
 }

if (!empty($type2)) {
  if($comma === TRUE)
   $query .= ", ";
   $query .= "TYPE = '$type2' ";
   $comma = TRUE;
 }

 if (!empty($degree2)) {
    if($comma === TRUE)
     $query .= ", ";
     $query5 = "DEGREE = '$degree2'";
     $comma = TRUE;
   }
 if (!empty($desc2)) {
  if($comma === TRUE)
   $query .= ", ";
   $query .= "MAJOR = '$desc2'";
   $comma = TRUE;
  }

  if (!empty($grad2)) {
    if($comma === TRUE)
      $query .= ", ";
     $query .= "GRAD = '$grad2'";
   }

 $query .= "WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "' AND RECORD = '" . $record2 ."'";
 if (!empty($school2) || !empty($type2) || !empty($degree2) || !empty($desc2) || !empty($grad2)) {
   mysqli_query($dbc, $query);
Teena Thomas
  • 5,139
  • 1
  • 13
  • 17