connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Change character set to utf8
mysqli_set_charset($conn,"utf8");
//一定要加上面這行,不然中文會變亂碼
$sql = "SELECT * FROM articles";
$result = $conn->query($sql);
//下面開始插入方式
$title = $_POST['title'];
$links = $_POST['links'];
$type = $_POST['type'];
$myps = $_POST['myps'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["title"])) {
$titleErr = "需要有文章標題";
} else {
$title = test_input($_POST["title"]);
}
if (empty($_POST["links"])) {
$linksErr = "需要有文章連結";
} else {
$links = test_input($_POST["links"]);
}
if (empty($_POST["type"])) {
$website = "";
} else {
$type = test_input($_POST["type"]);
}
if (empty($_POST["myps"])) {
$myps = "";
} else {
$myps = test_input($_POST["myps"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$sql = "INSERT INTO articles (title, links, type, myps)
VALUES ('$title', '$links', '$type', '$myps')";
if ($conn->query($sql) === TRUE) {
// echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
?>