Php foreach循环能否实现这一点?怎么用?

Php foreach循环能否实现这一点?怎么用?,php,arrays,forms,foreach,Php,Arrays,Forms,Foreach,我正在尝试创建一个表单页面,允许最终用户更新表中的多个条目。用户由ID_NUM标记,条目由记录标记。我想显示表单中的每一行,每一行都以单独的实例堆叠在页面上。详情如下: School Name: School Type: Degree: Major: Graduate: School Name: School Type: Degree: Major: Graduate: 我希望提交触发对任何行中的任何更改的更新。这是我的基本表单代码。如果这是解决问题的最佳方法,那么集成foreach循环需要

我正在尝试创建一个表单页面,允许最终用户更新表中的多个条目。用户由ID_NUM标记,条目由记录标记。我想显示表单中的每一行,每一行都以单独的实例堆叠在页面上。详情如下:

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

School Name:
School Type:
Degree:
Major:
Graduate:
我希望提交触发对任何行中的任何更改的更新。这是我的基本表单代码。如果这是解决问题的最佳方法,那么集成foreach循环需要做什么

<?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');
?>


您可以将多个
UPDATE
查询集成到一个查询,而不是多个
UPDATE
查询

 $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);

您的代码缺少数据库转义,请查看。要实现您的目标,请输出带有文件名的表单行,文件名为
name=“desc[$NUMID]”
,以便您在方便的列表中收到它们。然后在那些
$\u POST[$desc][$id]
数组上执行
foreach
,以更新数据库表。