PHPExcelReader无法写入数据库

时间:2012-05-08 21:33:30

标签: php mysql sql

我正在使用来自http://sourceforge.net/projects/phpexcelreader/的phpexcelreaderclass,我一直在关注这个问题:http://rackerhacker.com/2008/11/07/importing-excel-files-into-mysql-with-php/

现在一切似乎进展顺利,但似乎没有在数据库中插入任何东西?它回复了表格中的数据,所以我猜它正在读取并显示回来,但它只是没有写入MySQL数据库。

<?php
    ini_set('display_errors',1); 
    error_reporting(E_ALL);
    require_once 'Excel/reader.php';
    $data = new Spreadsheet_Excel_Reader();
    $data->setOutputEncoding('CP1251');
    $data->read('Export.xls');

    $conn = mysql_connect("localhost","root","");
    mysql_select_db("excel",$conn);

    for ($x = 2; $x <= count($data->sheets[1]["cells"]); $x++) {
        $rep_num = $data->sheets[1]["cells"][$x][1];
        $description = $data->sheets[1]["cells"][$x][2];
        $repair_type = $data->sheets[1]["cells"][$x][3];
        $sql = "INSERT INTO data (`rep_num`,`description`,`repair_type`) 
            VALUES ('$rep_num',$description,'$repair_type')";
        echo $sql."\n";
        mysql_query($sql);
}

我想这是显而易见的事情,我确信,提前谢谢。

2 个答案:

答案 0 :(得分:1)

$sql = "INSERT INTO data (rep_num, description, repair_type) VALUES ('$rep_num','$description','$repair_type')";

确保您的字段名称正确无误。

答案 1 :(得分:0)

检查你的mysql_query方法结果如下:

$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

php.net - mysql query description