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

KTE:

我在HTML表单中使用表格,当我在表单中输入详细信息时,我希望数据保存在两行中。目前,当我输入数据时,第1行被第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";
}
?>
真实性:

您的括号放错了位置,请检查我的代码。免责声明-尚未尝试执行。

<?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";
}

?>

只是一个建议- 尝试将MySQLi与准备好的语句一起使用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章