doudihuang7642 2014-01-13 04:08
浏览 117
已采纳

PDO无法将数据插入数据库

I'm newbie with pdo. Here i'm trying to insert the datas into database using this below coding. But, i cannot able to insert the datas into database. I'm getting this following error

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

I searched on SO and internet about this error. Some people says add global $conn; at the top of your code. I added these code but i'm getting same error. Anyone tell me what should i do if i want to clear this error?

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;

                        }
                }
            ?>
  • 写回答

1条回答 默认 最新

  • dongtanliefang8765 2014-01-13 04:23
    关注

    You did not print your exception. If you have any exception you will not get it. and i think problem in your $conn= null; $conn = null; this line makes your connection object invalid and after executing this line you have invalid pdo object.for this you have got this error in loop. it should execute all other query before this line is executed. just remove this line. and print your exception message like this:

    Try this:

    <?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;
    
                    }
            }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面