2012-11-09 27 views
-1

我正在尝试创建一个表单页面,允许最终用户更新表中的多个条目。用户由ID_NUM标记,RECORD标记项。我想要显示表单中的每一行,并将每一行堆叠在页面上的不同实例中。如下:我们foreach循环完成这个?怎么样?

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

由于我是新来的,试图教我自己,任何帮助表示赞赏!谢谢。

+0

您的代码缺少数据库逃逸,看看[PDO和准备语句(http://stackoverflow.com/questions/ 1457131/PHP-PDO-制备-语句)。为了实现您的目标,请输出名称为name =“desc [$ NUMID]”的表格行,以便您在方便的列表中收到它们。然后对这些'$ _POST [$ desc] [$ id]'数组''foreach'来更新数据库表。 – mario

回答

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