2012-11-26 3 views
-1

J'ai travaillé sur un script qui inclut un programme d'installation et maintenant je suis coincé dans une partie où je ne peux pas exécuter ma requête. J'utilise PDO. Donc, mon problème est que les deux requêtes à l'intérieur du deuxième bloc try ne sont pas exécutées.Ma requête ne sera pas exécutée

Ceci est mon code:

<?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> 
+0

Et exactement se passe ou ne se passe pas? Avez-vous des erreurs? Pouvez-vous s'il vous plaît réduire cela au code nécessaire? – deceze

+0

Les deux requêtes à l'intérieur du deuxième bloc 'try' ne sont pas exécutées. – Aborted

+0

On dirait un mélange sauvage de PDO et de l'extension mysql classique ... – arkascha

Répondre

1

vous manque une virgule dans votre create table stmt plus la ligne clé primaire

    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(); 
        } 

show tables:

+----------------------+ 
| 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 | 
+----+------------------------------+------------+--------------------+ 
+0

Super! Je vous remercie. Cela montre l'importance d'une telle virgule, elle peut donner un mal de tête terrible. – Aborted

+0

nous le faisons tous np – Drew

Questions connexes