PHP 代码的行为根据位置(表中的索引)而有所不同



该程序基本上是一个表,每行有三个btn(更新,配置文件,删除(。

更新和删除 BTN 运行良好,但配置文件 BTN 对于所有表的工作方式并不相同:(。

它应该将用户重定向到配置文件页面,但它只对前三行执行此操作,之后无法重定向。

我尝试添加一个 printf 语句,以便我知道是否调用了 if 语句,并且正确调用了 printf,但在索引 3 之后重定向行根本不起作用。

我现在将添加相关代码:

while($row =mysqli_fetch_assoc($result)){
echo '<tr>' ;
echo '<td>'.'<input type="text" name="id'.$i.'" value="'.$row['emp_id'].'"/> '.'</td>';

echo '<td>'.'<input type="text" name="fname'.$i.'" value="'.$row['emp_fname'].'" />'.'</td>';

echo '<td>'.'<input type="text" name="lname'.$i.'" value="'.$row['emp_lname'].'" />'.'</td>';

echo '<td>'.'<input type="text" name="phone_number'.$i.'" value="'.$row['emp_phonenumber'].'" >'.'</td>';

echo '<td>'.'<input type="text" name="email'.$i.'" value="'.$row['emp_email'].'"/> '.'</td>';

echo '<td>'.'<input type="text" name="salary'.$i.'" value="'.$row['emp_salary'].'"/> '.'</td>';

echo '<td>'.'<input type="text" name="status'.$i.'" value="'.$row['emp_status'].'" />'.'</td>';
echo '<td> <input type="submit" name="update_btn'.$i.'" value="update"/> ';
echo '<td> <input type="submit" name="profile_btn'.$i.'" value="profile"/> ';
if(isset($_GET['profile_btn'.$i])){
// the profile btn has been pressed..
header("Location: profile.php") ;
printf("Location : " . $i) ;

}

if(isset($_GET['update_btn'.$i])){
if(isset($_GET['check'.$i])){
$id = $_GET['id'.$i];
$fname  = $_GET['fname'.$i] ;
$lname = $_GET['lname'.$i];
$phone_number =  $_GET['phone_number'.$i];
$email = $_GET['email'.$i];
$salary =$_GET['salary'.$i] ;
$status =$_GET['status'.$i];

$updateStatus = "update employees set emp_fname ='$fname' , emp_lname='$lname' ,emp_phonenumber='$phone_number', emp_email='$email', emp_salary='$salary', emp_status='$status' WHERE emp_id = '$id' " ;
$qry = mysqli_query($con,$updateStatus);
if(!$qry){
echo 'failed to update...';
}else{
header("Location: employee.php");
}
}else{
echo'please activate the update modification options using the tick' ;
}
}
echo  '</td>';
echo '<td>' ;
echo ' <input type="submit" name="delete'.$i.'" value="delete"/> ';
if($_GET['delete'.$i]){
if($_GET['check'.$i]){
$id = $_GET['id'.$i];
$delete = "delete from employees where emp_id = '$id'";
$query = mysqli_query($con,$delete);
if(!$query){
echo'delete faile';
}else{
header("Location: employee.php");
}
}
}
echo '</td>';
echo '<td>' ;
echo '<input type="Checkbox" name="check'.$i.'"  />' ;
echo '</td>' ;
echo'</tr>';
$i++;
}

该问题仅与配置文件BTN有关,而与别的无关。

整个文件在这里 ::

https://drive.google.com/file/d/13gnoC5jhYfmi8vaJsJJs9KBvb61wQpoj/view?usp=sharing

您在header()之前使用echo

要解决此问题,您可以执行以下操作。

<?php
ob_start( );
?>
<html>
some output
<?php
if ( $some_condition )
{
ob_end_clean( );
header( 'Location: http://www.google.com' );
exit;
}
?>
some output
</html>
<?php
ob_end_flush( );
?>

请参阅此内容和更多详细信息。

您还可以使用 AJAX 回发正在编辑的配置文件的 ID,并根据该 ID 进行重定向。

最新更新