0

I have a database that is linked with the php. When I'm writing the first code, my web page gets redirected to the page 'mypage1.php'. But when I'm writing the second code, which is almost(exactly) the same, it doesn't get redirected to the mypage1.php. In the table inside my database, I have Orderid's that I'm using to relate to the php code. Could you tell me why doesn't the second code redirects to the same page? (It stays on the same page). The web page gets redirected to the mypage1.php when I am removing the text areas code from the 2nd code, particularly when I'm removing this code:

<input type='textarea' name='staffname'>
            </td><td>
            <input type='textarea' name='Time'>
            </td><td>
            <input type='textarea' name='custdetail'>
            </td><td>
            <input type='textarea' name='orderId'>

CODE 1 (PHP CODE): echo "";

echo "<tr>";
echo "<td>idSpatula</td><td>Qauntity</td><td>Staff Name</td><td>Staff Id</td><td>Customer Details</d><td>Order Id</td>";
echo "</tr>";

echo "<tr>";

echo "<form >";
echo "<td>" ."Spatula Id=3"."</td><td>

    <input type='textarea' name='3'>

    </td><td>
        <input type='textarea' name='staffname'>
        </td><td>
        <input type='textarea' name='Time'>
        </td><td>
        <input type='textarea' name='custdetail'>
        </td><td>
        <input type='textarea' name='orderId'>

        <input type='submit' id='submit' value='submit'>
        </td>

</form>";



echo "</tr>";

echo "</table>";




echo "</br>";

if(isset($_GET['3'])){
     // means submit clicked!

    $queryy3=0;
    $queryy3 = $_GET['3'];





        header('Location: mypage1.php'); 

        $sqll3 = "UPDATE Spatula SET  QuantityInStock=QuantityInStock-$queryy3  WHERE idSpatula=3 and QuantityInStock>0";
    $sqlorder3 = "insert into `Order`(idOrder, RequestedTime, ResponsibleStaffMember, CustomerDetails) values (3,'1987-04-20 11:34:09','Shubhams','tell')";

        $result1003 = mysqli_query($con, "SELECT QuantityInStock FROM Spatula where idSpatula=3");


    mysqli_query($con, $sqlorder2);


       if (mysqli_query($con, $sqll3) & $col1003['QuantityInStock']!=0 ) {
            //$sql = "UPDATE Spatula SET  QuantityInStock=QuantityInStock-$query  WHERE idSpatula= and QuantityInStock>0";
        echo "Updated"; 
        }
      while($col1003 = mysqli_fetch_array($result1003)) {
        echo $col1003['QuantityInStock'];
        if ($col1003['QuantityInStock']<=0){
              header('Location: mypage2.php'); 
         }
       }



      } 

CODE 2:

echo "<table border='1'>";

echo "<tr>";
echo "<td>idSpatula</td><td>Qauntity</td><td>Staff Name</td><td>Staff Id</td><td>Customer Details</d><td>Order Id</td>";
echo "</tr>";

echo "<tr>";

echo "<form >";
echo "<td>" ."Spatula Id=4"."</td><td>

    <input type='textarea' name='4'>

    </td><td>
        <input type='textarea' name='staffname'>
        </td><td>
        <input type='textarea' name='Time'>
        </td><td>
        <input type='textarea' name='custdetail'>
        </td><td>
        <input type='textarea' name='orderId'>
        </td>
    <input type='submit' id='submit' value='submit'>

</form>";



echo "</tr>";

echo "</table>";







if(isset($_GET['4'])){
     // means submit clicked!

    $queryy4=0;
    $queryy4 = $_GET['4'];




        header('Location: mypage1.php'); 

        $sqll4 = "UPDATE Spatula SET  QuantityInStock=QuantityInStock-$queryy4  WHERE idSpatula=4 and QuantityInStock>0";
    $sqlorder4 = "insert into `Order`(idOrder, RequestedTime, ResponsibleStaffMember, CustomerDetails) values (5,'1987-04-20 11:34:09','Shusss','tss')";

        $result1004 = mysqli_query($con, "SELECT QuantityInStock FROM Spatula where idSpatula=4");


    mysqli_query($con, $sqlorder4);


       if (mysqli_query($con, $sqll4) & $col1004['QuantityInStock']!=0 ) {

        echo "Updated"; 
        }
      while($col1004 = mysqli_fetch_array($result1004)) {
        echo $col1004['QuantityInStock'];
        if ($col1004['QuantityInStock']<=0){
              header('Location: mypage2.php'); 
         }
       }



      } 
Shubham
  • 11
  • 5
  • You can't send headers after output. If you enabled warnings, you would've known the problem. http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – icecub Oct 16 '16 at 05:18
  • It doesn't help. I've removed the print statement! – Shubham Oct 16 '16 at 05:21
  • 1
    The entire form before the if statement is output! Every `echo()` is output! Wrap those in a better if-statement like `if(!isset($_GET['4'])){ echo.. } else { header()`. – icecub Oct 16 '16 at 05:22
  • The first code gets redirected. Why do you think does that happen? Unless I use the textareas, I can't get its value to use the if statement that redirects to the mypage1.php if the if statement is true. – Shubham Oct 16 '16 at 05:26
  • Unless you didn't include the entire code, that first one shouldn't redirect either. – icecub Oct 16 '16 at 05:29
  • I don't see any difference, but try this code instead. Check the comments I've added to it! http://pastebin.com/U2Wwb2wX – icecub Oct 16 '16 at 05:49
  • Wait.. now I actually see what is going on. You have 2 `header('Location: .. ')`; in there. Of course that's gonna cause trouble.. – icecub Oct 16 '16 at 05:52
  • But that the other header location would work only on a certain situation. – Shubham Oct 16 '16 at 06:06
  • I tried even your code, still didn't help. – Shubham Oct 16 '16 at 06:11

0 Answers0