首页 > 解决方案 > 使用消息“object(PDO)#1 (0)”连接到 Mysql 数据库时出错

问题描述

我有点缺乏经验,我试图创建一个带有 php 和 DOP 连接的小型预订表单,但我总是收到 prepar () 属性的错误。连接上 var_dump () 的 esegundo 我已经看到它返回了这个答案......帮您一把,我告诉您我已经取消标记 php.ini 文件中的 PDO 扩展名并且我使用了 LAMP 堆栈在 Ubuntu 机器上。我不知道如何移动,能否有好人帮助我了解我错在哪里并让我找到解决方案并让我了解我的错误在哪里?

为了清楚起见,放置我的代码,并感谢任何想要登录的人

代码:

<?php
try{
  /*Connection file db.php */
  $dsn = "mysql:host=localhost;dbName=lamp";
  $conn = new PDO($dsn, "root", "StraMa87");
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "connessione avvenuta con successo";
}catch(PDOException $e){
$e->getMessage();
}
?>

这是管理预订的类文件:

<?php
/**
 *  Booking
 */
class Booking
{
  public $db;
  function __construct($db)
  {
    var_dump($db);
    $this->db = $db;
  }
  public function booking_table($post){
$nome = trim($post["nome"]);
$email = filter_var( filter_var( trim( $post["email"] ), FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL );
$tel = trim( $post["tel"] );
$intolleranze = trim($post["intolleranza"]);
$data = trim($post["date"]);
$hour = trim($post["hour"]);
/* Query  */
$q = "INSERT INTO booking (nome,telefono,email,intolleranze,data_prenotazione,hour) VALUES ('$nome', '$tel', '$email', '$intolleranze', '$data', '$hour')";
try {
  $rq = $this->db->prepare($q,  array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  $rq->bindParam(':nome', $nome, PDO::PARAM_STR);
  $rq->bindParam(':tel', $tel, PDO::PARAM_STR);
  $rq->bindParam(':email', $email, PDO::PARAM_STR);
  $rq->bindParam(':intolleranze', $intolleranze, PDO::PARAM_STR);
  $rq->bindParam(':data', $data, PDO::PARAM_STR);
  $rq->bindParam(':hour', $hour, PDO::PARAM_STR);
  $rq->execute();
} catch (PDOException $e) {
  $e->getMessage();
}

  }
}

这是必须发送数据的表格(html不是我做的,因为我的目的是在php和后端练习):

<?php
require_once('./booking/db.php');
require_once('./booking/booking.php');
ini_set('display_error', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
 $booking = new Booking($conn);
if( $_POST ){
  $booking->booking_table($_POST);
}
 ?>
<!DOCTYPE html>
 <html lang="en">
   <head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
 <meta name="description" content="">
 <meta name="author" content="">
 <title>Business Casual - Start Bootstrap Theme</title>
 <!-- Bootstrap core CSS -->
 <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
 <!-- Custom fonts for this template -->
 <link href="https://fonts.googleapis.com/css?family=Raleway:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">
 <link href="https://fonts.googleapis.com/css?family=Lora:400,400i,700,700i" rel="stylesheet">
 <!-- Custom styles for this template -->
 <link href="assets/css/master.css" rel="stylesheet">
  </head>
   <body>
 <h1 class="site-heading text-center text-white d-none d-lg-block">
   <span class="site-heading-upper text-primary mb-3">A Free Bootstrap 4 Business Theme</span>
   <span class="site-heading-lower">Business Casual</span>
 </h1>
 <!-- Navigation -->
 <nav class="navbar navbar-expand-lg navbar-dark py-lg-4" id="mainNav">
   <div class="container">
     <a class="navbar-brand text-uppercase text-expanded font-weight-bold d-lg-none" href="#">Start Bootstrap</a>
     <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
       <span class="navbar-toggler-icon"></span>
     </button>
     <div class="collapse navbar-collapse" id="navbarResponsive">
       <ul class="navbar-nav mx-auto">
         <li class="nav-item px-lg-4">
           <a class="nav-link text-uppercase text-expanded" href="index.php">Home
             <span class="sr-only">(current)</span>
           </a>
         </li>
         <li class="nav-item px-lg-4">
           <a class="nav-link text-uppercase text-expanded" href="about.php">About</a>
         </li>
         <li class="nav-item px-lg-4">
           <a class="nav-link text-uppercase text-expanded" href="products.php">Products</a>
         </li>
         <li class="nav-item px-lg-4">
           <a class="nav-link text-uppercase text-expanded" href="store.php">Store</a>
         </li>
       </ul>
     </div>
   </div>
 </nav>
 <section class="page-section cta">
   <div class="container">
     <div class="row">
       <div class="col-xl-9 mx-auto">
         <div class="cta-inner text-center rounded">
           <h2 class="section-heading mb-5">
             <span class="section-heading-upper">Prenota con noi</span>
             <span class="section-heading-lower">siamo aperti</span>
           </h2>
           <form class="" id='booking' action="<?php $_SERVER["PHP_SELF"]; ?>" method="post">
             <div class="row">
               <div class="form-group col-4">
                 <label for="name">Nome</label>
                 <input type="text" class="form-control" name="nome" id="name" required>
               </div>

               <div class="form-group col-4">
                 <label for="tel">Telefono</label>
                 <input type="tel" class="form-control" name="tel" id="tel" required>
               </div>

               <div class="form-group col-4">
                 <label for="email">Email</label>
                 <input type="email" class="form-control" name="email" id="email" required>
               </div>

               <div class="form-group col-4">
                 <label for="intolleranza">Intolleranze</label>
                 <select class="custom-select" required name="intolleranza" id="intolleranza">
                   <option selected>Problemi da dichiarare</option>
                   <option value='nessuno'>Nessuno</option>
                   <option value="celiachia">Celiachia</option>
                   <option value="Intolleranza">Intolleranza</option>
                   <option value="3">Allergia</option>
                 </select>
               </div>


               <div class="form-group col-4">
                 <label for="email">Data di prenotazione</label>
                 <input type="date" class="form-control" name="date" id="date" required>
               </div>

               <div class="form-group col-4">
                 <label for="hour">Orario</label>
                 <input type="time" class="form-control" name="hour" id="hour" required>
               </div>

               <div class="form-group col">
                 <button class="btn btn-primary mt-3" type="submit" name="submit" id='submit'>Prenota</button>
               </div>

             </div> <!-- row -->





           </form>
           <p class="address my-5">
             <em>
               <strong>1116 Orchard Street</strong>
               <br>
               Golden Valley, Minnesota
             </em>
           </p>
           <p class="mb-0">
             <small>
               <em>Call Anytime</em>
             </small>
             <br>
             (317) 585-8468
           </p>
         </div>
       </div>
     </div>
   </div>
 </section>

 <section class="page-section about-heading">
   <div class="container">
     <img class="img-fluid rounded about-heading-img mb-3 mb-lg-0" src="img/about.jpg" alt="">
     <div class="about-heading-content">
       <div class="row">
         <div class="col-xl-9 col-lg-10 mx-auto">
           <div class="bg-faded rounded p-5">
             <h2 class="section-heading mb-4">
               <span class="section-heading-upper">Strong Coffee, Strong Roots</span>
               <span class="section-heading-lower">About Our Cafe</span>
             </h2>
             <p>Founded in 1987 by the Hernandez brothers, our establishment has been serving up rich coffee sourced from artisan farmers in various regions of South and Central America. We are dedicated to travelling the world, finding the best coffee, and bringing back to you here in our cafe.</p>
             <p class="mb-0">We guarantee that you will fall in
               <em>lust</em>
               with our decadent blends the moment you walk inside until you finish your last sip. Join us for your daily routine, an outing with friends, or simply just to enjoy some alone time.</p>
           </div>
         </div>
       </div>
     </div>
   </div>
 </section>

 <footer class="footer text-faded text-center py-5">
   <div class="container">
     <p class="m-0 small">Copyright &copy; Your Website 2018</p>
   </div>
 </footer>

 <!-- Bootstrap core JavaScript -->
 <!-- <script src="vendor/jquery/jquery.min.js"></script>
 <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script> -->
  </body>
 </html>

标签: phppdo

解决方案


推荐阅读