如何使用 PHP 将复选框的值传递给 mySQL 数据库?
Posted
技术标签:
【中文标题】如何使用 PHP 将复选框的值传递给 mySQL 数据库?【英文标题】:How do I pass the values of my checkboxes with PHP to mySQL database? 【发布时间】:2021-12-28 18:09:07 【问题描述】:现在我的数据库中的值只是“数组”。因为,它是一个数组。它可以查看我是否检查了至少一件事,或者根本没有检查。如果我不选中任何复选框,它不会返回任何内容,如果我至少选中一件事,它会返回“Array”。
我以前没有使用过 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>
【问题讨论】:
【参考方案1】:假设found
应该包含一个以逗号分隔的复选框值列表,请使用implode()
从数组中创建它。
$found = isset($_POST['found']) ? implode(',', $_POST['found']) : '';
但是,这通常是糟糕的设计。见Is storing a delimited list in a database column really that bad?
【讨论】:
我会试试的,如果它有效的话。我仍处于 HTML 的最初几个月,我很快就会获得更多的 PHP 课程,但这现在必须工作,不过谢谢! edit 这行得通,太棒了,谢谢!以上是关于如何使用 PHP 将复选框的值传递给 mySQL 数据库?的主要内容,如果未能解决你的问题,请参考以下文章
如何将一组选中/未选中的复选框值传递给 PHP 电子邮件生成器?
如何通过 PHP 将多个单选按钮值作为所有单独的值传递给 MYSQL 数据库,但将它们分成组?
使用 AJAX 将复选框值 1 如果选中,如果不是 0 传递给 PHP