PHP-如果没有结果,则重定向到其他页面
如果没有结果,我想将用户重定向到其他页面 我的意思是,我通过url传递变量,并在第二个页面上使用,如果变量为空,我可以重定向到另一个页面 但是,当用户将url中的变量id更改为PHP-如果没有结果,则重定向到其他页面,php,mysqli,Php,Mysqli,如果没有结果,我想将用户重定向到其他页面 我的意思是,我通过url传递变量,并在第二个页面上使用,如果变量为空,我可以重定向到另一个页面 但是,当用户将url中的变量id更改为 index.php?产品tit/=how+to+deal+with%20&%20item id pr=15 toindex.php?产品tit/=how+to+%20&%20item id pr= 页面上没有显示任何内容,因此在上述情况下是否有任何方法可以重定向到其他页面 $title=urldecode($_GET[
index.php?产品tit/=how+to+deal+with%20&%20item id pr=15
toindex.php?产品tit/=how+to+%20&%20item id pr=
页面上没有显示任何内容,因此在上述情况下是否有任何方法可以重定向到其他页面
$title=urldecode($_GET['product-tit/');
$id=$_GET['item-id-pr'];
$mydb=newmysqli('localhost','root','database');
if(空($title)&&empty($\u GET['item-id-pr'])){
标题('Location:products.php');
}
否则{
$stmt=$mydb->prepare(“从产品中选择*,其中标题=?和id=?限制1”);
$stmt->bind_参数('ss',$title,$id);
$stmt->execute();
?>
您的条件要求两个变量都为空,如果要在任何变量为空时重定向,则应使用OR(|
):
还要注意的是,在
标题
语句之前,您不能向浏览器输出任何内容。有两件事需要检查
<?php
$id = $_GET['item-id-pr'];
$mydb = new mysqli('localhost', 'root', '', 'database');
// I am assuming variable name is "product-tit"
$title = urldecode($_GET['product-tit']);
if(trim($title) == "" || trim($_GET['item-id-pr']) == ""){
header('Location: products.php');
exit;
}
$stmt = $mydb->prepare("SELECT * FROM products where title = ? AND id = ? limit 1 ");
$stmt->bind_param('ss', $title, $id);
$stmt->execute();
$result = $stmt->get_result();
if( $result->num_rows == 0 ) {
// redirect user
header('Location: products.php');
exit;
}
?>
<div>
<?php
while ($row = $result->fetch_assoc()) {
echo wordwrap($row['price'], 15, "<br />\n", true);
}
$mydb->close ();
?>
</div>
测试
$\u 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
为什么不计算结果项,然后根据结果重新定向呢?是一个输入错误empty($GET\u['item-id-pr'])
应该是:empty($\u GET['item-id-pr'])
哦,是的。我会修复它给我调用未定义的方法mysqli\u stmt::GET\u selected\u rows()
如果
条件我无法准确了解发生了什么情况,那么它会自动重定向而不等待条件。如果变量为空,它不应该重定向吗?我认为在变量定义模式中存在一些问题变量名不应该有/(最后前进)。我将再次编辑此项
<?php
$id = $_GET['item-id-pr'];
$mydb = new mysqli('localhost', 'root', '', 'database');
// I am assuming variable name is "product-tit"
$title = urldecode($_GET['product-tit']);
if(trim($title) == "" || trim($_GET['item-id-pr']) == ""){
header('Location: products.php');
exit;
}
$stmt = $mydb->prepare("SELECT * FROM products where title = ? AND id = ? limit 1 ");
$stmt->bind_param('ss', $title, $id);
$stmt->execute();
$result = $stmt->get_result();
if( $result->num_rows == 0 ) {
// redirect user
header('Location: products.php');
exit;
}
?>
<div>
<?php
while ($row = $result->fetch_assoc()) {
echo wordwrap($row['price'], 15, "<br />\n", true);
}
$mydb->close ();
?>
</div>
<?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