PHP - Redirect to URL after updating database

Redirect to URL after updating database using php

Redirect in Same Page after Updating database:

If you are going to update any data. after updating your data you want to redirect to url. I mean same page after updating your data you can use the following method:

Suppose you are in "view_requisition_details.php?rid=2&rdate=2020-02-18" this page. Now you want after updating your data you will be in this page then you have to follow the following code.

You have the Requisition List with Update Button. When you will click the update button then you will go "view_requisition_details.php?rid=2&rdate=2020-02-18" page. Now, get "rid" and "rdate" using $_GET method like ($rid = $_GET['rid']$rdate = $_GET['rdate']) and set this in window.location.replace(view_requisition_details.php?rid=$rid&rdate=rdate). In the following code i'm show how to Redirect to URL after updating database.
<?php
$rid = $_GET["rid"];
$rdate = $_GET["rdate"];

echo "<script type='text/javascript'>alert('Data has been Updated !'); 
window.location.replace(\"view_requisition_details.php?rid=".$rid."&rdate=".$rdate."\");
</script>";
?>

Redirect in Same Page with a Message:

You have a registration page which is "index.php". It's have two values "username" and "password". The action page of that registration page is "action.php". When you are going to complete your registration your will be perform on action page that is "action.php". Now if you want to redirect to url "index.php" then this article is helpful for you.

After completing your registration you want to redirect to url "index.php" with displaying a message named "Your Registration has been Successfully Completed!".

You can redirect using header() function Example: header('Location: index.php'); -If you want to display any message you can pass it through index.php?msg=your_message or flag
<?php
// user_name and user_password sent from form.
$user_name = $_POST['user_name']; 
$user_password = $_POST['user_password']; 

// To protect MySQL injection (more detail about MySQL injection) $user_name = stripslashes($user_name); 
$user_password = stripslashes($user_password); 
$user_name = mysql_real_escape_string($user_name); 
$user_password = mysql_real_escape_string($user_password);

$query = mysql_query("INSERT INTO users(`user_name`,`user_password`) VALUES('$user_name','$user_password')"); 
if($query) 
header('Location: index.php?msg=Your Registration has been successfully Completed!'); 
?>

Another Method is the below:

<?php 
$query=mysql_query("INSERT INTO users(`user_name`,`user_password`) VALUES('$user_name','$user_password')"); 
if($query) 
echo "Your Registration has been successfully Completed! <a href='index.php'>Continue</a>"; 
?>


EmoticonEmoticon