dongzhang1987 2014-10-23 14:36
浏览 42
已采纳

无法运行查询:1048列'element_oppningar'不能为空

I'm using PHPExcel to import xls to mysql. I recently switched connection to PDO. But then an error accord. Fields in my xls that is NULL are no longer accepted. Why? How can i change my code to accept NULL value?

PHP:

if(isset($_POST["Import"])){
echo '<a href=".php">Fortsätt...</a><br  />';
echo $path=$_FILES["file"]["tmp_name"];

//Load file into PHPExcel
$objPHPExcel = PHPExcel_IOFactory::load($path);

//Loop threw file to get data
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = 'J'; //$worksheet->getHighestColumn(''); // e.g 'F'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;

//Echo file info
echo "<br>The worksheet ".$worksheetTitle." has ";
echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
echo ' and ' . $highestRow . ' row.';
echo '<br>Data: <table border="1"><tr>';

//Loop threw colum, rows and cells
for ($row = 2; $row <= $highestRow; ++ $row) {
    echo '<tr>';
    for ($col = 0; $col < $highestColumnIndex; ++ $col) {
        $cell = $worksheet->getCellByColumnAndRow($col, $row);
        $val = $cell->getCalculatedValue();
        //$dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
        echo '<td>' . $val . '<br></td>';
    }
    echo '</tr>';
}
echo '</table>';
}

for ($row = 2; $row <= $highestRow; ++ $row) {
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getCalculatedValue();
}
// Prepare Query 
    $query = "INSERT INTO table(
                objekt_nr,
                objekt_rev,
                element_nr,
                element_hojd,
                element_typ,
                element_langd,
                element_oppningar,
                element_vikt,
                element_ritare,
                element_status) 
            VALUES ( 
            :objekt_nr, 
            :objekt_rev, 
            :element_nr,
            :element_hojd,
            :element_typ,
            :element_langd,
            :element_oppningar,
            :element_vikt,
            :element_ritare,
            :element_status
        )"; 

    // Security measures
    $query_params = array(  
        ':objekt_nr' => $val[0],
        ':objekt_rev' => $val[1],
        ':element_nr' => $val[2],
        ':element_hojd' => $val[3],
        ':element_typ' => $val[4],
        ':element_langd' => $val[5],
        ':element_oppningar' => $val[6],
        ':element_vikt' => $val[7],
        ':element_ritare' => $val[8],
        ':element_status' => $val[9]
    ); 
    try {  
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); }
  • 写回答

1条回答 默认 最新

  • douhu3424 2014-10-23 14:38
    关注

    To accept a NULL value you have to edit the database table, not the code. Your message error is generated by MySQL, not the PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名