【问题标题】:How to save form data (table inside form) in many rows in MySQL database如何在 MySQL 数据库的多行中保存表单数据(表单内的表)
【发布时间】: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";
}
?>

【问题讨论】:

    标签: javascript html mysql database forms


    【解决方案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 和准备好的语句吗?
    猜你喜欢
    • 1970-01-01
    • 2010-09-25
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 2016-08-09
    相关资源
    最近更新 更多