只要变量$ pid等于1、2或4,我就有一些PHP代码确实可以正常工作。但是当我将$ 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运行它时,我得到“错误”的回音。 有人可以帮我解决这个问题! 谢谢!
我确实有一个ID为3的数据库记录。 https://gyazo.com/509bed3b9dde9dfed6802ab9908d81b6
答案 0 :(得分:0)
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);
}