调用未定义的方法PDO :: execute(),之前的答案并没有解决错误

时间:2014-05-08 19:35:36

标签: php pdo

我正在尝试通过php创建一个CSV导入功能,它将信息存储在MySQL数据库中,但由于某种原因,它会出现以下错误: -

致命错误:在第25行的/home/a/public_html/admin/admin_import_product.php中调用未定义的方法PDO :: execute()

<?php  
session_start();
include '../inc/inc.functions.php';
include '../dbconnector.php';
include '../dbpdo.php';
include '../inc/inc.config.php';
if((isset($_SESSION['admin'])) && ($_SESSION['admin'] == 1))
{
    $adminusername = $_SESSION['username'];
    $date=date('Y-m-d');
    if ($_FILES[csv][size] > 0) { 

        //get the csv file 
        $file = $_FILES[csv][tmp_name]; 
        $handle = fopen($file,"r"); 
         //prepare the statement for the insertion of the record via PDO
        try{
            global $conn;

            $statement = $conn->prepare("INSERT INTO products(category,productname,baseprice,basepricewd,basepricenw,addedby,addedon) VALUES (?,?,?,?,?,?,?)");
            //loop through the csv file and insert into database 

            do { 
                if ($data[0]) { 
                    $statement=$conn->execute(array(
                        $data[0],
                        $data[1],
                        $data[2],
                        $data[3],
                        $data[4],
                        $adminusername,
                        $date));
                } 
            } while ($data = fgetcsv($handle,5000,",","'")); 
        // 
    }//try
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }


        //redirect 
       // header('Location: admin_importproducts_home.php?success=1'); die; 
        echo "Products imported successfully";

    } 

 }
   else
{
    header('Location:http://a.co.uk/' . $config["admindir"] . '/adminlogin');
}
?>

任何建议都有帮助。

1 个答案:

答案 0 :(得分:5)

您正在从错误的对象/资源(无论PDO是什么)调用execute()

// this is the proper chain of calls as stated in the documentation
$statement = $conn->prepare();
$execute_result = $statement->execute();

// execute() returns a boolean so you can use an if/else block to know if your query is hunky-dory
if($execute_result)
{
    // yay the query had no errors!
}
else
{
    // aww snap, you messed up now!
}

http://www.php.net/manual/en/pdostatement.execute.php