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!!!

6
  • $result? Where did you define $result? Commented Jul 5, 2018 at 10:00
  • 1
    Seems your are using the wrong action on your form...is it right? Commented Jul 5, 2018 at 10:04
  • There is some extra { in the code Commented Jul 5, 2018 at 10:04
  • you have "update-details.php" but you reference "update-docket.php" in your form action Commented Jul 5, 2018 at 10:05
  • Maybe u sent the header already. Check: stackoverflow.com/a/21229246/10026429 for more info. Commented Jul 5, 2018 at 10:07

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Tried. Updated the code. No Changes. still the same.
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!

2 Comments

No output should be made before this line as it will generate a header. Not a single echo before sending the response header!
I have updated my code. still facing same issue. It is only redirecting to update-details.php
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);
    }
}

3 Comments

This page isn’t working is currently unable to handle this request. HTTP ERROR 500
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_ the error is quite easy. It's the ' before the / of the url
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.

Comments

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

Comments

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".

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.