1

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

1 回答 1

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

?>

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

于 2020-08-05T17:07:12.667 回答