提交表单是创建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

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.

You could try adding up proper quotes and concatenation on your mysql statement in the insert.php file like below.

$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']."')";