-3

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

Noman
  • 1,459
  • 2
  • 18
  • 38
  • 1
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Sep 07 '16 at 12:05
  • Which of the 2 echo's do you see – RiggsFolly Sep 07 '16 at 12:07
  • add error_reporting() in your code, – devpro Sep 07 '16 at 12:08
  • 1
    DEBUGGING: if you are not getting this `Connection failed` it means, your connection is fine, now check the post values `print_r($_POST)` than echo your query and run manualy in php my admin. – devpro Sep 07 '16 at 12:10
  • your column and values also not matching. – devpro Sep 07 '16 at 12:16

1 Answers1

1

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

}
Option
  • 2,605
  • 2
  • 19
  • 29