PHP / MySQL查询不起作用

时间:2015-03-16 03:40:16

标签: php mysql database forms

我试图将轮胎添加到表格中的轮胎数据库中。如果我使用var_dump,查询是正确的,但它不会在数据库中插入任何内容。这是我的代码。

 <?php

require('../includes/database_connection.php');
error_reporting(E_ALL);
   ini_set('display_errors', 1);
    $idtires = '';
   $size = $_POST["size"];
 $idbrand = $_POST["idbrand"];
$price = $_POST["price"];
$mileage = $_POST["mileage"];
$idterrain = $_POST["idterrain"];
$idvehicle = $_POST["idvehicle"];
$idtiremodel = $_POST["idtiremodel"];

function add_tire($idtires, $size,  $idbrand, $price, $mileage, $idterrain,     $idvehicle, $idtiremodel) {
     global $db;
     $query = "INSERT INTO tires
                  (idtires, size, idbrand, price, mileage, idterrain, idvehicle, idtiremodel)
               VALUES
                  ($idtires, '$size',  '$idbrand','$price', '$mileage', '$idterrain', '$idvehicle', '$idtiremodel')";
   var_dump($query);
   // var_dump($db); 
     $db->exec($query);

}


  add_tire($idtires, $size, $idbrand, $price, $mileage, $idterrain, $idvehicle, $idtiremodel);



include('add.php');

?>

这里是ADD.PHP表格:

<table>

            <form method='post' action='addatire.php' name='addform' id='addform'>
                <tr>
                    <td>
                        <p>
                            <label for='size'>Size:</label><br />
                        </p>
                     </td>
                     <td>
                        <p>
                            <input type='text' name='size' required value="<?php echo(isset($size['size'])); ?>"/>
                        </p>
                    </td>
                </tr>
                 <tr>
                    <td>
                        <p>
                            <label for='idbrand'>Brand ID:</label><br/>
                        </p>
                    </td>
                     <td>
                        <p>
                        <input type='text' name='idbrand' required value="<?php echo(isset($idbrand['idbrand'])); ?>"/>
                        </p>
                    </td>
                 </tr>
                 <tr>
                    <td>
                        <p>
                            <label for='price'>Price:</label><br/>
                        </p></td>
                    <td><p>
                            <input type='text' name='price' required value="<?php echo(isset($price['price'])); ?>"/>
                        </p></td>
                 </tr>
                 <tr>
                    <td>
                        <p>
                            <label for='mileage'>Mileage:</label><br/>
                        </p></td>
                      <td><p>
                            <input type='text' name='mileage' required value="<?php echo(isset($mileage['mileage'])); ?>"/>
                        </p></td>
                 </tr>
                 <tr>
                    <td>
                        <p>
                            <label for='idterrain'>Terrain ID:</label><br/>
                        </p></td>
                     <td><p>
                            <input type='text' name='idterrain' required value="<?php echo(isset($idterrain['idterrain'])); ?>"/>
                        </p></td>
                 </tr>
                 <tr>
                  <td>
                        <p>
                            <label for='idvehicle'>Vehicle ID:</label><br/>
                        </p></td>
                     <td><p>
                            <input type="text" name="idvehicle" required value="<?php echo(isset($idvehicle['idvehicle'])); ?>"/>

                        </p></td>
                  </tr>

                 <tr>
                     <td>
                        <p>
                            <label for='idtiremodel'>Tire Model ID:</label><br/>
                        </p></td>
                    <td><p>
                            <input type="text" name="idtiremodel" required value="<?php echo(isset($idtiremodel['idmodel'])); ?>"/>

                        </p></td>
                </tr>



                 <tr>
                      <td><p>
                            <input type="submit" name="action" value="Add A Tire"/>

                        </p></td>
                 </tr>
             </form>
          </table>



                    <?php echo get_tires(); ?>
                 </div>


?>

HERE是数据库连接:

<?php
    if ($_SERVER['HTTP_HOST'] == "localhost" OR $_SERVER['HTTP_HOST'] == "127.0.0.1") {
        // login locally
        $dsn = 'mysql:host=localhost;dbname=?';
        $username = ?;
        $password = ?;

 } else {
        // login online
         $dsn = 'mysql:host=localhost;dbname=?';
           $username = '?';
         $password = '?';

    }

    try {
    $db = new PDO($dsn, $username, $password);
    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        include('database_error.php');
        exit();

    }
?>

我的用户名和密码不是真的吗?但是,为了隐私和安全,我只是在这里使用它们。

1 个答案:

答案 0 :(得分:0)

您可能在插入时遇到错误而只是看不到它:

$db->exec($query) or die(print_r($db->errorInfo(), true));