【问题标题】:Information from form will not submit to mysql database表单中的信息不会提交到 mysql 数据库
【发布时间】:2014-08-12 14:56:55
【问题描述】:

感谢您抽出宝贵时间阅读本文。我正在使用 HTML 表单并使用 PHP 通过它向 MySQL 数据库提交信息。我无法弄清楚为什么这些信息没有进入数据库。任何帮助将不胜感激。

这是数据库的样子:

这是 HTML 表单:

<?php ?>
<html>
    <head>
        <title>Raid Boss Strategy Editor</title>
    </head>

    <body>

        <h3>Raid Strats</h3>

        <p>
            <form action="stratadd2.php" method="POST">
            <table cellpadding="3" cellspacing="4" border="0">
                <tr>
                    <td>Boss Name</td>
                    <td><input type="text" name="bossName" value="<?php echo "$bossName"; ?>"></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Raid Zone</td>
                    <td><input type="text" name="raidZone" value="<?php echo "$raidZone"; ?>"></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Boss Health</td>
                    <td><input type="text" name="bossHealth" value="<?php echo "$bossHealth"; ?>"></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Boss Enrage</td>
                    <td><input type="text" name="bossEnrage" value="<?php echo "$bossEnrage"; ?>"></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Boss Abilities</td>
                    <td><input type="text" name="bossAbilities" value="<?php echo "$bossAbilities"; ?>"></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Phase One</td>
                    <td><input type="text" name="bossPhaseone" value="<?php echo "$bossPhaseone"; ?>"></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Phase Two</td>
                    <td><input type="text" name="bossPhasetwo" value="<?php echo "$bossPhasetwo"; ?>"></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Phase Three</td>
                    <td><input type="text" name="bossPhasethree" value="<?php echo "$bossPhasethree"; ?>"></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Final Notes</td>
                    <td><input type="text" name="finalNotes" value="<?php echo "$finalNotes"; ?>"></td>
                    <td></td>
                </tr>
                <tr>
                    <td><input type="submit" name="submit" value="Submit"> <input type="reset" name="reset" value="reset"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

这是 PHP 代码:

<?php
require_once 'dbc.php';
error_reporting(0);

if($_POST['submit'])
{
    $bossName = $_POST['bossName'];
    $raidZone = $_POST['raidZone'];
    $bossHealth = $_POST['bossHealth'];
    $bossEnrage = $_POST['bossEnrage'];
    $bossAbilities = $_POST['bossAbilities'];
    $bossPhaseone = $_POST['bossPhaseone'];
    $bossPhasetwo = $_POST['bossPhasetwo'];
    $bossPhasethree = $_POST['bossPhasethree'];
    $finalNotes = $_POST['finalNotes'];

    require "dbc.php";
    $query = mysql_query("INSERT INTO raidstrats VALUES ('','$bossName','$raidZone','$bossHealth','$bossEnrage','$bossAbilities','$bossPhaseone','$bossPhasetwo','$bossPhasethree','$finalNotes')");
    die("Information Submitted!");
}
?>

【问题讨论】:

  • 您的代码中有很多安全漏洞。我建议您阅读有关 mysql 注入和 PDO 的内容。
  • 为什么是require_once 'dbc.php'; 和require "dbc.php";?
  • 嗨 Fred,删除它们中的任何一个似乎都不会改变任何东西,但你是对的,这是不必要的。感谢您的建议 Luc Prevoo。
  • 打开error_reporting():测试$query:我们无法在dbc.php 中看到您的连接。

标签: php html mysql database forms


【解决方案1】:

可能与将空字符串 ('') 插入到自动增量索引列 (Id) 中有关。尝试指定列名

$query = mysql_query("INSERT INTO raidstrats (bossName,raidZone,bossHealth,bossEnrage,bossAbilities,bossPhaseone,bossPhasetwo,bossPhasethree,finalNotes) VALUES ('$bossName','$raidZone','$bossHealth','$bossEnrage','$bossAbilities','$bossPhaseone','$bossPhasetwo','$bossPhasethree','$finalNotes')");

【讨论】:

  • 刚刚尝试过,但似乎没有任何改变。不过非常感谢!
【解决方案2】:
<?php
require_once 'dbc.php';
error_reporting(0);

if($_POST['submit'])
{
    $bossName = $_POST['bossName'];
    $raidZone = $_POST['raidZone'];
    $bossHealth = $_POST['bossHealth'];
    $bossEnrage = $_POST['bossEnrage'];
    $bossAbilities = $_POST['bossAbilities'];
    $bossPhaseone = $_POST['bossPhaseone'];
    $bossPhasetwo = $_POST['bossPhasetwo'];
    $bossPhasethree = $_POST['bossPhasethree'];
    $finalNotes = $_POST['finalNotes'];

    require "dbc.php";
    $query = mysql_query("INSERT INTO raidstrats VALUES (NULL,$bossName, $raidZone, $bossHealth, $bossEnrage, $bossAbilities, $bossPhaseone, $bossPhasetwo', $bossPhasethree,  $finalNotes)");
    die("Information Submitted!");
}
?>

【讨论】:

  • 刚刚尝试过,但似乎没有任何改变。不过非常感谢!
猜你喜欢
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 2021-08-15
相关资源
最近更新 更多