-1

インストーラーを含むスクリプトに取り組んでいますが、クエリを実行できない部分に行き詰まっています。PDOを使用しています。

tryしたがって、私の問題は、2 番目のブロック内の 2 つのクエリが実行されていないことです。

これは私のコードです:

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

1 に答える 1