duanpin9531 2017-05-01 10:26
浏览 41

PHP表单提交多个条目

How can I keep the current form with clear fields after successful insertion. I have used to php files, one for connecting the database and another for collecting data from user. Both of the files code are given below: For connecting database:

<?php
$servername = "localhost";
$username = "local";
$password = "host";
$dbname = "form";

$conn = mysqli_connect($servername,$username,$password,$dbname);

if(!$conn){
    die("Connection failed: ".mysqli_connect_error());
}

$roll=$_POST['roll'];
$name=$_POST['name'];
$city=$_POST['city'];

$sql = "insert into people_info (roll,name,city) values ('$roll','$name','$city')";


if(mysqli_query($conn,$sql)){
    echo "New record created successflly";
}
else{
    echo "Error: ".$sql."<br>".mysqli_error($conn);
}

mysqli_close($conn);
?>

And the form page is:

<html>
<body>

<form action="connection.php" method="post">
    <table border="1">
        <tr>
            <th>
                Field
            </th>
            <th>
                Value
            </th>
        </tr>
        <tr>
            <td>
                Roll:
            </td>
            <td>
                <input type="text" name="roll"/><br>
            </td>
        </tr>
        <tr>
            <td>
                Name:
            </td>
            <td>
                <input type="text" name="name"/><br>
            </td>
        </tr>
        <tr>
            <td>
                City:
            </td>
            <td>
                <input type="text" name="city"/><br>
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit"/>
            </td>
        </tr>
    </table>
</form>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongtu0088 2017-05-01 10:31
    关注

    Instead of this:

    if(mysqli_query($conn,$sql)){
        echo "New record created successfully";
    }
    else{
        echo "Error: ".$sql."<br>".mysqli_error($conn);
    }
    

    try

    if(mysqli_query($conn,$sql)){
        header('location: page1.php?message=New record created successfully');
    }
    else{
        header('location: page1.php?message=' . mysqli_error($conn));
    }
    

    here header() function will redirect you to form page with $message variable having response message. Use it to show the response on page.

    评论

报告相同问题?