通过提交显示所有行并更新所有行
Posted
技术标签:
【中文标题】通过提交显示所有行并更新所有行【英文标题】:Display all rows and update all rows by a submit 【发布时间】:2015-07-11 03:28:27 【问题描述】:我想通过 php 查询显示所有行,并通过 sql 中的提交按钮更新所有行。我以这种方式在下面可以显示所有行并通过其自己的提交按钮更新特定行。但我想通过一个提交按钮来更新所有内容。
所以,谢谢,我想循环更新。但我不明白在这种情况下该怎么做。
这是我的代码:
<?php
include_once('../db.php');
global $db;
$result = mysqli_query($dbh,"SELECT * FROM ppad");
if(!$result)
die("Database query failed: " . mysqli_error());
while($row = mysqli_fetch_assoc($result))
$id=$row['id'];
$name=$row['name'];
$date=$row['date'];
$title=$row['title'];
$Detail=$row['Detail'];
echo '<form action="padSproccess.php" method="POST">
<table border="1">
<tr>
<td >Date</td>
<td >Time</td>
<td >Name(url)</td>
<td >Detail</td>
</tr>
<tr>
<td ><input type="text" name="date" maxlength="2" value="'.$date.'"></td>
<td ><input type="text" name="title" maxlength="50" value="'.$title.'"></td>
<td ><input type="text" name="name" maxlength="50" value="'.$name.'"></td>
<td ><input type="text" name="Detail" maxlength="100" value="'.$Detail.'"></td>
<input type="hidden" name="id" value="'.$id.'">
</tr>
</table>
<input type="submit" name="submit" id="submit" value="Submit">
</form>';
?>
padSproccess.php
include("../db.php");
global $db;
if(isset($_POST['submit']))
$date = mysqli_real_escape_string($dbh,$_POST['date']);
$title = mysqli_real_escape_string($dbh,$_POST['title']);
$name = mysqli_real_escape_string($dbh,$_POST['name']);
$Detail = mysqli_real_escape_string($dbh,$_POST['Detail']);
$id = mysqli_real_escape_string($dbh,$_POST['id']);
// update data in mysql database
$update = mysqli_query($dbh,"UPDATE ppad SET date='$date', month='$month', name='$name', Detail='$Detail' WHERE id = '$id'");
// if successfully updated.
【问题讨论】:
当前代码遇到的具体问题是什么? 【参考方案1】:您首先需要做的是有一个整体的表单,而不是每个表单(除非您想使用 javascript 来触发 ajax 调用)。所以你需要做的是确保每一行都可以与一个特定的 id 相关联:
<?php
include_once '../db.php';
$result = mysqli_query($dbh, "SELECT * FROM ppad");
if(!$result)
die("Database query failed: " . mysqli_error());
?>
<form action="padSproccess.php" method="POST">
<table border="1">
<thead>
<tr>
<td >Date</td>
<td >Time</td>
<td >Name(url)</td>
<td >Detail</td>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result))
$id = $row['id'];
$name = $row['name'];
$date = $row['date'];
$title = $row['title'];
$Detail = $row['Detail'];
echo '
<tr>
<td ><input type="text" name="date[' . $id . ']" maxlength="2" value="'.$date.'"></td>
<td ><input type="text" name="title[' . $id . ']" maxlength="50" value="'.$title.'"></td>
<td ><input type="text" name="name[' . $id . ']" maxlength="50" value="'.$name.'"></td>
<td ><input type="text" name="Detail[' . $id . ']" maxlength="100" value="'.$Detail.'"></td>
</tr>
';
?>
</tbody>
</table>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
然后在padSproccess.php
中,您将收到names
、dates
、titles
和Details
的数组,每一个都由行的ID
键入。所以会变成这样:
<?php
include "../db.php";
if(isset($_POST['submit']))
$ids = array_keys($_POST['name']);
foreach ($ids as $id)
$date = mysqli_real_escape_string($dbh,$_POST['date'][$id]);
$title = mysqli_real_escape_string($dbh,$_POST['title'][$id]);
$name = mysqli_real_escape_string($dbh,$_POST['name'][$id]);
$Detail = mysqli_real_escape_string($dbh,$_POST['Detail'][$id]);
$id = mysqli_real_escape_string($id);
// update data in mysql database
$update = mysqli_query($dbh,"UPDATE ppad SET date='$date', month='$month', name='$name', Detail='$Detail' WHERE id = '$id'");
// if successfully updated.
【讨论】:
【参考方案2】:试试这个。当然对于我还没有启动的数据库,可能会出错。
<?php
include_once('../db.php');
global $db;
$result = mysqli_query($dbh,"SELECT * FROM ppad");
if(!$result)
die("Database query failed: " . mysqli_error());
?>
<form action="padSproccess.php" method="POST">
<?php
while($row = mysqli_fetch_assoc($result))
$id=$row['id'];
$name=$row['name'];
$date=$row['date'];
$title=$row['title'];
$Detail=$row['Detail'];
echo '
<table border="1">
<tr>
<td >Date</td>
<td >Time</td>
<td >Name(url)</td>
<td >Detail</td>
</tr>
<tr>
<td ><input type="text" name="ar['.$id.'][date]" maxlength="2" value="'.$date.'"></td>
<td ><input type="text" name="ar['.$id.'][title]" maxlength="50" value="'.$title.'"></td>
<td ><input type="text" name="ar['.$id.'][name]" maxlength="50" value="'.$name.'"></td>
<td ><input type="text" name="ar['.$id.'][Detail]" maxlength="100" value="'.$Detail.'"></td>
</tr>
</table>
';
?>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
<?php
include("../db.php");
global $db;
if(isset($_POST['submit']))
foreach($_POST['ar'] as $id=>$dat)
$date = mysqli_real_escape_string($dbh,$dat['date']);
$title = mysqli_real_escape_string($dbh,$dat['title']);
$name = mysqli_real_escape_string($dbh,$dat['name']);
$Detail = mysqli_real_escape_string($dbh,$dat['Detail']);
$id = mysqli_real_escape_string($dbh,$id]);
// update data in mysql database
$update = mysqli_query($dbh,"UPDATE ppad SET date='$date', month='$month', name='$name', Detail='$Detail' WHERE id = '$id'");
// if successfully updated.
?>
【讨论】:
【参考方案3】:为此,您需要将代码更新为
<?php
include_once('../db.php');
global $db;
$result = mysqli_query($dbh,"SELECT * FROM ppad");
if(!$result)
die("Database query failed: " . mysqli_error());
?>
<form action="padSproccess.php" method="POST">
<table border="1">
<tr>
<td >Date</td>
<td >Time</td>
<td >Name(url)</td>
<td >Detail</td>
</tr>
<?php
while($row = mysqli_fetch_assoc($result))
$id=$row['id'];
$name=$row['name'];
$date=$row['date'];
$title=$row['title'];
$Detail=$row['Detail'];
echo '<tr>
<td ><input type="text" name="date[]" maxlength="2" value="'.$date.'"></td>
<td ><input type="text" name="title[]" maxlength="50" value="'.$title.'"></td>
<td ><input type="text" name="name[]" maxlength="50" value="'.$name.'"></td>
<td ><input type="text" name="Detail[]" maxlength="100" value="'.$Detail.'"></td>
<input type="hidden" name="id[]" value="'.$id.'">
</tr>';
?>
</table>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
现在,在您的 padSproccess.php
中,您将在变量中收到一组结果,这些结果将使用 foreach
循环进行更新
【讨论】:
以上是关于通过提交显示所有行并更新所有行的主要内容,如果未能解决你的问题,请参考以下文章
MS-ACCESS:删除除 top 1 之外的所有行并从查询中更新表
如何将 extjs 网格限制为仅显示 100 行并提供所有行作为下载?
从数据库中检索特定行并显示特定行中的所有数据并使用 php 发送 json