我有一些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代码:
<?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
发布于 2019-05-17 03:17:54
如果查询不成功,mysqli_query()将返回false,这意味着要么没有$pid的数据库记录,要么$data字符串格式有问题。
$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);
}
https://stackoverflow.com/questions/56175129
复制相似问题