dongyou8087 2014-05-20 01:25
浏览 78
已采纳

提交表单是创建MySQL记录和空白记录

I have a form created with multiple text inputs. On submission, it creates a record in my database. All works fine. I am able to submit successfully and review the entry in phpMyAdmin.

Recently, I have added a drop down menu for some of the fields. These dropdowns are populated from a table in my database. Suddenly, I seem to be having a problem.

Now, when I fill out the form and submit it, a record is created in the database with the information. Perfect.

The problem is somehow a completely blank record is being created in my database along with the actual record from the form data.

Here is my code with the form:

<?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'); ?>

And here is the insert.php code:

<?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'); ?>

Any ideas why it would be submitting the entry, but ALSO submitting a second, completely blank entry?

EDIT: This is what happens when I submit my form. I get a good entry, and a blank entry: phpMyAdmin entries

  • 写回答

2条回答 默认 最新

  • doutongwei4380 2014-05-20 02:24
    关注

    Switched from Chrome browser to Firefox. Creating an entry works perfectly. Only one entry, no blank.

    Seems to be a browser issue.

    EDIT: Working in Chrome again. I'm assuming it's a cache issue or something.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大