MySQL / PHP将空值上传到时间戳类型属性

时间:2018-07-04 09:14:53

标签: php mysql

我正在尝试使用php将csv文件上传到Mysql数据库。

属性/列名称closed的类型为timestamp,默认值为Null

我要上传的文件在这些列中有日期,但是其中一些没有日期,并且在csv文件中显示为空。

这些空值需要作为默认Null上载到closed列中。

我正在尝试上传:

INSERT INTO work_tickets(wt_id, wt_created, closed, client_id, agent_id)  
VALUES ('$wt_id', '$wt_created', '$closed', '$client_id', '$agent_id')

并尝试过($ client [21]是日期值):

    if (is_null($client[21])) {
        $closed = NULL;
    } else {
        $closed = $mysqli->real_escape_string($client[21]);
    } 

和:

    if (empty($client[21])) {
        $closed = NULL;
    } else {
        $closed = $mysqli->real_escape_string($client[21]);
    } 

和:

    if (empty($client[21])) {
        $closed = 'default';
    } else {
        $closed = $mysqli->real_escape_string($client[21]);
    } 

,并且还尝试将其保持原样;

$closed = $mysqli->real_escape_string($client[21]);

但是所有这些结果是未插入空行。

如何上传这些内容以便将其插入Null

或者,我意识到如果我在插入语句中排除$closed值,它将恢复为默认值null,但是我将执行两个插入语句吗?

编辑:完整的php代码:

<?php
//load the database configuration file
include 'config1.php';

if(isset($_POST['importSubmit'])){

    //validate whether uploaded file is a csv file
    $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
    if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
        if(is_uploaded_file($_FILES['file']['tmp_name'])){

            //open uploaded csv file with read only mode
            $csvFile = fopen($_FILES['file']['tmp_name'], "r");

            //skip first line
            fgetcsv($csvFile);

            //parse data from csv file line by line
            while(($client = fgetcsv($csvFile)) !== FALSE){


                $wt_id = $mysqli->real_escape_string($client[0]);
                $wt_created = $mysqli->real_escape_string($client[3]);
                $client_address = $mysqli->real_escape_string($client[4]);
                $prob_type = $mysqli->real_escape_string($client[6]);
                $prob_descr = $mysqli->real_escape_string($client[7]);
                $follow_up1 = $mysqli->real_escape_string($client[9]);
                $follow_up2 = $mysqli->real_escape_string($client[10]);
                $follow_up3 = $mysqli->real_escape_string($client[11]);
                $prior_stock_move = $mysqli->real_escape_string($client[20]);
                $client_id = $mysqli->real_escape_string($client[24]);

                // Timsestamp
                if (empty($client[21])) {
                    $closed = null;
                } else {
                    $closed = $mysqli->real_escape_string($client[21]);
                } 


                if (($client[26]) =='-') {
                    $agent_id = NULL;
                } else {
                    $agent_id = $mysqli->real_escape_string($client[26]);
                }  

                $wt_active = $mysqli->real_escape_string($client[27]);

                $query = $mysqli->query(
                "INSERT INTO work_tickets(wt_id, wt_created, client_address, prob_type, prob_descr, wt_active, follow_up1, follow_up2, follow_up3, prior_stock_move, closed, client_id, agent_id)  
                 VALUES ('$wt_id', '$wt_created', '$client_address', '$prob_type', '$prob_descr', '$wt_active', '$follow_up1', '$follow_up2', '$follow_up3', '$prior_stock_move', '$closed', '$client_id', '$agent_id')
                 ON DUPLICATE KEY UPDATE 
                    wt_id='$wt_id' , wt_created='$wt_created', client_address = '$client_address', prob_type='$prob_type', prob_descr = '$prob_descr', wt_active = '$wt_active', follow_up1= '$follow_up1', follow_up2 = '$follow_up2', follow_up3 = '$follow_up3', prior_stock_move ='$prior_stock_move', closed = '$closed', client_id = '$client_id', agent_id = '$agent_id'
                ");

            }

            //close opened csv file

            fclose($csvFile);

            $qstring = '?status=succ';
        }else{

            $qstring = '?status=err';
        }
    }else{
        $qstring = '?status=invalid_file';
    }
}

//redirect to the listing page
header("Location: worktickets.php".$qstring);

1 个答案:

答案 0 :(得分:0)

删除SQL语句中$ closed变量中的单引号。

INSERT INTO work_tickets
            (wt_id,
             wt_created,
             closed,
             client_id,
             agent_id)
VALUES      ('$wt_id',
             '$wt_created',
             $closed,
             '$client_id',
             '$agent_id')

稍微聊天后,发现查询无效。如果将完整的SQL语句放入变量中,并在发生故障时回显它,则可以直观地快速确定问题所在。另外,如果您无法发现问题,则可以直接在SQL程序(CLI / MySQL Workbench /其他SQL工具)中运行它。确保列类型与查询为插入生成的内容匹配,也可以使用mysql_error进行协助。尝试通过包装它来再次强制$ closed变量,以查看您的错误是否也不同。为了澄清,这是一个数据问题。