Vixen Gaming Vixen Gaming - 3 months ago 8
PHP Question

Empty SQL after form submission

I've searched and searched I cannot find an answer to this
I've made a form HERE It's very basic. Somehow it's not submitting the final data. the code is

<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>


That's the 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();
?>


What am I doing wrong? It will connect and do something because the ID fills because of its autoincremented. everything else gets lost.
Thanks in advance guys
Kyle

Answer

So I switched it around a little for you but this should work fine..

firstly the form, I have added an isset in for you:

<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>

You'll notice I have changed action to action="" so it runs under its own page.

Followed next by the PHP of which I have added conditions into for you.

Add the PHP to submit.php

Please note: you can also add required at the end of each input field.

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

}