我有一个用多个文本输入创建的表单。提交时,它会在我的数据库中创建一条记录。一切正常。我能够成功提交并查看phpMyAdmin中的条目。
最近,我为一些字段添加了一个下拉菜单。这些下拉列表是从我的数据库中的表中填充的。突然间,我似乎遇到了问题。
现在,当我填写表单并提交表单时,会在数据库中创建包含该信息的记录。完美。
问题是我的数据库中创建了一个完全空白的记录以及表单数据中的实际记录。
这是我的代码,格式为:
<?php
include('_header.php');
define('DB_NAME', '????');
define('DB_USER', '????');
define('DB_PASSWORD', '????');
define('DB_HOST', '????');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$races = "SELECT * FROM `wyver2_charactercards`.`races` ORDER BY race_name";
$result_races = mysqli_query($con, $races);
$psps = "SELECT * FROM `wyver2_charactercards`.`classes` ORDER BY class_name";
$result_psps = mysqli_query($con, $psps);
echo "<div class='viewcard'>";
echo "<form action='insert.php' method='post'>";
echo "<table>";
echo "<tr>";
echo "<td colspan='6'><h1>Wyvern Rising - New Character Form</h1></td>";
echo "</tr>";
echo "<tr class='heads'>";
echo "<td><h3>Last Name : </h3></td>";
echo "<td><h3>First Name : </h3></td>";
echo "<td></td>";
echo "<td><h3>Character Name : </h3></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr class='spaced'>";
echo "<td><input type='text' name='lname'></td>";
echo "<td><input type='text' name='fname'></td>";
echo "<td></td>";
echo "<td><input type='text' name='cname'></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr class='heads'>";
echo "<td><h3>Race : </h3></td>";
echo "<td><h3>PSP : </h3></td>";
echo "<td><h3>Deaths : </h3></td>";
echo "<td></td>";
echo "<td><h3>Unspent Build : </h3></td>";
echo "<td><h3>Build Total : </h3></td>";
echo "</tr>";
echo "<tr class='spaced'>";
echo "<td><select name='dd_race'>";
while($row = mysqli_fetch_array($result_races)) {
echo "<option value='" . $row[race_name] . "'>" . $row[race_name] . "</option>";
}
echo "</td>";
echo "<td><select name='dd_class'>";
while($row = mysqli_fetch_array($result_psps)) {
echo "<option value='" . $row[class_name] . "'>" . $row[class_name] . "</option>";
}
echo "</select>";
echo "</td>";
echo "<td><input type='text' name='deaths'></td>";
echo "<td></td>";
echo "<td><input type='text' name='unspentbuild'></td>";
echo "<td><input type='text' name='btotal'></td>";
echo "</tr>";
echo "<tr class='heads'>";
echo "<td><h3>Body : </h3></td>";
echo "<td><h3>DEX : </h3></td>";
echo "<td><h3>END : </h3></td>";
echo "<td><h3>PRE : </h3></td>";
echo "<td><h3>RP : </h3></td>";
echo "<td><h3>SP : </h3></td>";
echo "</tr>";
echo "<tr class='spaced'>";
echo "<td><input type='text' name='body'></td>";
echo "<td><input type='text' name='dex'></td>";
echo "<td><input type='text' name='end'></td>";
echo "<td><input type='text' name='pre'></td>";
echo "<td><input type='text' name='rp'></td>";
echo "<td><input type='text' name='sp'></td>";
echo "</tr>";
echo "</table>";
echo "<br />";
echo "<input type='submit'>";
echo "</form>";
echo "</div>";
echo "<br />";
echo "<a href='index.php'>Back</a>";
echo "<div align='center'>";
echo WORDING_YOU_ARE_LOGGED_IN_AS . $_SESSION['user_name'] . "<br />";
echo "<a href='index.php?logout'>" . WORDING_LOGOUT . "</a>";
echo " ";
echo "<a href='edit.php'>" . WORDING_EDIT_USER_DATA . "</a>";
echo "</div";
mysqli_close($con);
?>
<?php include('_footer.php'); ?>
这是insert.php代码:
<?php
define('DB_NAME', '????');
define('DB_USER', '????');
define('DB_PASSWORD', '????');
define('DB_HOST', '????');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO `wyver2_charactercards`.`character`
(`character_id`,
`last_name`,
`first_name`,
`character_name`,
`race`,
`psp`,
`deaths`,
`build_total`,
`build_unspent`,
`body`,
`dex`,
`end`,
`pre`,
`rp_points`,
`service_points`)
VALUES
(NULL,
'$_POST[lname]',
'$_POST[fname]',
'$_POST[cname]',
'$_POST[dd_race]',
'$_POST[dd_class]',
'$_POST[deaths]',
'$_POST[btotal]',
'$_POST[unspentbuild]',
'$_POST[body]',
'$_POST[dex]',
'$_POST[end]',
'$_POST[pre]',
'$_POST[rp]',
'$_POST[sp]')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 character added";
echo "<br /><br /><br />";
echo '<a href="index.php">Back</a>';
mysqli_close($con);
?>
<?php include('_footer.php'); ?>
为什么会提交条目,还提交第二个完全空白的条目?
编辑:这是我提交表单时发生的事情。我得到一个很好的条目和一个空白条目:
答案 0 :(得分:0)
您可以尝试在insert.php文件中的mysql语句中添加适当的引号和连接,如下所示。
$sql="INSERT INTO `wyver2_charactercards`.`character`
(`character_id`,
`last_name`,
`first_name`,
`character_name`,
`race`,
`psp`,
`deaths`,
`build_total`,
`build_unspent`,
`body`,
`dex`,
`end`,
`pre`,
`rp_points`,
`service_points`)
VALUES
(NULL,
'".$_POST['lname']."',
'".$_POST['fname']."',
'".$_POST['cname']."',
'".$_POST['dd_race']."',
'".$_POST['dd_class']."',
'".$_POST['deaths']."',
'".$_POST['btotal']."',
'".$_POST['unspentbuild']."',
'".$_POST['body']."',
'".$_POST['dex']."',
'".$_POST['end']."',
'".$_POST['pre']."',
'".$_POST['rp']."',
'".$_POST['sp']."')";
答案 1 :(得分:0)
从Chrome浏览器切换到Firefox。创建条目非常有效。只有一个条目,没有空白。
似乎是一个浏览器问题。
编辑:再次在Chrome中工作。我认为它是一个缓存问题或其他什么。