成功提交表单后,浏览器页面变为空白,为什么?

时间:2015-12-04 07:32:04

标签: php forms

我将csv文件导入db,如果发生任何错误,则会显示错误消息。我的要求是,我想在成功提交表单或scv文件导入后显示成功的消息,但浏览器窗口变为空白。请帮帮我。

<?php
    include 'config.php';
    if (isset($_POST['upload'])) {
        $ext = end(explode('.', $_FILES['csvUpload']['name']));
        if( $ext === 'csv' ) {
            $csvfile = fopen($_FILES['csvUpload']['tmp_name'], 'r');
            $lines = explode("\n",file_get_contents($_FILES['csvUpload']['tmp_name']));
            $count = count($lines);
            $i = 0;
            while (!feof($csvfile)) {
                if ($i<($count-1)) {
                    $csv_data[] = fgets($csvfile, 1024);
                    $csv_array = explode(",", $csv_data[$i]);
                    $insert_csv = array();
                    $insert_csv['fname'] = $csv_array[0];
                    $insert_csv['lname'] = $csv_array[1];
                    $query = "INSERT INTO user (fname,lname) VALUES ('".$insert_csv['fname']."','".$insert_csv['lname']."')";
                    $n=mysqli_query($conn, $query);
                    $i++;
                }
            }
            fclose($csvfile);
            $errMsg = array('Data has been imported into database successfully !', 1);
        } else {
            $errMsg = array('Upload a CSV file.', 0);
        }

    }
?>

<html>
    <head>
        <title> Upload CSV File </title>
    </head>
    <body>
        <h2> Upload CSV file : </h2>
        <?php if(isset($errMsg)) {
            $color = $errMsg[1] ? 'green' : 'red';
        } ?>
        <div style="color : <?php if(isset($color)) { echo $color; } ?>; font-size : 15px; margin-bottom : 5px; margin-left : 5px;"><?php if(isset($errMsg[0])) { echo $errMsg[0]; } ?></div>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="csvUpload" /><br/><br/>

            <input type="submit" name="upload" value="Upload" />
        <form>
    </body>
</html>

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个它正在发挥作用。

<?php
include 'config.php';
if (isset($_POST['upload'])) {
    $ext = end(explode('.', $_FILES['csvUpload']['name']));
    if( $ext === 'csv' ) {
        $csvfile = fopen($_FILES['csvUpload']['tmp_name'], 'r');
        $lines = explode("\n",file_get_contents($_FILES['csvUpload']['tmp_name']));
        $count = count($lines);
        $i = 0;
        while (($data = fgetcsv($csvfile, 1000, ",")) !== FALSE) { 
            if ($i<($count-1)) {

                //$csv_array = explode(",", $csv_data[$i]);
                $insert_csv = array();
                $insert_csv['fname'] = $data[0];
                $insert_csv['lname'] = $data[1];
                 $query = "INSERT INTO user (fname,lname) VALUES ('".$insert_csv['fname']."','".$insert_csv['lname']."')";
                $n=mysqli_query($conn, $query);

                $i++;
            }
        }

        fclose($csvfile);
        $errMsg = array('Data has been imported into database successfully !', 1);
       // print_r($errMsg);exit;
    } else {
        $errMsg = array('Upload a CSV file.', 0);
    }

}