2016-07-20 4 views
-1

データベースを使用してhtmlフォームを使用するときに問題が発生しました。コードを実行すると、Webページに「Completed successfully」と表示されますが、レコードは表示されません。別のレコードを入力しようとすると「このエラーが発生しました」エラー:「PRIMARY」キーの重複したエントリ '0'」これはどのように解決できますか?htmlフォームを使用してデータベースにレコードを追加する

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

は、このようなデータを収集:

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

$sql = "INSERT INTO players 
     SET 
     playerID = '".$playerID."', 
     ........"; 
+2

はそれが 'PRIMARY'キーだ設定increment'オート' id' 1人の列名を '作成します。 –

+0

あなたのフォームタグは実際には 'method =" post "'の代わりに 'method" post "'を持っていますか、それとも単に質問の誤字ですか? –

+0

フォームメソッドが定義されていない場合、[デフォルトはGET](http://stackoverflow.com/questions/2314401/what-is-the-default-form-http-method)ので、POST値はすべて空の。 –

答えて

-2

次に、if(filter_input( 'REQUEST_METHOD')=== 'POST')チェックを使用する必要があります。そのよう

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

    </form> 
     <?php 

if(filter_input(INPUT_SERVER, 'REQUEST_METHOD') === 'POST'){ 
    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()); 
    } 
} 
+1

フォームから入力からデータを渡すために 'id'属性は不要です。 'name'属性で十分です。 –

0

はフォームと同じファイル上のPHPコードはありますか?あなたが入力にIDを追加する必要が

<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()); 
} 
関連する問題