PDO查询:SQLSTATE [42000]

时间:2013-07-15 18:46:25

标签: php mysql pdo

当尝试使用PDO将数据插入mysql表时,我收到以下错误。

  

致命错误:带有消息'SQLSTATE [42000]的未捕获异常'PDOException':语法错误或访问冲突:1064 SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在/home/gaz492/public_html/dayz/user/pages/admin/vip.php:26中的“第1行”附近使用正确的语法。堆栈跟踪:#0 / home / gaz492 / public_html / dayz / user / pages / admin / vip.php(26):PDO-&gt; 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中抛出< / p>

以下是该页面上的代码。

如果有人能够帮助它会意味着很多,因为我一直在寻找修复,但找不到任何有用的东西。询问您是否需要更多信息,我很乐意提供您所需的信息。

<?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
}

?>

2 个答案:

答案 0 :(得分: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) )
}

答案 1 :(得分:0)

您缺少VALUES集的右括号。您的SQL字符串,格式为空格,目前是:

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

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

  1. prepare()声明;然后

  2. bind the parameters并且不带参数调用execute(),或使用参数化参数数组调用execute()