显示所有行并通过提交更新所有行

时间:2015-05-01 09:31:29

标签: php mysql loops mysqli foreach

我想通过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 width="100%" border="1">
<tr>
    <td width="10%">Date</td>
    <td width="14%">Time</td>
    <td width="20%">Name(url)</td>
    <td width="30%">Detail</td>
</tr>
<tr>
    <td width="10%"><input type="text" name="date" maxlength="2" value="'.$date.'"></td>
    <td width="14%"><input type="text" name="title" maxlength="50" value="'.$title.'"></td>
    <td width="20%"><input type="text" name="name" maxlength="50" value="'.$name.'"></td>
    <td width="30%"><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. 
}

3 个答案:

答案 0 :(得分:1)

为此,您需要将代码更新为

<?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 width="100%" border="1">
<tr>
    <td width="10%">Date</td>
    <td width="14%">Time</td>
    <td width="20%">Name(url)</td>
    <td width="30%">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 width="10%"><input type="text" name="date[]" maxlength="2" value="'.$date.'"></td>
    <td width="14%"><input type="text" name="title[]" maxlength="50" value="'.$title.'"></td>
    <td width="20%"><input type="text" name="name[]" maxlength="50" value="'.$name.'"></td>
    <td width="30%"><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循环进行更新

答案 1 :(得分:0)

试试这个。当然对于数据库我还没有开始,这可能是错误。

<?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 width="100%" border="1">
<tr>
    <td width="10%">Date</td>
    <td width="14%">Time</td>
    <td width="20%">Name(url)</td>
    <td width="30%">Detail</td>
</tr>
<tr>
    <td width="10%"><input type="text" name="ar['.$id.'][date]" maxlength="2" value="'.$date.'"></td>
    <td width="14%"><input type="text" name="ar['.$id.'][title]" maxlength="50" value="'.$title.'"></td>
    <td width="20%"><input type="text" name="ar['.$id.'][name]" maxlength="50" value="'.$name.'"></td>
    <td width="30%"><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. 
}
?>

答案 2 :(得分:0)

首先需要做的是拥有一个整体表单,而不是每个表单的形式(除非你想抛出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 width="100%" border="1">
        <thead>
            <tr>
                <td width="10%">Date</td>
                <td width="14%">Time</td>
                <td width="20%">Name(url)</td>
                <td width="30%">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 width="10%"><input type="text" name="date[' . $id . ']" maxlength="2" value="'.$date.'"></td>
                        <td width="14%"><input type="text" name="title[' . $id . ']" maxlength="50" value="'.$title.'"></td>
                        <td width="20%"><input type="text" name="name[' . $id . ']" maxlength="50" value="'.$name.'"></td>
                        <td width="30%"><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中,您会收到namesdatestitlesDetails的数组,每个数据都由{ID键入行的1}}所以这将改为:

<?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. 
}