douliao5467 2013-08-13 04:07
浏览 62

PHP - 如果没有结果,重定向到其他页面

I would like to redirect the user to other page if there are no results.

what I meant like is, I am passing variables via url and using on the second page and if the the variables are empty I am able to redirect to another page.

However when the user changes the variable id in the url to something like

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

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

Nothing is displayed on the page so is there any way that I can redirect to other page in the above condition?

$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 />
", true); 
}
$mydb->close ();}
?>
</div>
  • 写回答

3条回答 默认 最新

  • donglu5000 2013-08-13 04:13
    关注

    Your condition requires both variables to be empty, if you want to redirect when any is empty you should use an OR (||):

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

    Also note that you cannot output anything to the browser before a header statement.

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了