表单提交后清空SQL

时间:2016-09-07 12:02:44

标签: php html sql

我搜索过并搜索过,我找不到答案 我已经制作了HERE表格,这是非常基本的。不知何故,它没有提交最终数据。代码是

<form class="form" action="submit.php" method="post">
   <table>
   <tr>             
<td>Team Name: </td>
<td><input type="text" name="team"></td>
</tr>
<tr>
<td>Captains xbox tag: </td>
<td><input type="text" name="cap"></td>
<tr>
<td>Captains E-mail:</td> 
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Team Mates: </td>
<td><TEXTAREA name="teammates" rows="6" cols="50">
Please place each team member on a new line
   </TEXTAREA><br></td>
</tr>
<tr>
<td>Sub team mates: </td>
<td><TEXTAREA name="subs" rows="2" cols="50"></TEXTAREA></td>
</tr>
</table>
<p><input type="submit"></p>
</form>

表格

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

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO tournament1 (team, teammates, cap, email, subs)
VALUES ('$_POST[team]','$_POST[cap]','$_POST[email]','$_POST[teammates]','$_POST[subs]')";

if ($conn->query($sql) === TRUE) {
    echo "Your team has been submitted Thankyou. You will be redirected back to the tournaments page Shortly";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

我做错了什么?它将连接并执行某些操作,因为ID因其自动增量而填充。其他一切都迷失了。 先谢谢你们 凯尔

1 个答案:

答案 0 :(得分:1)

所以我为你换了一点但这应该可以正常工作..

首先是表单,我已经为你添加了一个isset:

<form class="form" action="submit.php" method="post">
<table>
    <tr>
        <td>Team Name: </td>
        <td><input type="text" name="team"></td>
    </tr>
    <tr>
        <td>Captains xbox tag: </td>
        <td><input type="text" name="cap"></td>
    <tr>
        <td>Captains E-mail:</td>
        <td><input type="text" name="email"></td>
    </tr>
    <tr>
        <td>Team Mates: </td>
        <td><TEXTAREA name="teammates" rows="6" cols="50">
Please place each team member on a new line
   </TEXTAREA><br></td>
    </tr>
    <tr>
        <td>Sub team mates: </td>
        <td><TEXTAREA name="subs" rows="2" cols="50"></TEXTAREA></td>
    </tr>
</table>
<p><input type="submit" name="sendit"></p>
</form>

您会注意到我已将行动改为行动=&#34;&#34;所以它在自己的页面下运行。

接下来是PHP,我为你添加了条件。

将PHP添加到submit.php

请注意:您还可以在每个输入字段的末尾添加required

if (isset($_POST['sendit']))
{
    $team = $_POST['team'];
    $captain = $_POST['cap'];
    $email = $_POST['email'];
    $mates = $_POST['teammates'];
    $sub = $_POST['subs'];

    if (!empty($team))
    {
        if (!empty($captain))
        {
            if (!empty($email))
            {
                if (!empty($mates))
                {
                    if (!empty($sub))
                    {
                        $sql = "INSERT INTO `tournament1` (team, teammates, cap, email, subs) VALUES ('$team', '$mates', '$captain', '$email', '$sub')";

                        if ($conn->query($sql) === TRUE) {
                            echo "New record created successfully";
                        } else {
                            echo "Error: " . $sql . "<br>" . $conn->error;
                        }

                    } else {
                        echo "Please add subs..";
                    }
                } else {
                    echo "Please add mates..";
                }
            } else {
                echo "Please add captains email..";
            }
        } else {
            echo "Please add captains name..";
        }
    } else {
        echo "Please add team name..";
    }

}