0

My code is working great in terms of submitting data to my MySQL database. After submission, I'd like to go to another page. I have read through the many posts on this subject and the consistent answer is to use header('Location: /directory/mypage.php');.

I have tried to insert this header into many different locations on my page and I cannot get it to work. I'm sure I am missing something small but I cannot locate the issue. My code is below. Thanks in advance.

<html>   
<body>
  <?php

  session_start();
  $sessionid = session_id();

     if(isset($_POST['add']))
     {

        $dbhost = 'localhost:3036';
        $dbuser = 'user';
        $dbpass = 'pass';
        $conn = mysql_connect($dbhost, $dbuser, $dbpass);

        if(! $conn )
        {
           die('Could not connect: ' . mysql_error());
        }

        if(! get_magic_quotes_gpc() )
        {
           $Gravity = addslashes ($_POST['Gravity']);
           $Time = addslashes ($_POST['BoilTime']);
        }
        else
        {
           $Gravity= $_POST['Gravity'];
           $Time = $_POST['BoilTime'];
        }

        $IBU = $_POST['IBU'];

                $sql = "INSERT INTO tblVariableTimeUtilzation ( Sessionid, Gravity, IBU, ml, Time, Utilization ) SELECT '$sessionid',tblIBUat60minBoil.Gravity, tblIBUat60minBoil.IBU, tblIBUat60minBoil.ml, tblUtilization.Time, tblUtilization.Utilization FROM tblIBUat60minBoil INNER JOIN tblUtilization ON tblIBUat60minBoil.Gravity = tblUtilization.Gravity GROUP BY tblIBUat60minBoil.Gravity, tblIBUat60minBoil.IBU, tblIBUat60minBoil.ml, tblUtilization.Time, tblUtilization.Utilization HAVING (((tblIBUat60minBoil.Gravity)=". $Gravity .") AND ((tblIBUat60minBoil.IBU)=". $IBU . ") AND ((tblUtilization.Time)='60'));";

                $sql2 = "INSERT INTO tblPull60MinUtilization ( Sessionid, Gravity, IBU, ml, Time, Utilization ) SELECT '$sessionid', tblIBUat60minBoil.Gravity, tblIBUat60minBoil.IBU, tblIBUat60minBoil.ml, tblUtilization.Time, tblUtilization.Utilization FROM tblIBUat60minBoil INNER JOIN tblUtilization ON tblIBUat60minBoil.Gravity = tblUtilization.Gravity GROUP BY tblIBUat60minBoil.Gravity, tblIBUat60minBoil.IBU, tblIBUat60minBoil.ml, tblUtilization.Time, tblUtilization.Utilization HAVING (((tblIBUat60minBoil.Gravity)=". $Gravity .") AND ((tblIBUat60minBoil.IBU)=". $IBU . ") AND ((tblUtilization.Time)='60'));";

       $sql3 = "SELECT Sessionid FROM tblPull60MinUtilization WHERE Sessionid='59a185f576f0d27ab5ae5825fe1463f3'";
 $result = mysql_query($sql3); 
 $myrow = mysql_fetch_array($result);


        mysql_select_db('ExtractCalculation');
        $retval = mysql_query( $sql, $conn );

        if(! $retval )
        {
           die('Could not enter data: ' . mysql_error());
        }

        $retval2 = mysql_query( $sql2, $conn );

        if(! $retval2 )
        {
          die('Could not enter data: ' . mysql_error());
        }

     echo "Entered data successfully";
        header('Location: http://www.cnn.com);
        mysql_close($conn);
     }
     else
     {
        ?>

           <form method="post" action="<?php $_PHP_SELF ?>">
              <table width="400" border="0" cellspacing="1" cellpadding="2">

                 <tr>
                    <td width="100">Gravity</td>
                    <td><input name="Gravity" type="text" id="Gravity"></td>
                 </tr>

                 <tr>
                    <td width="100">IBU</td>
                    <td><input name="IBU" type="text" id="IBU"></td>
                 </tr>

                 <tr>
                    <td width="100">Boil Time</td>
                    <td><input name="BoilTime" type="text" id="BoilTime"></td>
                 </tr>

                 <tr>
                    <td width="100"> </td>
                    <td> </td>
                 </tr>

                 <tr>
                    <td width="100"> </td>
                    <td>
                       <input name="add" type="submit" id="add" value="Calculate mls Needed">

                    </td>
                 </tr>



              </table>

           </form>

        <?php
     }

  ?>

</body>
</html>
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • `$dbpass = 'pass;` is missing a quote. – al'ein Sep 21 '15 at 17:01
  • Close the string literal in this line `header('Location: http://www.cnn.com);` like so `header('Location: http://www.cnn.com');` – RiggsFolly Sep 21 '15 at 17:03
  • @AlanMachado *and Smokey* (Riggs) they fixed that but so does `header('Location: http://www.cnn.com);` so it's defintely a bad paste. The error is obvious as to why their header isn't working. – Funk Forty Niner Sep 21 '15 at 17:03
  • 1
    Your form action points to the same page it resides. If you want a redirection, you have to separate your code. Leave HTML with form controls in one page (if possible, with raw markup) and the processing code of PHP into another .php one, then point your form action to it. – al'ein Sep 21 '15 at 17:03
  • @Fred-ii- I know that, it was just a comment. – al'ein Sep 21 '15 at 17:04
  • You can't echo data before performing header modifications. Along the same lines, you cannot print anything to the browser (white space between ?> and – Ohgodwhy Sep 21 '15 at 17:04
  • @Fred-ii- Yea, now I see the blatently obvious, thanks Ralph Who is going to tell him??? – RiggsFolly Sep 21 '15 at 17:05
  • @RiggsFolly *You're welcome Smokey* Edit: Oh, I did. Reload the question. – Funk Forty Niner Sep 21 '15 at 17:06
  • We can't behave like half-typehinting people anymore, it's oppressive. – al'ein Sep 21 '15 at 17:09
  • Amazing how fast all of you come to the rescue... Within minutes... The $dbpass and cnn.com missing the single quote was simply a fat finger as I changed these prior to submitting my question. I should be more careful. Alan's response makes sense. So, I remove the php code from the page and move it to a seperate page. Do I place the header redirect into the html or the php? – indianajns Sep 21 '15 at 17:16

0 Answers0