作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理一个复选框表,提交的数据应该保存在mysql数据库中。表中有两列,自动增量Id,另一列用于保存复选框的值。
目前,即使我选择多个值,一次也只能保存一个值,也只会保存最新的一个值。代码如下:
report.php
<?php
$active = "report";
require_once 'pages/header.php';
require_once './functions/schema-functions.php';
$course = Schema::getCourse();
$objective = Schema::getObjective();
?>
<form id="addReport" action ='./functions/report-functions.php' method="post">
<table id="table1" class="table">
<?php
echo '<tr><th>Objectives</th>';
for ($i = 0; $i < count($course); $i++) {
echo '<th id = "rotate1">'. $course[$i]->commonName . '</th>';
}
echo '</tr>';
for ($y = 0; $y < count($objective); $y++) {
echo '<tr><th class=row-header>'.$objective[$y]->objective.'</th>';
for ($x = 0; $x < count($course); $x++) {
echo "<td><input name='check[]' type=checkbox value=c".$course[$x]->courseId."-o".$objective[$y]->objectiveId." id=checked></td>";
}
echo '</tr>';
}
?>
</table>
<input type="submit" name= "submit" value= "Submit"/>
报告函数.php
<?php
require_once 'db-connect.php';
if(isset($_POST['submit'])){
{
$conn = DatabaseConnection::getConnection();
$conn->beginTransaction();
if(isset($_POST['check'])){
foreach($_POST['check'] as $value){
$sql = $conn->prepare("INSERT INTO Report (ColRow) VALUES
('$value')");
}
if ($sql->execute(array( ':checked' => $checked))) {
$conn->commit();
return true;
} else {
$conn->rollback();
return false;
}
}
}
}
?>
我想将多个选定的复选框保存在数据库的不同行中,提交后,我想再次显示表格并选中复选框,并且用户应该能够再次进行检查并提交数据。
最佳答案
要保存记录,您可以尝试这样:
<?php
if( isset( $_POST['submit'], $_POST['check'] ) ){
try{
require_once 'db-connect.php';
$conn = DatabaseConnection::getConnection();
$sql='insert into `report` ( `colrow` ) values ( :value )';
$stmt = $conn->prepare( $sql );
if( $stmt ){
$conn->beginTransaction();
foreach( $_POST['check'] as $index => $value ) {
$result = $stmt->execute( [ ':value' => $value ] );
if( !$result ) {
throw new Exception( sprintf( 'Failed to execute query %d for %s', $index, $value ) );
}
}
$conn->commit();
exit();
}
}catch( Exception $e ){
$conn->rollback();
exit( $e->getMessage() );
}
}
?>
关于php - 如何在数据库的不同行中保存多个复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55576010/
在我的 android 应用程序中,我想在同一行中实现 2 个文本字段。如果文本太长可以转到下一行。所以我使用了更精简的水平布局并添加了 2 个文本字段。 [例如:TextView1、TextV
由于node使用了javascript,它可以充当webrtc peer吗? 然后我可以将它编码为 VP8 流并广播给所有其他对等方吗? 最佳答案 WebRTC 是一个浏览器 API 而不是 Java
我是一名优秀的程序员,十分优秀!