【问题标题】:How to update data from dynamically created table to database in php如何在php中将数据从动态创建的表更新到数据库
【发布时间】:2020-01-22 20:34:05
【问题描述】:

我已经使用 php 动态创建了一个表。但我需要根据用户输入更新一些单元格值。但我不知道该怎么做。我的代码如下。 我找到了一种使名称成为数组的方法是在stackoverflow的名称属性中使用[]。但这对我不起作用。如果我在 while 循环或循环外回显 $_POST['std_name'] ,则只计算学生姓名的最后一行。请有人帮忙。我被困在这里3天了。

<form action="" method="post">
        <table>
            <tr>
                <th>Student ID</th>
                <th>Student Name</th>
                <th>Student Father Name</th>
                <th>Student Mother Name</th>
                <th>Student Parents Mobile Number</th>
                <th>Student Mobile Number</th>
                <th>Student Batch Name</th>
                <th>Student College Name</th>
                <th>Student Gender</th>
                <th>Student Picture</th>
                <th>Student Admit Date</th>
                <th>January</th>
                <th>February</th>
                <th>March</th>
                <th>April</th>
                <th>May</th>
                <th>June</th>
                <th>July</th>
                <th>August</th>
                <th>September</th>
                <th>October</th>
                <th>November</th>
                <th>December</th>
            </tr>
            <tr>
                <?php
            $host="localhost";
            $username="root";
            $password="";
            $db="paragon_first_year";
            $qry="select * from `student_info`";
            $con=mysqli_connect($host,$username,$password);
            mysqli_select_db($con,$db);
            $res=mysqli_query($con,$qry);
            if(mysqli_num_rows($res)>0){
                while($dt=mysqli_fetch_array($res)){?>
                <td><input type="text" name="std_id[]" value="<?php echo $dt['std_id']?>"></td>
                <td><input type="text" name="std_name[]" value="<?php echo $dt['std_name']?>"></td>
                <td><input type="text" name="std_father_name[]" value="<?php echo $dt['std_father_name']?>"></td>
                <td><input type="text" name="std_mother_name[]" value="<?php echo $dt['std_mother_name']?>"></td>
                <td><input type="text" name="std_parent_mob[]" value="<?php echo $dt['std_parents_mob_no']?>"></td>
                <td><input type="text" name="std_mob[]" value="<?php echo $dt['std_mob_no']?>"></td>
                <td><?php echo $dt['std_batch_name'] ?></td>
                <td><?php echo $dt['std_college_name'] ?></td>
                <td><?php echo $dt['std_gender'] ?></td>
                <td><img style="height:100px;width:100px;" src="uploadImage/<?php echo $dt['std_name']."_".$dt['std_pic'] ?>" alt=""></td>
                <td><?php echo $dt['std_admit_date'] ?></td>
                <td><input type="text" name="jan[]" value="<?php echo $dt['january']?>"></td>
                <td><input type="text" name="feb[]" value="<?php echo $dt['february']?>"></td>
                <td><input type="text" name="mar[]" value="<?php echo $dt['march']?>"></td>
                <td><input type="text" name="apr[]" value="<?php echo $dt['april']?>"></td>
                <td><input type="text" name="may[]" value="<?php echo $dt['may']?>"></td>
                <td><input type="text" name="jun[]" value="<?php echo $dt['june']?>"></td>
                <td><input type="text" name="jul[]" value="<?php echo $dt['july']?>"></td>
                <td><input type="text" name="aug[]" value="<?php echo $dt['august']?>"></td>
                <td><input type="text" name="sep[]" value="<?php echo $dt['september']?>"></td>
                <td><input type="text" name="oct[]" value="<?php echo $dt['october']?>"></td>
                <td><input type="text" name="nov[]" value="<?php echo $dt['november']?>"></td>
                <td><input type="text" name="dec[]" value="<?php echo $dt['december']?>"></td>
            </tr><?php 

                }
            }
        ?>

        </table>
        <center>
            <input type="submit" name="submit" value="Update Data">
        </center>
</form>
</body>

my table is showing nicely

希望有人帮忙。提前致谢。

【问题讨论】:

  • 为索引命名,使用主ID:&lt;input type="text" name="std_name[&lt;?=$dt['std_id']?&gt;]" value="&lt;?=$dt['std_name']?&gt;"&gt; ...
  • 请显示(通过将其包含在您的问题正文中)对应于“。如果我在 while 循环或循环外回显 $_POST['std_name'] 仅最后一行学生姓名被计算在内”

标签: javascript php jquery


【解决方案1】:

您可以使用数组键来传递信息。在这个例子中,第一个键是数据库主键,第二个键是变量名。

生成的数组如下所示:

$row[123][std_name] = 'joe';
$row[123][std_father_name] = 'bob';
... etc
$row[124][std_name] = 'sally';
$row[124][std_father_name] = 'john';
...etc

然后,当您遍历行时,您将获得用于更新数据库的密钥 (123),如 where std_id=123

注意:我使用准备好的语句来显示更新数据库。这是必不可少的。永远不要使用连接将变量放入查询中(即"select * from table where id='$id'"

最后,注意结构。从 PHP 开始,没有 print 或 echo 语句。这允许您使用用户输入然后重定向。首先初始化,然后处理用户输入,然后做页面逻辑,最后退出php并输出视图(html)

这个例子不是复制粘贴;它有一些需要填写的东西,并且可能需要调试。它只是为了展示大局。

<?php

// initialize
$host="localhost";
$username="root";
$password="";
$db="paragon_first_year";
$con=mysqli_connect($host,$username,$password);

// a simple wrapper to make the prepared query more manageable
// see https://phpdelusions.net/mysqli/mysqli_connect#helper
function prepared_query($mysqli, $sql, $params, $types = "")
{
    $types = $types ?: str_repeat("s", count($params));
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    return $stmt;
}

// work with user input
if($_SERVER['REQUEST_METHOD'] === 'POST') {

    $dataRows = (array)$_POST['data'];
    foreach($dataRows as $id=>$data) {

        $sql = "INSERT INTO student_info SET std_name=?, std_father_name=?, " .
               // etc  . 
               "WHERE std_id=?";
        $params = array(
                    $data['std_name'], 
                    $data['std_father_name'], 
                    // etc... 

                    $id
                  );
        // this does the prepare, bind, and execute... Yes, I could just do the 
        // bind and execute here, but the time savings isn't worth the trouble.
        $stmt = prepared_query($con, $sql, $params); 

    }

    // always redirect after a POST
    header('Location: /path/to/next/page/or/this/page');
    die;
}

// no user input; ok to use plain old query.
$qry="select * from `student_info`";
mysqli_select_db($con,$db);
$res=mysqli_query($con,$qry);

?>
<html>

...

<form action="" method="post">
    <table>
        <tr>
            <th>Student ID</th>
            <th>Student Name</th>
            <th>Student Father Name</th>
            <th>Student Mother Name</th>
            <th>Student Parents Mobile Number</th>
            <th>Student Mobile Number</th>
            <th>Student Batch Name</th>
            <th>Student College Name</th>
            <th>Student Gender</th>
            <th>Student Picture</th>
            <th>Student Admit Date</th>
            <th>January</th>
            <th>February</th>
            <th>March</th>
            <th>April</th>
            <th>May</th>
            <th>June</th>
            <th>July</th>
            <th>August</th>
            <th>September</th>
            <th>October</th>
            <th>November</th>
            <th>December</th>
        </tr>
        <!-- don't need mysqli_num_rows; the while won't loop if no results -->
        <?php while($dt=mysqli_fetch_array($res)): ?>
        <tr>
            <td><?= $dt['std_id'] ?></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_name']" value="<?= $dt['std_name'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_father_name']" value="<?= $dt['std_father_name'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_mother_name']" value="<?= $dt['std_mother_name'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_parent_mob']" value="<?= $dt['std_parents_mob_no'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['std_mob']" value="<?= $dt['std_mob_no'] ?>"></td>
            <td><?= $dt['std_batch_name']  ?></td>
            <td><?= $dt['std_college_name']  ?></td>
            <td><?= $dt['std_gender']  ?></td>
            <td><img style="height:100px;width:100px;" src="uploadImage/<?= $dt['std_name']."_".$dt['std_pic']  ?>" alt=""></td>
            <td><?= $dt['std_admit_date']  ?></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['jan']" value="<?= $dt['january'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['feb']" value="<?= $dt['february'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['mar']" value="<?= $dt['march'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['apr']" value="<?= $dt['april'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['may']" value="<?= $dt['may'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['jun']" value="<?= $dt['june'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['jul']" value="<?= $dt['july'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['aug']" value="<?= $dt['august'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['sep']" value="<?= $dt['september'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['oct']" value="<?= $dt['october'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['nov']" value="<?= $dt['november'] ?>"></td>
            <td><input type="text" name="data[<?= $dt['std_id'] ?>]['dec']" value="<?= $dt['december'] ?>"></td>
        <?php endwhile; ?>
        </tr>
    </table>
    <center>
        <input type="submit" name="submit" value="Update Data">
    </center>
</form>
</body>

【讨论】:

    猜你喜欢
    • 2017-11-16
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    相关资源
    最近更新 更多