-1

エンド ユーザーがテーブル内の複数のエントリを更新できるようにするフォーム ページを作成しようとしています。ユーザーは ID_NUM でタグ付けされ、エントリは RECORD でタグ付けされます。各行をフォームに表示し、各行を別々のインスタンスでページに積み上げたいと考えています。以下のように:

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

私はこれに不慣れで、自分自身を教えようとしているので、どんな助けも大歓迎です! ありがとうございました。

4

1 に答える 1

0

複数のクエリを持つ代わりに、UPDATEそれらを 1 つのクエリに統合できます。

 $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);
于 2012-11-09T20:04:17.283 に答える