使用html表单填充数据库时遇到问题。一旦我运行代码,网页输出"成功完成"但记录没有显示。看起来它为每个字段输入空值,因为当我尝试输入另一条记录时,我收到此错误"连接成功错误:重复输入' 0' 0关键' PRIMARY'"我该如何解决这个问题?
<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());
}
答案 0 :(得分:-2)
您需要在输入中添加id:
<p> id: <input type="text" name="playerid" id="playerid"/></p>
收集如下数据:
$playerID = mysql_real_escape_string($_POST['playerid'])
$sql = "INSERT INTO players
SET
playerID = '".$playerID."',
........";