Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-如果没有结果,则重定向到其他页面_Php_Mysqli - Fatal编程技术网

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

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[

如果没有结果,我想将用户重定向到其他页面

我的意思是,我通过url传递变量,并在第二个页面上使用,如果变量为空,我可以重定向到另一个页面

但是,当用户将url中的变量id更改为

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

to
index.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(
|
):


还要注意的是,在
标题
语句之前,您不能向浏览器输出任何内容。

有两件事需要检查

  • 检查传递的变量是否有值。在这种情况下,您已经应用了重定向
  • 若用户更改URL参数值,如示例中所示。您需要验证数据库是否返回与标题和ID对应的任何行。若不返回,则将用户重定向到其他页面
  • 这里可以是伪代码

    <?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