首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如果变量仅等于3,则脚本失败

如果变量仅等于3,则脚本失败
EN

Stack Overflow用户
提问于 2019-05-17 03:07:55
回答 1查看 31关注 0票数 0

我有一些PHP代码,只要我的变量$pid等于1、2或4,它们就能正常工作。但是,当我在$pid设置为3的情况下运行这段代码时,向数据库发送一些数据的查询失败。这在错误日志中:Cannot modify header information - headers already sent by (output started at /home/sndmipat/public_html/user-panel/industrihandel/upload.php:2) in /home/sndmipat/public_html/user-panel/industrihandel/upload.php on line 24

PHP代码:

代码语言:javascript
复制
<?php 
if (isset($_POST['subup'])) {
    $data = $_POST['textbox'];
    $pid = $_GET['pid'];

    include '../../connection.php';

    $sql = "UPDATE Industrihandel SET Content='$data' WHERE ID=$pid";
    $result = mysqli_query($conn, $sql);
    if($result) {
        switch ($pid) {
        case 1:
            header("Refresh:0;url=index.php?pid=1");
            exit;
        case 2:
            header("Refresh:0;url=omraden.php?pid=2");
            exit;
        case 3:
            header("Refresh:0;url=sortiment.php?pid=3");
            exit;
        case 4:
            header("Refresh:0;url=kontakt.php?pid=4");
            exit;
        }
    } else {
        echo 'ERROR';
    }
} else {
    echo 'Permission denied.';
    echo "<br>Error message = " . mysqli_error(); //Returns: "Error message = " without any error.
}
?>

因此,当我尝试在$pid =3的情况下运行此命令时,会得到"ERROR“回显。有人能帮我解决这个问题吗?谢谢!

我确实有一个ID为3的数据库记录。https://gyazo.com/509bed3b9dde9dfed6802ab9908d81b6

EN

回答 1

Stack Overflow用户

发布于 2019-05-17 03:17:54

如果查询不成功,mysqli_query()将返回false,这意味着要么没有$pid的数据库记录,要么$data字符串格式有问题。

代码语言:javascript
复制
$pid = 3;
$sql = "UPDATE Industrihandel SET Content='$data' WHERE ID = $pid";
$result = mysqli_query($conn, $sql);

if ($result) { // $result === false if no record exists with an ID of 3
    // ...
} else {
    echo 'ERROR: ' . mysqli_error($conn);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56175129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档