0

結果がない場合は、ユーザーを別のページにリダイレクトしたいと思います。

つまり、URL を介して変数を渡し、2 番目のページで使用しています。変数が空の場合は、別のページにリダイレクトできます。

ただし、ユーザーが URL の変数 id を次のように変更すると、

index.php?product-tit/=how+to+deal+with%20&%20item-id-pr=15

index.php?product-tit/=how+to+%20&%20item-id-pr=

ページに何も表示されないのですが、上記の状態で別のページにリダイレクトする方法はありますか?

$title = urldecode($_GET['product-tit/']);
$id = $_GET['item-id-pr'];
$mydb = new mysqli('localhost', 'root', '', 'database');

if(empty($title) && empty($_GET['item-id-pr'])){
header('Location: products.php');
}
else{
$stmt = $mydb->prepare("SELECT * FROM products where title = ? AND id = ? limit 1 ");
$stmt->bind_param('ss', $title, $id);
$stmt->execute();
?> 
<div>
<?php
$result = $stmt->get_result();
 while ($row = $result->fetch_assoc()) {
echo wordwrap($row['price'], 15, "<br />\n", true); 
}
$mydb->close ();}
?>
</div>
4

3 に答える 3

1

いずれかが空のときにリダイレクトする場合は、OR ( ||)を使用する必要があります。

if(empty($title) || empty($_GET['item-id-pr'])){
  header('Location: products.php');
  // make sure nothing more gets executed
  exit();
}

headerまた、ステートメントの前ではブラウザに何も出力できないことに注意してください。

于 2013-08-13T04:13:58.913 に答える
0

パラメータを他の変数に割り当てて他のことを行う前に$_GET、パラメータが設定されていて空でないかどうかをテストします。

<?php
if (!isset($_GET['product-tit/'], $_GET['item-id-pr'])
    || empty($_GET['product-tit/'])
    || empty($_GET['item-id-pr']))
{
    header('Location: products.php');
    // although note that HTTP technically requires an absolute URI
    exit;
}
// now assign $title and $id, initialize the db, etc
于 2013-08-13T04:49:39.527 に答える