我很难弄明白如何使用php将选中的框插入或未选中的数据库插入数据库。
我尝试了许多不同的方法,但没有一个工作,我认为我非常接近,但无法弄清楚问题。 顺便说一句,我使用Javascript并且从未使用PHP,除非这次。
的index.html
<html>
<head>
<style type="text/css">
@import "demo_page.css";
@import "header.ccss";
@import "demo_table.css";
@import "select.dataTables.min.css";
@import "jquery.dataTables.min.css";
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="RowGroupingWithFixedColumn.js"></script>
<script>$(document).ready(function(){load_(); console.log('load running')});</script>
</head>
<body id="dt_example">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="endpoints">
<thead>
<tr>
<th></th>
<th>Nr.</th>
<th>Java Class Name</th>
<th>http Method</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,DB_NAME);
$sql='SELECT * FROM url';
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
print $row['method'];
switch ($row['http_method']) {
case "GET":
echo "<tr class='gradeA'>";
break;
case "PUT":
echo "<tr class='gradeC'>";
break;
case "POST":
echo "<tr class='gradeU'>";
break;
case "DELETE":
echo "<tr class='gradeX'>";
break;
default:
echo "<tr>";
}
if($row['checked']){
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "' checked> </td>";
} else {
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "'> </td>";
}
echo "<td align=center >" . $row['number'] . "</td>";
echo "<td align=center >" . $row['class_name'] . "</td>";
echo "<td>" . $row['http_method'] . "</td>";
echo "<td style='font-weight:bold'>" . $row['endpoint'] . "</td>";
echo "</tr>";
}
if(isset($_POST['save'])){
echo "<script>console.log(" . $checkboxes.length . ");</script>";
$rows = $_POST['case'];
foreach($rows as $row){
$sql = "UPDATE url SET checked = 1 WHERE number = " . $case;
$result = mysqli_query($con,$sql);
}
}
mysqli_close($con);
echo "</tbody></table>";
echo "<input type='submit' name='save' id='save' value='Save' />";
?>
</body>
</html>
任何帮助都将不胜感激。
图片:
编辑: 解决了 - 这是代码:
<?php
if(isset($_POST['save'])){
echo "<script>console.log(" . $checkboxes.length . ");</script>";
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,DB_NAME);
$rows = $_POST['case'];
foreach($rows as $row){
$sql = "UPDATE url SET checked = 1 WHERE number = " . $row;
$result = mysqli_query($con,$sql);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
@import "demo_page.css";
@import "header.ccss";
@import "demo_table.css";
@import "select.dataTables.min.css";
@import "jquery.dataTables.min.css";
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="RowGroupingWithFixedColumn.js"></script>
<script>$(document).ready(function(){load_(); console.log('load running')});</script>
</head>
<body id="dt_example">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="endpoints">
<thead>
<tr>
<th></th>
<th>Nr.</th>
<th>Java Class Name</th>
<th>http Method</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect('sql7.freemysqlhosting.net','sql7117068','GZqaZj69G9','sql7117068');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,'sql7117068');
$sql='SELECT * FROM url';
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
print $row['method'];
switch ($row['http_method']) {
case "GET":
echo "<tr class='gradeA'>";
break;
case "PUT":
echo "<tr class='gradeC'>";
break;
case "POST":
echo "<tr class='gradeU'>";
break;
case "DELETE":
echo "<tr class='gradeX'>";
break;
default:
echo "<tr>";
}
if($row['checked']){
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "' checked> </td>";
} else {
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "'> </td>";
}
echo "<td align=center >" . $row['number'] . "</td>";
echo "<td align=center >" . $row['class_name'] . "</td>";
echo "<td>" . $row['http_method'] . "</td>";
echo "<td style='font-weight:bold'>" . $row['endpoint'] . "</td>";
echo "</tr>";
}
mysqli_close($con);
echo "</tbody></table>";
echo "<input type='submit' name='save' id='save' value='Save' />";
echo "</form>";
?>
</body>
</html>
PS:代码中可能存在许多泄漏和错误的编程风格,但我知道我不是PHP开发人员,如果我的服务器上存在不同的攻击并不重要。我只想解决问题并继续前进。没有时间停在每一点上。
答案 0 :(得分:1)
使用下面的代码检查是否选中了复选框。然后在某些变量上设置一些标志,以便相应地将数据插入到数据库中。
//html
<input type='checkbox' name='boxname' value='1' />
//php
<?php
if(isset($_POST['boxname'])
{
echo "check box is checked";
}
?>