-1

尝试使用 PDO 将数据插入 mysql 表时,出现以下错误。

致命错误:未捕获异常 'PDOException' 并带有消息 'SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 /home/gaz492/public_html/dayz/user/pages/admin/vip.php:26 中的 '' at line 1' 附近使用正确的语法:26 堆栈跟踪:#0 / home/gaz492/public_html/dayz/user/pages/admin/vip.php(26): PDO->query('INSERT INTO `cu...') #1 /home/gaz492/public_html/dayz/user/index .php(89): include_once('/home/gaz492/pu...') #2 {main} 在第 26 行的 /home/gaz492/public_html/dayz/user/pages/admin/vip.php 中抛出

这是该页面上的代码。

如果有人能够提供帮助,那将意义重大,因为我一直在到处寻找解决方案,但找不到任何可行的方法。询问您是否需要更多信息,我很乐意提供您需要的信息。

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Gareth
 * Date: 14/07/13
 * Time: 19:01
 * To change this template use File | Settings | File Templates.
 */
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$news = "";

if ($loggedin === false) { echo "Please log in to access the admin panel."; include_once("login.php"); }
else {

    $uid = $_SESSION['adminid'];

    if(isset($_REQUEST['addPackageBtn'])) {
        $packageName = $_POST['inputName'];
        $packageInventory = $_POST['inputInventory'];
        $packageBackpack = $_POST['inputBackpack'];
        $packageSkin = $_POST['inputSkin'];

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        //$dbh->query("INSERT INTO `cust_loadout`(`inventory`, `backpack`, `model`, `description`) VALUES ('". $packageInventory ."','". $packageBackpack ."','". $packageSkin ."','". $packageName . "'");
        $dbh->query("INSERT INTO `cust_loadout` (`inventory`, `backpack`, `model`, `description`) VALUES ([],[],[],[]");
    }

    ?>
    <h1>Manage VIP's</h1>

    <table class="table table-striped table-bordered span8">
        <thead>
        <tr>
            <th>Delete</th>
            <th>Unique ID</th>
            <th>Package</th>
        </tr>
        </thead>
        <tbody>
        <?php
        foreach ($dbh->query("SELECT * FROM cust_loadout_profile") AS $vip) {

        echo "<tr>";
        echo "<td>Delete</td>";
        echo "<td>".$vip["unique_id"]."</td>";
        echo "<td>".$vip["cust_loadout_id"]."</td>";
        echo "</tr>";


        }
    ?>
        </tbody>
    </table>

    <table class="table table-striped table-bordered span3">
        <thead>
        <tr>
            <th>Manage VIP's</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>
                <a href="#addVIP" role="button" data-toggle="modal" class="btn btn-primary"><i class="icon-white icon-plus-sign"></i> Add VIP</a>
            </td>
        </tr>
        <tr>
            <td>
                <a href="#addPackage" role="button" data-toggle="modal" class="btn btn-primary"><i class="icon-white icon-plus-sign"></i> Add VIP Package</a>
            </td>
        </tr>
        </tbody>
    </table>

    <table class="table table-striped table-bordered span8">
        <thead>
        <tr>
            <th>Delete</th>
            <th>Name</th>
            <th>Inventory</th>
            <th>Backpack</th>
            <th>Skin</th>
        </tr>
        </thead>
        <tbody>
        <?php
        foreach ($dbh->query("SELECT * FROM cust_loadout") AS $vip) {

            echo "<tr>";
            echo "<td>Delete</td>";
            echo "<td>".$vip["description"]."</td>";
            echo "<td>".$vip["inventory"]."</td>";
            echo "<td>".$vip["backpack"]."</td>";
            echo "<td>".$vip["model"]."</td>";
            echo "</tr>";


        }
        ?>
        </tbody>
    </table>

    <!-- Add VIP Modal -->
    <div id="addVIP" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="addVIPLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="addVIPLabel">Add VIP</h3>
        </div>
        <div class="modal-body">
            <form class="form-horizontal">
                <div class="control-group">
                    <label class="control-label" for="inputUniqueID">Unique ID</label>
                    <div class="controls">
                        <input type="text" id="inputUniqueID" placeholder="Unique ID">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="inputPackage">Package</label>
                    <div class="controls">
                        <input type="text" id="inputPackage" placeholder="Package">
                    </div>
                </div>
                <div class="control-group">
                    <div class="controls">
                    </div>
                </div>

        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
            <button type="submit" class="btn btn-primary">Save changes</button>
            </form>
        </div>
    </div>

    <!-- Add VIP Package Modal -->
    <div id="addPackage" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="addPackagePLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="addPackageLabel">Add VIP Package</h3>
        </div>
        <div class="modal-body">
            <form class="form-horizontal" acion="./?p=admin/vip" method="post">
                <div class="control-group">
                    <label class="control-label" for="inputName">Name</label>
                    <div class="controls">
                        <input type="text" id="inputName" name="inputName" placeholder="Name">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="inputInventory">Inventory</label>
                    <div class="controls">
                        <input type="text" id="inputInventory" name="inputInventory"  placeholder="Inventory">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="inputBackpack">Backpack</label>
                    <div class="controls">
                        <input type="text" id="inputBackpack" name="inputBackpack" placeholder="Backpack">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="inputSkin">Skin</label>
                    <div class="controls">
                        <input type="text" id="inputSkin" name="inputSkin" placeholder="Skin">
                    </div>
                </div>
                <p>Click <a href="./pages/loadoutS/loadout.php" onclick="window.open(this.href, 'mywin',
'left=20,top=20,width=726px,height=481,toolbar=1,resizable=0'); return false;" >here</a> to generate a inventory string</p>

        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
            <button type="submit" name="addPackageBtn" class="btn btn-primary">Save changes</button>
            </form>
        </div>
    </div>
<?php
}

?>
4

2 回答 2

0

改变

    //$dbh->query("INSERT INTO `cust_loadout`(`inventory`, `backpack`, `model`, `description`) VALUES ('". $packageInventory ."','". $packageBackpack ."','". $packageSkin ."','". $packageName . "'");
    $dbh->query("INSERT INTO `cust_loadout` (`inventory`, `backpack`, `model`, `description`) VALUES ([],[],[],[]");
}

    $stm = $dbh->prepare("INSERT INTO `cust_loadout`(`inventory`, `backpack`, `model`, `description`) VALUES (?, ?, ?, ?)");
    $stm->execute( array($packageInventory, $packageBackpack, $packageSkin, $packageName) )
}
于 2013-07-15T18:50:13.343 回答
0

您缺少VALUES集合的右括号。您的 SQL 字符串(格式化为空格)当前是:

INSERT INTO `cust_loadout`
  (`inventory`, `backpack`, `model`, `description`)
VALUES
  ([],[],[],[]
           -- ^ note that closing parenthesis is missing

也就是说,我真的不知道你打算[]做什么。如果它们应该是参数化值的匿名占位符,则 PDO 使用?字符...但不是使用query(),您将需要:

  1. prepare()该声明; 进而

  2. 要么绑定参数execute()并不带参数调用,要么execute()使用参数化参数数组调用。

于 2013-07-15T18:50:31.250 回答