PHP tricky

header() 不加 exit() or die() 會往下執行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
session_start();
require_once('conn.php');
require_once('utils.php');

if (empty($_POST['content']) || empty($_POST['id'])) {
$nickname = $_POST['nickname'] ;
$id = $_POST['id'];
header("Location: update_content.php?errorCode=1&nickname=$nickname&id=$id");
// 不加會往下執行
// die("輸入不齊全!");
}

$sql = "update comment set content=? where id = ? and username=?";
$stmt= $conn->prepare($sql);
$stmt->bind_param('sss', $_POST['content'], $_POST['id'], $_SESSION['username']);
$result = $stmt->execute();
if (!$result) {
print_r($conn->error);
header('Location: update_content.php?errorCode=2&nickname=$_POST["nickname"]&id=$_POST["id"]');
}

$affect_rows = (int)$conn->affected_rows;
if ($affect_rows<1) {
header('Location: index.php?errorCode=3');
exit();
}

echo "編輯完成";
header('Location: index.php');
?>