php - 如何一次编辑数据库中的多个值
问题描述
我正在为我的网站制作一个管理面板。我在一次编辑数据库中的许多值时遇到问题。我的网站是https://jaguarz.cf但管理面板对普通访问者不可见,只有我可以控制它
警告:count():参数必须是数组或实现 Countable 的对象
我在计算 id si 想要更改的数量时收到此错误
if (isset($_POST['edit_features'])) {
$count=count($_POST['id_edit']);
for($i=0;$i<$count;$i++){
$sql1="UPDATE projects
SET name='" . $_POST['name_edit'][$i] . "',
link='" . $_POST['link_edit'][$i] . "',
rank='" . $_POST['rank_edit'][$i] . "'
WHERE id='" . $_POST['id'][$i] . "'";
$result1=mysqli_query($db,$sql1);
}
}
这是我的表格的一个例子:
<form method="post" action="admin_panel" style="margin:auto;" class="resize form-style-8">
<div class="table-responsive" style="width:99%;">
<table class="table table-bordered">
<thead>
<tr>
<th style="color: rgb(0,0,0);font-size: 20px;">Id</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Name</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Link</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Rank</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="id_edit" value="1" readonly></td>
<td><input type="text" name="name_edit" value="Videos"></td>
<td><input type="text" name="link_edit" value="https://www.youtube.com/channel/UCXqIrdlG-gsfSGuFUt0v-1g"></td>
<td><input type="text" name="rank_edit" value="AAA"></td>
</tr>
<tr>
<td><input type="text" name="id_edit" value="2" readonly></td>
<td><input type="text" name="name_edit" value="Basic Message Encrypter And Decrypter"></td>
<td><input type="text" name="link_edit" value="project?page=en-decrypter"></td>
<td><input type="text" name="rank_edit" value="B"></td>
</tr>
<tr>
<td><input type="text" name="id_edit" value="5" readonly></td>
<td><input type="text" name="name_edit" value="Calculator"></td>
<td><input type="text" name="link_edit" value="project?page=calculator"></td>
<td><input type="text" name="rank_edit" value="E"></td>
</tr>
</tbody>
</table>
<div style="margin: 20px 0px 0px 0px">
<button type="submit" name="edit_features" class="btn btn-info text-justify" style="margin: 10px 0px 10px 0px; width:auto; text-align:center !important;">Edit Projects</button>
</div>
</div><br><br>
<div class="table-responsive" style="width:99%;">
<table class="table table-bordered">
<thead>
<tr>
<th style="color: rgb(0,0,0);font-size: 20px;">Id</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Name</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Link</th>
<th style="color: rgb(0,0,0);font-size: 20px;">Rank</th>
</tr>
</thead>
<tbody>
<tr>
<td>Auto Generated</td>
<td><input type="text" name="name_add"></td>
<td><input type="text" name="link_add"></td>
<td><input type="text" name="rank_add"></td>
</tr>
</tbody>
</table>
<div style="margin: 20px 0px 0px 0px">
<button type="submit" name="add_features" class="btn btn-danger text-justify" style="margin: 10px 0px 10px 0px; width:auto; text-align:center !important;">Add Projects</button>
</div>
</div>
</form>
我希望它一次更改所有值
解决方案
您的代码有几个主要问题:
<input type="text" name="id_edit" value="1" disabled>
(根据您的原始代码)
在这些输入
1)disabled
浏览器不提交元素。您应该将它们更改为readonly
(正如您在编辑中已经完成的那样)
2)name="id_edit"
重复多次,但您不能有多个具有相同名称的项目 - 服务器只会看到其中一个。要使其以相同名称提交一组值,请将其更改为name="id_edit[]"
. 您需要对所有可重复字段执行此操作。
最后一个问题是您的服务器代码中的拼写错误:
WHERE id='" . $_POST['id'][$i] . "'";
需要是
WHERE id='" . $_POST['id_edit'][$i] . "'";
因为您没有提交id
表单中调用的任何字段。
最后我要发出一个更广泛的警告,对于一个你需要纠正的问题,现在你已经让表单在基本级别上工作:你的代码容易受到 SQL 注入攻击。您应该使用参数化查询和准备好的语句来帮助防止攻击者通过使用恶意输入值来破坏您的数据库。http://bobby-tables.com给出了风险解释,以及如何使用 PHP / mysqli 安全地编写查询的一些示例。切勿将未经处理的数据直接插入 SQL。以您现在编写代码的方式,有人可以轻松窃取、错误更改甚至删除您的数据。
推荐阅读
- c# - 如何将 SQLite 数据库文件嵌入到 WPF 应用程序中
- c# - C#、访问、查询 T/F 条件和检索其他列字符串
- git - 不小心做了一个 Visual Studio“更新”并丢失了我的所有更改。他们永远消失了吗?
- php - Move additional information from product tab under add to cart button in WooCommerce
- java - 来自 Spring Boot 的指标未显示在 Prometheus 中
- rest-assured-jsonpath - 通过发送变量搜索 RestASSURED 值
- php - PHP if statement strpos === false with an or ||
- c# - C#二维数组验证表单中的条目
- asp.net-mvc - 排序最新消息 MVC
- c# - xamarin.forms 的 SQLite 扩展:级联删除不起作用,但代码编译没有错误