gpt4 book ai didi

php - 提交表单正在创建 MySQL 记录和空白记录

转载 作者:行者123 更新时间:2023-11-30 00:14:46 25 4
gpt4 key购买 nike

我有一个使用多个文本输入创建的表单。提交后,它会在我的数据库中创建一条记录。一切正常。我能够成功提交并在 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'); ?>

知道为什么要提交参赛作品,但还提交第二个完全空白的参赛作品吗?

编辑:这就是我提交表单时发生的情况。我得到一个好的条目和一个空白条目: phpMyAdmin entries

最佳答案

您可以尝试在 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']."')";

关于php - 提交表单正在创建 MySQL 记录和空白记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23749646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com