-1

Having trouble populating my database usning a html form. Once i run the code the webpage outputs "Completed successfully" but the record does not display. It seems like its entering null values for each of the fields because when i try to enter another record, i'm getting this error "connected successfully Error: Duplicate entry '0' for key 'PRIMARY'" How can i solve this?

<form action="addplayer.php"method "post"/>

     <p> id: <input type="text" name="playerid"/></p>
     <p> Name: <input type="text" name="name"/></p>
     <p> Age: <input type="text" name="age"/></p>
     <p> Position: <input type="text" name="position"/></p>
     <p> Nationality: <input type="text" name="nationality"/></p>
 <input type="submit" value="submit"/>


     ?php
require 'connection.php';


$id = filter_input(INPUT_POST, 'playerid');
$name = filter_input(INPUT_POST, 'name');
$age = filter_input(INPUT_POST, 'age');
$position = filter_input(INPUT_POST, 'position');
$nationality = filter_input(INPUT_POST, 'nationality');

$_id = mysql_real_escape_string( $id );
$_name = mysql_real_escape_string( $name );
$_age = mysql_real_escape_string( $age );
$_position = mysql_real_escape_string( $position );
$_nationality = mysql_real_escape_string( $nationality );
$sql = "INSERT INTO players ( playerid, name, age, position, nationality ) 
         VALUES ( '$_id', '$_name', '$_age', '$_position', '$_nationality' )";

if (!mysql_query($sql)){
    die('Error: ' . mysql_error());
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Kevin G
  • 119
  • 1
  • 8

1 Answers1

-2

You need to add id to the input:

<p> id: <input type="text" name="playerid" id="playerid"/></p>

collect the data like this:

$playerID = mysql_real_escape_string($_POST['playerid'])

$sql = "INSERT INTO players
        SET
        playerID = '".$playerID."',
        ........";
makie
  • 87
  • 1
  • 6
  • 1
    an `id` attribute is not required for the form to pass data from an input; the `name` attribute is sufficient. – Don't Panic Jul 20 '16 at 20:54