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.
source
share