現在我的資料庫中的值只是“陣列”。因為,它是一個陣列。它可以看到我是否至少檢查了一件事情,或者根本沒有檢查過任何事情。如果我不選中任何復選框,它不會回傳任何內容,如果我至少選中一件事,它會回傳“陣列”。
我以前沒有接觸過 PHP,我們也沒有上過任何關于這方面的課程,但經過幾天的嘗試,我還是想不通。

<?php
// Show all errors (for educational purposes)
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 0);
// Constanten (connectie-instellingen databank)
define('DB_HOST', '');
define('DB_USER', '');
define('DB_PASS', '');
define('DB_NAME', '');
date_default_timezone_set('Europe/Brussels');
// Verbinding maken met de databank
try {
$db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8mb4', DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Verbindingsfout: ' . $e->getMessage();
exit;
}
$name = isset($_POST['name']) ? (string)$_POST['name'] : '';
$message = isset($_POST['message']) ? (string)$_POST['message'] : '';
$found = isset($_POST['found']) ? (string)$_POST['found'] : '';
$msgName = '';
$msgMessage = '';
// form is sent: perform formchecking!
if (isset($_POST['btnSubmit'])) {
$allOk = true;
// name not empty
if (trim($name) === '') {
$msgName = 'Gelieve een naam in te voeren';
$allOk = false;
}
if (trim($message) === '') {
$msgMessage = 'Gelieve een boodschap in te voeren';
$allOk = false;
}
// end of form check. If $allOk still is true, then the form was sent in correctly
if ($allOk) {
// build & execute prepared statement
$stmt = $db->prepare('INSERT INTO messages (sender, message, found, added_on) VALUES (?, ?, ?, ?)');
$stmt->execute(array($name, $message, $found, (new DateTime())->format('Y-m-d H:i:s')));
// the query succeeded, redirect to this very same page
if ($db->lastInsertId() !== 0) {
header('Location: formchecking_thanks.php?name=' . urlencode($name));
exit();
} // the query failed
else {
echo 'Databankfout.';
exit;
}
}
}
?><!DOCTYPE html>
<html lang="nl">
<head>
<title>Testform</title>
<meta charset="UTF-8"/>
<link href="https://unpkg.com/@csstools/normalize.css" rel="stylesheet"/>
<link rel="stylesheet" href="./css/contact.css">
<link rel="stylesheet" href="/css-algemeen/stylesheet.css">
</head>
<body>
<header>
<nav>
<a href="/" class="logo-nav">Loran</a>
<ul>
<li><a href="/About">Over mij</a></li>
<li class="dropdown">
<a href="/Work">Projecten</a>
<div class="dropdown-menu-content">
<a href="/Work/Project1/">Project 1</a>
<a href="/Work/Project2/">Project 2</a>
<a href="/Work/Project3/">Project 3</a>
<a href="/Work/Project4/">Project 4</a>
<a href="/Work/Project5/">Project 5</a>
<a href="/Work/Project6">Project 6</a>
<a href="/Work/Project7/">Project 7</a>
<a href="/Work/Project8/">Project 8</a>
</div>
</li>
<li><a href="/Blog">Blog</a></li>
<li><a href="/Contact" class="current">Contact</a></li>
</ul>
</nav>
</header>
<main class="container">
<div class="contactformulier">
<h1>Contacteer mij</h1>
<p>Heb je een vraag, of wil je samenwerken? Contacteer mij!</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div class="form-container">
<label for="name">Uw naam</label>
<input type="text" id="name" name="name" value="<?php echo $name; ?>" class="input-text"/>
<span class="message error"><?php echo $msgName; ?></span>
</div>
<div class="form-container">
<label for="message">Uw bericht</label>
<textarea name="message" id="message" rows="5" cols="40"><?php echo $message; ?></textarea>
<span class="message error"><?php echo $msgMessage; ?></span>
</div>
<div class="form-container">
<fieldset>
<legend>Hoe heeft u mij gevonden?</legend>
<div class="fieldset-form">
<label for="vrienden">Vrienden</label>
<input type="checkbox" name="found[]" id="vrienden" value="vrienden">
</div>
<div class="fieldset-form">
<label for="socials">Sociale media</label>
<input type="checkbox" name="found[]" id="socials" value="social">
</div>
<div class="fieldset-form">
<label for="google">Google</label>
<input type="checkbox" name="found[]" id="google" value="google">
</div>
<div class="fieldset-form">
<label for="andere">Andere</label>
<input type="checkbox" name="found[]" id="andere" value="andere">
</div>
</fieldset>
</div>
<input type="submit" id="btnSubmit" name="btnSubmit" value="Verstuur"/>
</form>
</div>
</main>
<footer>
<p>Footer © 2021 Loran Maes - <a href="https://www.linkedin.com/in/loran-maes-524a05160/" target="blank">LinkedIn</a> - <a
href="https://www.instagram.com/loran_maes/" target="blank">Instagram</a> - copyright - Dendermondesteenweg 809 9070 Destelbergen</p>
</footer>
</body>
</html>
uj5u.com熱心網友回復:
假設found應該包含一個以逗號分隔的復選框值串列,用于implode()從陣列中創建該串列。
$found = isset($_POST['found']) ? implode(',', $_POST['found']) : '';
然而,這通常是糟糕的設計。請參閱在資料庫列中存盤分隔串列真的那么糟糕嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/396002.html
上一篇:如何在CSS和HTML中使用過渡
