Click here to Skip to main content
15,867,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem and don't know what is it I have 5 sets of code Now if the image is set he is redirecting me to index.php but if image is not set he is staying on same page what I am doing wrong why he is not redirecting me to index.php if image is not set

What I have tried:

<?php 
         if (isset($_REQUEST["save"])){
         if (($_REQUEST["status"]!=2)){
         if(!isset($_FILES['image'])){
$sql="UPDATE tickets SET openedby='$flname''WHERE   id ='$tid'";
             mysqli_query($dbc, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($dbc), E_USER_ERROR);
       header("Location: ../index.php");
       exit();
              
             }
             }
             }
             if (isset($_REQUEST["save"])){
             if (($_REQUEST["status"]=2)){
             if(!isset($_FILES['image'])){
 $sql="UPDATE tickets SET calername='$calername'WHERE id ='$tid'";
          mysqli_query($dbc, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($dbc), E_USER_ERROR);
          header("Location: ../index.php");
           exit();
              
            }
             }
             }
     
             if (isset($_REQUEST["save"])){
             if (($_REQUEST["status"]!=2)){
 $sql="UPDATE tickets SET openedby='$flname' WHERE id ='$tid'";
                       mysqli_query($dbc, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($dbc), E_USER_ERROR);
                     
                 }
                 }
                 
             
                 if (isset($_REQUEST["save"])){
                 if (($_REQUEST["status"]=2)){
              $sql="UPDATE tickets SET calername='$calername'WHERE id ='$tid'";
               mysqli_query($dbc, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($dbc), E_USER_ERROR);
                          
                 }
                 }
                 
              ?>
    
     
    
            <?php
                if (isset($_REQUEST["save"])){
              
              if(isset($_FILES['image'])){
              
         $query = "UPDATE tickets SET attachments='$ben' WHERE id ='$tid'";
                 mysqli_query($dbc,$query) or trigger_error("Query Failed! SQL: $query - Error: ".mysqli_error($dbc), E_USER_ERROR);
                      header("Location: ../index.php");
                      exit();
                      
                   }
              
                }
             
           ?>
Posted
Updated 20-Feb-22 23:00pm

Your problem is that the header was already sent. There is also whitespaces involved, remove those.

To solve this problem, we have to store the header in a buffer and send the buffer at the end of the script, so to store the header in the buffer, we will use the php ob_start() function and to clean the buffer, we will use the ob_end_flush() function. See the below code snippet.

<?php
ob_start(); //MUST be at the top of your page
?>
<!DOCTYPE html>
<html>
<head><title></title></head>
<body>
<p>Hello world</p>
<?php

header("Location:https://mysite.com"); //Will automatically load your index page
ob_end_flush();
?>
</body>
</html>
 
Share this answer
 
Comments
Member 13084733 21-Feb-22 9:52am    
Thank you very much for your time and explanation it is working
Andre Oosthuizen 22-Feb-22 2:57am    
Only a pleasure
I would start by rewriting that: since all your "if blocks" start with the same test pull that out as a separate test:
<?php 
if (isset($_REQUEST["save"])){
   ...
   }
else {
   ... do something else entirely ...
   }
?>
Now your tests start to become more readable - so do the same with your "image test:
<?php 
if (isset($_REQUEST["save"])){
   if (!isset($_FILES['image'])){
      ... Do your status check ...
      }
   else {
      ... Do your other stuff ...
      }
   }
else {
   ... do something else entirely ...
   }
?>
Again, you make the code clearer.

When you've done that, test it again, and see what happens.
If it still doesn't work, break out the debugger and see what path through your code it takes in the "real world" - that should tell you where it is going wrong, and get you started to fixing it.

We can't do that for you: we have no idea what your code should do when, or what the variable if conditions are testing, and we can't see the data at all!
 
Share this answer
 
Comments
Member 13084733 20-Feb-22 10:13am    
the update part is working and image upload is working with my code he is just not redirecting me to index.php i will try with else statement

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900