如何在 MySQL 数据库的多行中保存表单数据(表单内的表)

Posted

技术标签:

【中文标题】如何在 MySQL 数据库的多行中保存表单数据(表单内的表)【英文标题】:How to save form data (table inside form) in many rows in MySQL database 【发布时间】:2020-11-25 21:31:33 【问题描述】:

我在 html 表单中使用表格,当我在表单中输入详细信息时,我希望将数据保存在两行中。目前,当我输入数据时,第 1 行被第 2 行覆盖,并且仅显示在第 2 行中提交的信息。

<form name="contact-form" action="" method="post" id="contact-form">
    <table class="table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email ID</th>
                <th>Phone No</th>
                <th>Comments</th>
                
            </tr>
        </thead>
            <tr>
       
            <td> <input type="text" class="form-control" name="your_name[]" placeholder="Name" required> </td>

            <td> <input type="text" class="form-control" name="your_email[]" placeholder="Email" required></td>

          <td>  <input type="text" class="form-control" name="your_phone[]" placeholder="Phone" required></td>

            <td><input type="text" class="form-control" name="comments[]" placeholder="Comments" required></td>
    </tr>
    <tr>
       
            <td> <input type="text" class="form-control" name="your_name[]" placeholder="Name" required> </td>

            <td> <input type="text" class="form-control" name="your_email[]" placeholder="Email" required></td>

          <td>  <input type="text" class="form-control" name="your_phone[]" placeholder="Phone" required></td>

            <td><input type="text" class="form-control" name="comments[]" placeholder="Comments" required></td>
    </tr>
    </table>

        <button type="submit" class="btn btn-primary" name="submit" value="Submit" id="submit_form">Submit</button>
 <img src="img/loading.gif" id="loading-img">
</form>

在响应端,我的代码如下所示:

<?php 
require_once("database_connection.php");
if((isset($_POST['your_name'])&& $_POST['your_name'] !='') && (isset($_POST['your_email'])&& $_POST['your_email'] !=''))

 require_once("contact_mail.php");


for ($i = 0; $i<count($_POST['your_name']); $i++)
$yourName = $conn->real_escape_string($_POST['your_name'][$i]);
$yourEmail = $conn->real_escape_string($_POST['your_email'][$i]);
$yourPhone = $conn->real_escape_string($_POST['your_phone'][$i]);
$comments = $conn->real_escape_string($_POST['comments'][$i]);




$sql="INSERT INTO contact_form_info (name, email, phone, comments) VALUES ('".$yourName."','".$yourEmail."', '".$yourPhone."', '".$comments."')";




if(!$result = $conn->query($sql))
die('There was an error running the query [' . $conn->error . ']');

else

echo "Thank you! We will contact you soon";


else

echo "Please fill Name and Email";

?>

【问题讨论】:

【参考方案1】:

你的括号放错了,检查我的代码。免责声明 - 没有尝试执行它。

<?php 
require_once("database_connection.php");
require_once("contact_mail.php");
if((isset($_POST['your_name'])&& $_POST['your_name'] !='') && (isset($_POST['your_email'])&& $_POST['your_email'] !=''))

    for ($i = 0; $i<count($_POST['your_name']); $i++) 
        $yourName = $conn->real_escape_string($_POST['your_name'][$i]);
        $yourEmail = $conn->real_escape_string($_POST['your_email'][$i]);
        $yourPhone = $conn->real_escape_string($_POST['your_phone'][$i]);
        $comments = $conn->real_escape_string($_POST['comments'][$i]);
        $sql="INSERT INTO contact_form_info (name, email, phone, comments) VALUES ('".$yourName."','".$yourEmail."', '".$yourPhone."', '".$comments."')";
        if(!$result = $conn->query($sql))
            die('There was an error running the query [' . $conn->error . ']');
        
    
    echo "Thank you! We will contact you soon";

else

    echo "Please fill Name and Email";


?>

只是一个建议 - try using mysqli with prepared statements

【讨论】:

谢谢!完美运行。只是放错了括号。我能问一下你为什么建议使用 MySQLi 和准备好的语句吗?

以上是关于如何在 MySQL 数据库的多行中保存表单数据(表单内的表)的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 php 从多行表单更新 mysql 数据库 [关闭]

如何一次将多行保存到多个数据库表

在 Laravel 5 中将多行从动态表单保存到数据库

php表单mysql更新保存数据

使用 Laravel 数据透视表的多行和多列输入表单

Canonical:如何将 HTML 表单数据保存到 MySQL 数据库中