0

the program is basically a table with three btns for each row (Update, profile, delete).

the update and the delete btn is working perfectly, but the profile btn doesn't work the same for all of the table:).

it is supposed to redirect the user to a profile page but it's only doing it for first three rows and fails to redirect after that.

I tried to add a printf statement so I know if the if statement is getting called, and the printf is getting called properly but the redirect line is not working at all after the index 3.

i'll add the related code now :

                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++;
            }

the problem is only related to the profile btn and nothing else.

the whole file is here ::

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

  • where is `$i` set? it is probably lost on submit - hence the issue –  May 31 '18 at 03:25
  • it is intilized before the while loop ,, and i can print it out with the printf statement without any problem .. --- in fact i'll add the whole file to a drive and add it here if i can :). – 輝き しんや May 31 '18 at 03:35
  • i added a link so you can browse the whole php file – 輝き しんや May 31 '18 at 03:38
  • You're concatenating things in that file you don't even have to concatenate such as the . between `` and `` for one. Should just be together if you're insisting on doing it that way: echo `''` – Statik Stasis May 31 '18 at 03:41
  • tahnks for the tip (i'm actually an android developer and this is my first project using html and php :) ). i'll keep that in mind . – 輝き しんや May 31 '18 at 06:32

1 Answers1

0

Your using echo before header().

To fix that you can do something like this.

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

See this and this more details.

You can also use AJAX to post back the id of the profile being edited and redirect depending on that.

Jay Bhatt
  • 5,601
  • 5
  • 40
  • 62