我有一个复选框输入,以便在"sangamdb“数据库中的”献血者“表中的"act”列中插入活动。
但我似乎找不到正确的方法将表单中的多个选项插入到数据库表中。
除了这个元素,其他元素都能正常工作。
到目前为止我的代码是:
表格:
<form role="form" action = "addeddonor.php" method = "post">
<div class="form-group">
<label for="exampleInputEmail1">Activites</label><br>
<input type="checkbox" id="Football" name="act[]" value="Football">
<label for="Football">FootBall</label><br>
<input type="checkbox" id="Basketball" name="act[]" value="Basketball">
<label for="Basketball">BasketBall</label><br>
<input type="checkbox" id="Nattation" name="act[]" value="Nattation">
<label for="Nattation">Nattation</label><br>
<input type="checkbox" id="Karate" name="act[]" value="Karate">
<label for="Karate">Karate</label><br>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
PHP:(它不会将值插入数据库)
$checkBox = implode(',', $_POST['act']);
if(isset($_POST['submit']))
{
$query="INSERT INTO blooddonor (act) VALUES ('" . $checkBox . "')";
mysql_query($query) or die (mysql_error() );
echo "Complete";
}
if(isset($_POST['name'])){
$name = $_POST["name"];
$gender = $_POST["gender"];
$dob = $_POST["dob"];
$weight = $_POST["weight"];
$contact = $_POST["contact"];
$bloodtype = $_POST["bloodtype"];
$adress = $_POST["adress"];
include 'conn.php';
//code after connection is successfull
$qry = "insert into blooddonor(name,gender,dob,weight,contact,bloodtype,adress) values ('$name','$gender','$dob','$weight','$contact','$bloodtype','$adress')";
$result = mysqli_query($conn,$qry); //query executes
if(!$result){
echo"ERROR";
}else {
echo" <div style='text-align: center'><h1>ADDED SUCCESSFULLY</h1>";
echo" <a href='index.php' div style='text-align: center'><h3>Go Back</h3>";
}
}else{
echo"<h3>YOU ARE NOT AUTHORIZED TO REDIRECT THIS PAGE. GO BACK to <a href='index.php'> DASHBOARD </a></h3>";
}
数据库:
CREATE TABLE IF NOT EXISTS `blooddonor`
(
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`gender` varchar(20) NOT NULL,
`dob` date NOT NULL,
`weight` int(5) NOT NULL,
`contact` int(10) NOT NULL,
`bloodtype` varchar(3) NOT NULL,
`adress` varchar(50) NOT NULL,
`act` varchar(50) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
发布于 2021-02-03 23:53:06
复选框表示值的列表。用户可以选择多个复选框,这意味着对于每个用户记录,您可能有一个值列表。因此,需要数据库中的另一个表来存储这些选项。实际上,您可能总共需要三个表:献血者、活动、activities_per_donor。有关更多信息,请参见What's the best way to store Checkbox Values in MySQL Database?
如何设计表取决于您自己,但activities_per_donor至少需要有两列: user_id和activity。还应在两列上创建复合主键,以避免重复值。活动列应该引用第三个表中预定义的活动列表,这样用户就不能插入无效的活动。
当正确创建表单并将复选框命名为数组(即name="act[]"
)时,您将收到在$_POST['act']
变量中选择的值数组。如果没有选择任何值,那么这个变量将不会被设置,所以您也需要检查它。您需要处理这个数组,并将每个元素作为新行插入到activities_per_donor表中。
如何使用PDO存储多个复选框
大多数情况下,您将使用PDO与数据库进行交互。若要插入执行准备语句所需的值,请执行以下操作。您需要将捐助方数据插入到一个表中,并将其活动插入另一个表中,这要求将两个插入都包装在事务中。
$pdo = new \PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'user', 'password', [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_EMULATE_PREPARES => false
]);
if (isset($_POST["name"], $_POST["gender"], $_POST["dob"], $_POST["weight"], $_POST["contact"], $_POST["bloodtype"], $_POST["adress"])) {
$pdo->beginTransaction();
// Insert blood donor
$stmt = $pdo->prepare('INSERT INTO blooddonor(name,gender,dob,weight,contact,bloodtype,adress) VALUES (?,?,?,?,?,?,?)');
$stmt->execute([
$_POST["name"],
$_POST["gender"],
$_POST["dob"],
$_POST["weight"],
$_POST["contact"],
$_POST["bloodtype"],
$_POST["adress"],
]);
$donor_id = $pdo->lastInsertId();
// Insert donor's acitvities
if(isset($_POST['act'])) {
$stmt = $pdo->prepare('INSERT INTO activities_per_donor(donor_id, activity) VALUES (?,?)');
$stmt->bindValue(1, $donor_id);
$stmt->bindParam(2, $activity);
foreach ($_POST['act'] as $activity) {
$stmt->execute();
}
}
$pdo->commit();
}
如何使用mysqli存储多个复选框
如果您必须使用mysqli,您仍然可以使用非常类似的代码实现相同的功能。再次,我们启动一个事务并执行2个准备好的语句,然后将其提交到数据库中。
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'password', 'test');
$mysqli->set_charset('utf8mb4'); // always set the charset
if (isset($_POST["name"], $_POST["gender"], $_POST["dob"], $_POST["weight"], $_POST["contact"], $_POST["bloodtype"], $_POST["adress"])) {
$mysqli->begin_transaction();
// Insert blood donor
$stmt = $mysqli->prepare('INSERT INTO blooddonor(name,gender,dob,weight,contact,bloodtype,adress) VALUES (?,?,?,?,?,?,?)');
$stmt->bind_param('sssssss', $_POST["name"], $_POST["gender"], $_POST["dob"], $_POST["weight"], $_POST["contact"], $_POST["bloodtype"], $_POST["adress"]);
$stmt->execute();
$donor_id = $mysqli->insert_id;
// Insert donor's acitvities
if(isset($_POST['act'])) {
$stmt = $mysqli->prepare('INSERT INTO activities_per_donor(donor_id, activity) VALUES (?,?)');
$stmt->bind_param('ss', $donor_id, $activity);
foreach ($_POST['act'] as $activity) {
$stmt->execute();
}
}
$mysqli->commit();
}
https://stackoverflow.com/questions/66035773
复制相似问题