Using PHP to redirect when SQL query appears

I am using this PHP script to return search input values ​​with matching URL values ​​in a MySQL database. The idea is to add them to the redirect to automatically go to this page.

<?php
$searchResults = $_POST['search'];
$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = 'pwd';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = "SELECT url
    FROM Table_1
    WHERE input = '" .$searchResults."'";

mysql_select_db('database_1');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    $redirect = $row['url'];
    header('Location:'.$redirect);  
} 

To catch any input that does not match the value in table_1, this If statement has been added. It will accept any irrelevant or incorrectly written inputs and redirect them to xyz.html

if (mysql_num_rows($retval) < 1)  {

header('Location: xyz.html');

}

Is this wrong? It seems to work, but I suppose there should be a cleaner way for this / it might be bad practice.

+4
source share
2

, .
, . .
:

if($retval)
{ .$row = mysql_fetch_array($retval, MYSQL_ASSOC);
 header('Location:'.$row);
}
else 
header('Location:xyz.html');

while, .

+2

, , , .

2 :

 1.If result is x then redirect to page A.

 2.If result is x then load view just similar like page A with same header ,footer.

, 2- , , , .

, Shopping cart, "", , Sorry stock is empty, no shoes, , , .

+2

Source: https://habr.com/ru/post/1584762/


All Articles