0

ここでは、いくつかのテストを行っただけでは十分な検証がないことを知っています。$result は常に空を返しますか? 私のクエリは悪いですか?私はPHPが初めてで、変数を文字列に連結することは、私が完全に把握したものではありません。私はそれと概念にかなり精通しているので、OOP フォームを使用します。

また、私はこのコードがひどくずさんであることを知っています... =) `に飛び込もうとしているだけです

$page = new Page();

$page->title = "Add a New Item";
$page->DisplayHeader();
$page->DisplaySidebar();

if (isset($_POST['submit']))
{
    // make short variable names
    $name = trim($_POST['name']);
    $level = intval($_POST['level']);
    $slot = strtolower($_POST['slot']);
    $hp = intval($_POST['hp']);
    $mana = intval($_POST['mana']);
    $mvs = intval($_POST['mvs']);
    $int = intval($_POST['int']);
    $wis = intval($_POST['wis']);
    $str = intval($_POST['str']);
    $dex = intval($_POST['dex']);
    $con = intval($_POST['con']);
    $p_ac = intval($_POST['p_ac']);
    $m_ac = intval($_POST['m_ac']);
    $saves = intval($_POST['saves']);
    $hit = intval($_POST['hit']);
    $dam = intval($_POST['dam']);


    $queryOk = 1;

    if (empty($name) || empty($level) || empty($slot))
    {
        echo '<h3>Please enter all the required fields</h3>';
        $queryOk = 0;
    }

    // Instantiate database object and connect
    @ $db = new mysqli('*host*', '*user*', '*pass*', '*database*');

    // Check connection to 
    if (mysqli_connect_errno()) {
        echo 'Error:  Could not connect to database, try again later';
    }

    $query = "INSERT INTO items (name, level, slot, hp, mana, mvs, int, wis, str, dex, con, p_ac, m_ac, saves, hit, dam)".
    "V ALUES ('$name', $level, '$slot', $hp, $mana, $mvs, $int, $wis, $str, $dex, $con, $p_ac, $m_ac, $saves, $hit, $dam)";

    $result = $db->query($query);

    if (!$result)
    {
        echo '<h3>Error:  Item was not entered.  (Your webmaster sucks)</h3>';
    }
    else    {
        echo "<p>The items \"$name\" was successfully entered into the database.  <a href=\"equipment.php\>Back to Equipment or add another item.</a></p>";
    }

    $db->close();
}`
4

2 に答える 2

2

のスペースV ALUESが実際にコード内にあり、クエリが失敗する原因となる場合

アップデート

それがエラーの原因でない場合は、発生したエラー$mysqli->errorを確認してください。

if (!$result)
{
    echo '<h3>'$mysqli->error'  (Your webmaster sucks)</h3>';
}
于 2012-06-15T03:03:26.340 に答える
0

intは mysql の予約語で、フィールド名として使用しています。バックティックでエスケープする必要があります。

INSERT INTO ... (..., `int`, ...)
                      ^---^-- escapes

あなたのクエリ:

INSERT INTO items (name, level, slot, hp, mana, mvs, int, wis, str, dex, con, p_ac, m_ac, saves, hit, dam)
                                                     ^^^^--- problem here
VALUES ('$name', $level, '$slot', $hp, $mana, $mvs, $int, $wis, $str, $dex, $con, $p_ac, $m_ac, $saves, $hit, $dam)";
                                                   ^^^^^---NOT here
于 2012-06-15T03:27:34.587 に答える