2014-01-13 110 views
0

我是pdo的新手。在这里,我试图使用下面的代码将数据插入到数据库中。但是,我无法将数据插入数据库。我收到以下错误PDO无法将数据插入数据库

Fatal error: Call to a member function prepare() on a non-object

我在SO和互联网上搜索了这个错误。有些人说在你的代码的顶部添加global $conn;。我添加了这些代码,但我得到相同的错误。任何人都告诉我,如果我想清除此错误,我应该怎么做?

的config.php

 <?php 
      $user = "root"; 
      $password = "password"; 

      try 
      { 
       $conn = new PDO('mysql:host=localhost;dbname=evouchers', $user, $password); 
       $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
      } 
      catch(PDOException $e) 
      { 
       'DATABASE CONNECTION ERROR' .$e->getMessage(); 
      } 
     ?> 

    **Database.php** 

     <?php 
      session_start(); 

      include('config.php'); 

       if(isset($_POST['submit_val'])) 
       { 
        $cmeal = $_POST['meal']; 
        try 
        { 
         $stmt = $conn->prepare("INSERT INTO ebmealplans (MealPlanName, CreatedOn) VALUES (:cmeal, NOW())"); 
         $conn->errorInfo(); 
         $stmt->bindParam(':cmeal', $cmeal, PDO::PARAM_STR); 
         $stmt->execute(); 
        } 
        catch(PDOException $e) 
        { 
         'Query failed to insert into database ' .$e->getMessage(); 
        } 

        $croom = $_POST['room']; 
        $ref_key = $conn->lastInsertId(); 
        try 
        { 
         $stmt = $conn->prepare("INSERT INTO ebroomtypes (RoomTypeName, CreatedOn) VALUES (:croom, NOW())"); 
         $conn->errorInfo(); 
         $stmt->bindParam(':croom', $croom, PDO::PARAM_STR); 
         $stmt->execute(); 
        } 
        catch(PDOException $e) 
        { 
         'Query failed to insert into database ' .$e->getMessage(); 
        } 


<*************** UPDATED CODES ***************> 


    $creference = $_POST['reference']; 
      $crefdate = $_POST['refdate']; 
      $ccin = $_POST['cin']; 
      $cout = $_POST['out']; 
      $cgname = $_POST['gname']; 
      $ctotaladults = $_POST['totaladults']; 
      $cchildrens = $_POST['childrens']; 
      $cinfants = $_POST['infants']; 
      $cgphone = $_POST['gphone']; 
      $cgemail = $_POST['gemail']; 
      $cgfax = $_POST['gfax']; 
      $cgaddress1 = $_POST['gaddress1']; 
      $cgaddress2 = $_POST['gaddress2']; 
      $cregion = $_POST['region']; 
      $ccity = $_POST['city']; 
      $cstate = $_POST['city_state']; 
      $ccountry = $_POST['country']; 
      $ccurrency = $_POST['currency']; 
      $ccurrencyto = $_POST['tocurrency']; 
      $camount = $_POST['camount']; 
      $ccurrencyvalue = $_POST['currencyvalue']; 
      $voucher_fk = $conn->lastInsertId(); 
      try 
      { 
       $stmt = $conn->prepare("INSERT INTO ebvouchers (VoucherReference, BookingDate, CheckIndate, CheckOutDate, MealPlanID_Fk, RoomTypeID_Fk, GuestName, TotalAdults, Childrens, Infants, GuestPhone, GuestEmail, GuestFax, GuestAddressLine1, GuestAddressLine2, GuestRegion, GuestCity, GuestState, GuestCountry, GuestCurrency, GuestCurrencyTo, CurrencyAmount, GuestCurrencyValue, VoucherCreatedOn) VALUES (:reference, :refdate, :ccin, :cout, :r_key, :r_key, :gname, :totaladults, :childrens, :infants, :gphone, :gemail, :gfax, :gaddress1, :gaddress2, :gregion, :city, :state, :country, :currency, :currencyto, :amount, :currencyvalue, NOW())"); 
       $conn->errorInfo(); 
       $stmt->bindParam(':reference', $creference, PDO::PARAM_STR); 
       $stmt->bindParam(':refdate', $crefdate, PDO::PARAM_STR); 
       $stmt->bindParam(':ccin', $ccin, PDO::PARAM_STR); 
       $stmt->bindParam(':cout', $cout, PDO::PARAM_STR); 
       $stmt->bindParam(':r_key', $ref_key, PDO::PARAM_STR); 
       $stmt->bindParam(':r_key', $ref_key, PDO::PARAM_STR); 
       $stmt->bindParam(':gname', $cgname, PDO::PARAM_STR); 
       $stmt->bindParam(':totaladults', $ctotaladults, PDO::PARAM_STR); 
       $stmt->bindParam(':childrens', $cchildrens, PDO::PARAM_STR); 
       $stmt->bindParam(':infants', $cinfants, PDO::PARAM_STR); 
       $stmt->bindParam(':gphone', $cgphone, PDO::PARAM_STR); 
       $stmt->bindParam(':gemail', $cgemail, PDO::PARAM_STR); 
       $stmt->bindParam(':gfax', $cgfax, PDO::PARAM_STR); 
       $stmt->bindParam(':gaddress1', $cgaddress1, PDO::PARAM_STR); 
       $stmt->bindParam(':gaddress2', $cgaddress2, PDO::PARAM_STR); 
       $stmt->bindParam(':gregion', $cregion, PDO::PARAM_STR); 
       $stmt->bindParam(':city', $ccity, PDO::PARAM_STR); 
       $stmt->bindParam(':state', $cstate, PDO::PARAM_STR); 
       $stmt->bindParam(':country', $ccountry, PDO::PARAM_STR); 
       $stmt->bindParam(':currency', $ccurrency, PDO::PARAM_STR); 
       $stmt->bindParam(':currencyto', $ccurrencyto, PDO::PARAM_STR); 
       $stmt->bindParam(':amount', $camount, PDO::PARAM_STR); 
       $stmt->bindParam(':currencyvalue', $ccurrencyvalue, PDO::PARAM_STR); 
       $stmt->execute(); 
      } 
      catch(PDOException $e) 
      { 
       'Query failed to insert into database ' .$e->getMessage(); 
      } 

<*************** UPDATED CODES ***************> 

        foreach ($_POST['slno'] as $key=>$slno) 
        { 
         $date = $_POST['date'][$key]; 
         $particulars = $_POST['particulars'][$key]; 
         $noofnights = $_POST['noofnights'][$key]; 
         $rate = $_POST['rate'][$key]; 
         $price = $_POST['price'][$key]; 
         $tax = $_POST['tax'][$key]; 
         $nettotal = $_POST['nettotal']; 
         $totalamount = $_POST['totalamount']; 
         $finaltotal = $_POST['finaltotal']; 
         $c_date = $date; 
         $c_slno = $slno; 
         $c_particulars = $particulars; 
         $c_noofnights = $noofnights; 
         $c_rate = $rate; 
         $c_price = $price; 
         $c_tax = $tax; 
         $c_nettotal = $nettotal; 
         $c_totalamount = $totalamount; 
         $c_finaltotal = $finaltotal; 

         try 
         { 
          $stmt = $conn->prepare("INSERT INTO ebvouchertariffs (TariffSlNo, TariffDate, TariffParticulars, NoOfNights, TariffRate, TariffPrice, TariffTax, TariffNetTotal, TariffAddTotal, TariffFinalTotal, VoucherID_Fk, CreatedOn) VALUES (:c_slno, :c_date, :c_particulars, :c_noofnights, :c_rate, :c_price, :c_tax, :c_nettotal, :c_totalamount, :c_finaltotal, :voucher_fk, NOW())"); 
         $conn->errorInfo(); 
         $stmt->bindParam(':c_slno', $c_slno, PDO::PARAM_STR); 
         $stmt->bindParam(':c_date', $c_date, PDO::PARAM_STR); 
         $stmt->bindParam(':c_particulars', $c_particulars, PDO::PARAM_STR); 
         $stmt->bindParam(':c_noofnights', $c_noofnights, PDO::PARAM_STR); 
         $stmt->bindParam(':c_rate', $c_rate, PDO::PARAM_STR); 
         $stmt->bindParam(':c_price', $c_price, PDO::PARAM_STR); 
         $stmt->bindParam(':c_tax', $c_tax, PDO::PARAM_STR); 
         $stmt->bindParam(':c_nettotal', $c_nettotal, PDO::PARAM_STR); 
         $stmt->bindParam(':c_totalamount', $c_totalamount, PDO::PARAM_STR); 
         $stmt->bindParam(':c_finaltotal', $c_finaltotal, PDO::PARAM_STR); 
         $stmt->bindParam(':voucher_fk', $voucher_fk, PDO::PARAM_STR); 
         $stmt->execute(); 
         } 
         catch(PDOException $e) 
         { 
          'Query failed to insert into database ' .$e->getMessage(); 
         } 

         $conn = null; 

         } 
       } 
      ?> 
+0

哪里是你的$声明参数conn? – rccoros

+2

**数据**是复数和不可数的! – undone

+0

@undone:复数的意思?不明白 – Karuppiah

回答

0

你没有打印例外。如果你有任何例外,你将不会得到它。我认为你的问题$conn= null; $ conn = null;这一行使得你的连接对象无效,并且在执行完这一行之后,你有无效的pdo对象。为此,你在循环中得到了这个错误。它应该在执行该行之前执行所有其他查询。只要删除这一行。并打印你这样的异常消息:

试试这个:

<?php 
    session_start(); 

    include('config.php'); 

     if(isset($_POST['submit_val'])) 
     { 
      $cmeal = $_POST['meal']; 
      try 
      { 
       $stmt = $conn->prepare("INSERT INTO ebmealplans (MealPlanName, CreatedOn) VALUES (:cmeal, NOW())"); 
       $conn->errorInfo(); 
       $stmt->bindParam(':cmeal', $cmeal, PDO::PARAM_STR); 
       $stmt->execute(); 
      } 
      catch(PDOException $e) 
      { 
       die('Query failed to insert into database ' .$e->getMessage()); 
      } 

      $croom = $_POST['room']; 
      $ref_key = $conn->lastInsertId(); 
      try 
      { 
       $stmt = $conn->prepare("INSERT INTO ebroomtypes (RoomTypeName, CreatedOn) VALUES (:croom, NOW())"); 
       $conn->errorInfo(); 
       $stmt->bindParam(':croom', $croom, PDO::PARAM_STR); 
       $stmt->execute(); 
      } 
      catch(PDOException $e) 
      { 
       die('Query failed to insert into database ' .$e->getMessage()); 
      } 

      foreach ($_POST['slno'] as $key=>$slno) 
      { 
       $date = $_POST['date'][$key]; 
       $particulars = $_POST['particulars'][$key]; 
       $noofnights = $_POST['noofnights'][$key]; 
       $rate = $_POST['rate'][$key]; 
       $price = $_POST['price'][$key]; 
       $tax = $_POST['tax'][$key]; 
       $nettotal = $_POST['nettotal']; 
       $totalamount = $_POST['totalamount']; 
       $finaltotal = $_POST['finaltotal']; 
       $c_date = $date; 
       $c_slno = $slno; 
       $c_particulars = $particulars; 
       $c_noofnights = $noofnights; 
       $c_rate = $rate; 
       $c_price = $price; 
       $c_tax = $tax; 
       $c_nettotal = $nettotal; 
       $c_totalamount = $totalamount; 
       $c_finaltotal = $finaltotal; 

       try 
       { 
        $stmt = $conn->prepare("INSERT INTO ebvouchertariffs (TariffSlNo, TariffDate, TariffParticulars, NoOfNights, TariffRate, TariffPrice, TariffTax, TariffNetTotal, TariffAddTotal, TariffFinalTotal, VoucherID_Fk, CreatedOn) VALUES (:c_slno, :c_date, :c_particulars, :c_noofnights, :c_rate, :c_price, :c_tax, :c_nettotal, :c_totalamount, :c_finaltotal, :voucher_fk, NOW())"); 
       $conn->errorInfo(); 
       $stmt->bindParam(':c_slno', $c_slno, PDO::PARAM_STR); 
       $stmt->bindParam(':c_date', $c_date, PDO::PARAM_STR); 
       $stmt->bindParam(':c_particulars', $c_particulars, PDO::PARAM_STR); 
       $stmt->bindParam(':c_noofnights', $c_noofnights, PDO::PARAM_STR); 
       $stmt->bindParam(':c_rate', $c_rate, PDO::PARAM_STR); 
       $stmt->bindParam(':c_price', $c_price, PDO::PARAM_STR); 
       $stmt->bindParam(':c_tax', $c_tax, PDO::PARAM_STR); 
       $stmt->bindParam(':c_nettotal', $c_nettotal, PDO::PARAM_STR); 
       $stmt->bindParam(':c_totalamount', $c_totalamount, PDO::PARAM_STR); 
       $stmt->bindParam(':c_finaltotal', $c_finaltotal, PDO::PARAM_STR); 
       $stmt->bindParam(':voucher_fk', $voucher_fk, PDO::PARAM_STR); 
       $stmt->execute(); 
       } 
       catch(PDOException $e) 
       { 
        die('Query failed to insert into database ' .$e->getMessage()); 
       } 

       //$conn = null; 

       } 
     } 
    ?> 
+0

没有相同的错误... – Karuppiah

+0

@ user3181207:你有没有得到任何错误? –

+0

同样的错误 - 致命错误:调用一个成员函数prepare()在非对象 – Karuppiah