我的查询不会执行

时间:2012-11-26 13:58:46

标签: php database forms pdo

我一直在编写一个包含安装程序的脚本,现在我陷入了无法执行查询的部分。我正在使用PDO。

所以,我的问题是第二个try块内的两个查询没有被执行。

这是我的代码:

<?php
$root_path = '../';
require("{$root_path}includes/functions_captcha.php");

$sub    = (isset($_GET['sub'])) ? $_GET['sub'] : '';
$step   = (isset($_POST['step'])) ? (int) $_POST['step'] : 0;
$submit = (isset($_POST['submit'])) ? true : false;

$driver_options = '';
$db_errors = array();

$drivers = PDO::getAvailableDrivers();
foreach ($drivers as $driver)
{
    $driver_options .= '<option value="' . $driver . '">' . $driver . '</option>';
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Q&amp;A CAPTCHA &bull; Installation</title>
    <link type="text/css" rel="stylesheet" href="../admin/style/stylesheet.css" />
</head>
<body>
    <div id="main">
        <a href="index.php"><h1>Q&amp;A CAPTCHA Installation</h1></a>

        <div class="content">
            <?php if (!$sub || ($sub == 'database' && !$step) && !$submit): ?>
            <form method="post" action="?sub=database">
            <table width="100%">
                    <tr class="table_top">
                        <th align="center" colspan="2">Database settings</th>
                    </tr>

                <tr class="row1">
                    <td><strong>Database type:</strong></td>
                    <td><select name="driver"><?php echo $driver_options; ?></select></td>
                </tr>
                <tr class="row2">
                    <td><strong>Database server hostname:</strong></td>
                    <td><input type="text" name="hostname" placeholder="localhost" required value="" /></td>
                </tr>
                <tr class="row1">
                    <td><strong>Database name:</strong></td>
                    <td><input type="text" name="name" required value="" /></td>
                </tr>
                <tr class="row2">
                    <td><strong>Database username:</strong></td>
                    <td><input type="text" name="username" required value="" /></td>
                </tr>
                <tr class="row1">
                    <td><strong>Database password:</strong></td>
                    <td><input type="password" name="password" value="" /></td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                        <input type="submit" name="submit" value="Proceed to next step &raquo;" />
                        <input type="hidden" name="step" value="2" />
                    </td>
                </tr>
            </table>
            </form>
            <?php endif; ?>
            <?php if ($sub == 'database' && $step == 2 && $submit): ?>
                <?php 
                    try {
                        $db = new PDO("{$_POST['driver']}:host={$_POST['hostname']};dbname={$_POST['name']}", $_POST['username'], $_POST['password']);

                        try {
                            $db->beginTransaction();
                            $time = time();

                            $sql = "CREATE TABLE captcha_config (
                                id mediumint(8) UNSIGNED NOT NULL auto_increment,
                                question varchar(255) DEFAULT '' NOT NULL,
                                date int(11) UNSIGNED DEFAULT '0' NOT NULL
                                answers TEXT DEFAULT '' NOT NULL);";
                            $db->query($sql);

                            $sql = "INSERT INTO captcha_config 
                                (question, date, answers) VALUES (
                                'What color can the sky have?', {$time}, 'Blue~~Grey~~Orange');";
                            $db->query($sql);

                            $db->commit();
                        }
                        catch (PDOException $e)
                        {
                            $db->rollBack();
                            $db_errors[] = (strpos($e->getMessage(), ']') !== false) ? substr(strrchr($e->getMessage(), ']'), 1) : $e->getMessage();
                        }
                    }
                    catch (PDOException $e)
                    {
                        $db_errors[] = (strpos($e->getMessage(), ']') !== false) ? substr(strrchr($e->getMessage(), ']'), 1) : $e->getMessage();
                    }

                    $db_errors = implode('<br />', $db_errors);
                ?>
            <form method="post" action="?sub=config">
            <table width="100%">
                <tr class="table_top">
                    <th align="center" colspan="2">Database connection</th>
                </tr>
                <tr class="row1">
                    <td align="center"><?php echo (empty($db_errors)) ? '<strong style="color: green;">Connection was successful</strong>' : "<strong style='color: red;'>{$db_errors}</strong>"; ?></td>
                </tr>
                <tr>
                    <td align="center">
                        <?php if (empty($db_errors)): ?> 
                            <input type="submit" name="submit" value="Proceed to next step &raquo;" />
                        <?php else: ?> 
                            <a href="index.php" class="button">&laquo; Back to previous step</a>
                        <?php endif; ?>
                        <input type="hidden" name="step" value="3" />
                    </td>
                </tr>
            </table>
            </form>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您在创建表格stmt中缺少逗号加上主键行

                    try {

                        $db->beginTransaction();
                        $time = time();
                        $sql = "CREATE TABLE captcha_config (
                            id mediumint(8) UNSIGNED NOT NULL auto_increment,
                            question varchar(255) DEFAULT '' NOT NULL,
                            date int(11) UNSIGNED DEFAULT '0' NOT NULL,
                            answers TEXT DEFAULT '' NOT NULL,
            PRIMARY KEY (id));";
                        $db->query($sql);

                        $sql = "INSERT INTO captcha_config 
                            (question, date, answers) VALUES (
                            'What color can the sky have?', {$time}, 'Blue~~Grey~~Orange');";
                        $db->query($sql);

                        $db->commit();
                    }

显示表格:

+----------------------+
| Tables_in_jonah_junk |
+----------------------+
| _adminfiles          |
| _adminlang           |
| _adminlog            |
| _adminprivs          |
| _adminroles          |
| _adminusers          |
| captcha_config       |
| sometable            |
+----------------------+
8 rows in set (0.00 sec)

mysql> select * from captcha_config;
+----+------------------------------+------------+--------------------+
| id | question                     | date       | answers            |
+----+------------------------------+------------+--------------------+
|  1 | What color can the sky have? | 1353940684 | Blue~~Grey~~Orange |
+----+------------------------------+------------+--------------------+