0

Is it possible to change my search script to direct the client to a different page if there is an error?

Instead of having a text appear "No result have been found!", I would like them to be directed to search_error1.php.

Instead of having a text appear "You have not entered the search field", I would like them to be directed to search_error2.php.

<?php

$error = '';

if (isset($_POST['Submit'])) {
if (!empty($_POST['reg'])) {

$record = $_POST['reg'];

$query = mysql_query("SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");
$result = mysql_num_rows($query);

if ($result != 0) {

$row = mysql_fetch_array($query);

$first_name = $row['first_name'];
$last_name = $row['last_name'];
$reg = $row['reg'];

} else {$error = 'No result have been found!';}

} else {$error = 'You have not entered the search field, <a     href="javascript:history.back(1)">Go back</a>.';}

}

if (!empty($error)) { echo $error; } 

?>

2 Answers 2

2

Yes

Use header()

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

} else { header("Location: search_error1.php"); exit; }

} else { header("Location: search_error2.php"); exit; }

Your code (+ intended) would look like this:

<?php
if (isset($_POST['Submit'])) {
    if (!empty($_POST['reg'])) {

        $record = $_POST['reg'];

        $query = mysql_query("SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");
        $result = mysql_num_rows($query);

        if ($result != 0) {

            $row = mysql_fetch_array($query);

            $first_name = $row['first_name'];
            $last_name = $row['last_name'];
            $reg = $row['reg'];

        } else {
            header("Location: search_error1.php");
            exit;
        }

    } else {
        header("Location: search_error2.php");
        exit;
    }
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

If you are happy with my answer, please mark it as the accepted answer, you can do that by clicking on the check box outline to the left of this answer! :)
0

Instead of echoing the error, use a header redirect. Example.

header("Location: search_error1.php");

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.