【问题标题】:header location with id in php [duplicate]php中带有id的标头位置[重复]
【发布时间】:2018-03-03 17:31:36
【问题描述】:

是否可以在header() 中添加行 ID?我试图将header() 放在if 语句中,但我总是收到以下消息:

解析错误:语法错误、意外的 '' (T_ENCAPSED_AND_WHITESPACE)、需要标识符 (T_STRING) 或变量 (T_VARIABLE) 或数字 (T_NUM_STRING)

这是我的代码:

$result = mysqli_query($db_con,$query);
$target="Admin/upload/otherPic/".basename($_FILES['image']['name']);
if ($result){
    if(move_uploaded_file($_FILES['image']['tmp_name'], $target)){
        echo "<script>
            alert('Record updated successfully!');
            </script>";
        header("Location: admin-viewacc.php?id='$result['id'];'");
    }
    else{
        echo "Error updating record: " . mysqli_error($db_con);
    }
}

【问题讨论】:

  • 解析错误:语法错误,意外的 '' (T_ENCAPSED_AND_WHITESPACE),需要标识符 (T_STRING) 或变量 (T_VARIABLE) 或数字 (T_NUM_STRING)
  • 什么是$query

标签: php header


【解决方案1】:

您需要 fetch 记录集(假设它是 select 查询),并且不需要在标题字符串中的 ID 周围添加额外的单引号。

这很容易出错 - 如果您在调用某些 PHP 函数之前输出任何 html、空格等,您将触发错误。为此,不要添加 javascript 代码 - 而是在下一页显示一条消息。

if( $result ){

    if( move_uploaded_file( $_FILES['image']['tmp_name'], $target ) ){

        $result = mysqli_query( $db_con, $query );
        while( $rs=$result->fetch_object() )$id=$rs->id;


        $target = 'Admin/upload/otherPic/' . basename( $_FILES['image']['name'] );


        echo "
            <script>
                alert('Record updated successfully!');
            </script>";

        header( "Location: admin-viewacc.php?id={$id}" );

    } else{
        echo "Error updating record: " . mysqli_error( $db_con );
    }
}

【讨论】:

    【解决方案2】:

    尝试使用,

    header("Location: admin-viewacc.php?id=" . $result['id']);
    

    最好在header($string) 之后使用exit 语句,因为它有时会有点混乱。

    header("Location: admin-viewacc.php?id=" . $result['id']);
    exit;
    

    阅读 - header is not working when called within a function

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 2016-08-21
      • 1970-01-01
      相关资源
      最近更新 更多