1

My question is very simple. but I am struggling with it too much. The problem is once I hit the submit button that page should redirect to some other page. Kindly just tell me what I have done wrong? Because I have tried everything.

Code:

Update-docket.php

<?php
//error_reporting(0);
$link = mysqli_connect("localhost", "home_db", "root", "");

// Check connection 
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$sql = 'SELECT * FROM prod_details';
$result = mysqli_query($link,$sql);

while ($row = mysqli_fetch_array($result))
{ 
     $Quantity1 = $_POST['p_'.$row['id']. ''];          
     $id = $row['id'];
     $store = $row['rstore'];
     // echo $store;

     foreach ($_POST as $key => $value) 
     {}

     if(!empty($Quantity1)) {
         $query="update prod_details SET rdocket='$Quantity1' WHERE id='$id'";
         if(mysqli_query($link, $query)) {
            header('Location: /docket-details.php');
            exit();
         } else {
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
         }
      }
  }

docket-details.php

<form class="form-horizontal" action="update-docket.php" method="post" name="myform" autocomplete="off">
  <button  type='submit'  name='submit' class='btn c-theme-btn c-btn-square c-btn-uppercase c-btn-bold'>Submit</button>

Please help!!!

treyBake
  • 6,440
  • 6
  • 26
  • 57
Sharayu
  • 15
  • 1
  • 2
  • 8

7 Answers7

3

You're Location line is incorrect, redirect using:

header('Location: /url');

https://secure.php.net/manual/en/function.header.php

update

I reckon your if statement is not being met. Try enabling error logging again and var_dumping inside each conditional section to see where the real problem lies. Now you've updated your code to use header it should in theory work. But my guess is $_POST not returning what you want/expect.

treyBake
  • 6,440
  • 6
  • 26
  • 57
0

Edited!

Try this:

header('Location: /docket-details.php');
// i'm sorry, forgot to add exit
exit();

of course you can fill the full url if you refer.

Important: No output should be made before this line as it will generate a header. Not a single echo before sending the response header!

Reflective
  • 3,854
  • 1
  • 13
  • 25
  • No output should be made before this line as it will generate a header. Not a single echo before sending the response header! – Reflective Jul 05 '18 at 10:10
  • I have updated my code. still facing same issue. It is only redirecting to update-details.php – Sharayu Jul 05 '18 at 10:23
0

It should be something like that:

if(!empty($Quantity1)) {
    $query="update prod_details SET rdocket='$Quantity1' WHERE id='$id'";
    $res = mysqli_query($link, $query);

    if($res) {
        header('Location: /docket-details.php', true, '200');
    } else {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}
treyBake
  • 6,440
  • 6
  • 26
  • 57
RB_
  • 1,195
  • 15
  • 35
  • This page isn’t working is currently unable to handle this request. HTTP ERROR 500 – Sharayu Jul 05 '18 at 10:13
  • Well, it is hard to say what is wrong from your description. Try to break down the problem to a small chunks, and solve each of them. I suggest you to set the condition to true inside the if - if(!empty($Quantity1)) = if (true), and then leave only redirect line inside the if body, see if it works. If yes, then try executing query, if it doesn't work it can be another problem. – RB_ Jul 05 '18 at 10:17
  • 1
    @RB_ the error is quite easy. It's the `'` before the `/` of the url – SuperDJ Jul 05 '18 at 10:20
0

you have "update-details.php" but you reference "update-docket.php" in your form action. So the form action redirects to a different file.

As stated by @thisGuyHasTwoThumbs your Location line is incorrect.

header('Location: /url');
exit();

You also want to exit() as it will prevent any execution of code after that line.

https://secure.php.net/manual/en/function.header.php https://secure.php.net/manual/en/function.exit.php

Jus for the details:

"update prod_details SET rdocket='$Quantity1' WHERE id='$id'"

Wil look much cleaner and is easier to read like:

"UPDATE `prod_details` SET `rdocket` = '$Quantity1' WHERE `id` ='$id'"

Keeps MySQL words in capital. Wrap column and table names in backticks will prevent "MySQL serverd word error"

Making it:

<form class="form-horizontal" action="update-docket.php" method="post" name="myform" autocomplete="off">

<?php
if (!empty( $Quantity1) )
{
    $query = "UPDATE `prod_details` SET `rdocket` = '$Quantity1' WHERE `id` ='$id'";
    if( mysqli_query( $link, $query ) )
    {
        header( 'Location: /docket-details.php' );
        exit();
    } else {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}

The variable $Quantity1 is open for SQL-injection. To resolve this you can escape the variable if it is a string but you should use prepared statement.

SuperDJ
  • 7,488
  • 11
  • 40
  • 74
0

It's also best to not output anything before the header() .. eg; don't echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

Zlatan
  • 13
  • 8
0

It seems your file is named "Update-docket.php", with a capital U. But in the action of the form you call for action="update-docket.php".

-1

header(location:pagename with extension)

If the above code is not running properly and it give error that "header does not change" the put the below line on the top of the page and bottom of the page respectively

ob_start() - on the top of the page ob_end_flush() - bottom of the page