我想使用php将数据上传到我的数据库,但是得到这些错误注意:未定义的偏移量:4

时间:2016-11-07 06:12:30

标签: php mysql

注意:未定义的偏移量:第36行的C:\ xampp \ htdocs \ Upload_and_import.php中的4

注意:未定义的偏移量:第37行的C:\ xampp \ htdocs \ Upload_and_import.php中的5

注意:未定义的偏移量:第38行的C:\ xampp \ htdocs \ Upload_and_import.php中的6

注意:未定义的偏移量:第39行的C:\ xampp \ htdocs \ Upload_and_import.php中的7

注意:未定义的偏移量:第40行的C:\ xampp \ htdocs \ Upload_and_import.php中的8

这是我的代码

<?php
$connect = mysqli_connect('localhost','root','','idea'); // First paramater stands for host, Second for Database-user, Third stand for Database-password, Forth Database-name.

if (!$connect) { //Connection is possible using above setting or not
 die('Could not connect to MySQL: ' . mysqli_error());
}

$class="";
$message='';
$error=0;
$target_dir = dirname(__FILE__)."/Uploads/";
if(isset($_POST["import"]) && !empty($_FILES)) {
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $fileType = pathinfo($target_file,PATHINFO_EXTENSION);
    if($fileType != "csv")  // here we are checking for the file extension. We are not allowing othre then (.csv) extension .
    {
        $message .= "Sorry, only CSV file is allowed.<br>";
        $error=1;
    }
    else
    {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            $message .="File uplaoded successfully.<br>";

            if (($getdata = fopen($target_file, "r")) !== FALSE) {
               fgetcsv($getdata);   
               while (($data = fgetcsv($getdata)) !== FALSE) {
                    $fieldCount = count($data);
                    for ($c=0; $c < $fieldCount; $c++) {
                      $columnData[$c] = $data[$c];
                    }
             $Site_Name_A = mysqli_real_escape_string($connect ,$columnData[0]);
             $Site_ID_A = mysqli_real_escape_string($connect ,$columnData[1]);
             $Site_Name_B = mysqli_real_escape_string($connect ,$columnData[2]);
             $Site_ID_B = mysqli_real_escape_string($connect ,$columnData[3]);
             $Frequency = mysqli_real_escape_string($connect ,$columnData[4]);
             $Hardware = mysqli_real_escape_string($connect ,$columnData[5]);
             $Status = mysqli_real_escape_string($connect ,$columnData[6]);
             $Site_Engg = mysqli_real_escape_string($connect ,$columnData[7]);
             $Remarks = mysqli_real_escape_string($connect ,$columnData[8]);
             $import_data[]="('".$Site_Name_A."','".$Site_ID_A."','".$Site_Name_B."','".$Site_ID_B."','".$Frequency."','".$Hardware."','".$Status."','".$Site_Engg."','".$Remarks."')";
            // SQL Query to insert data into DataBase

             }
             $import_data = implode(",", $import_data);
             $query = "INSERT INTO idean(Site_Name_A,Site_ID_A,Site_Name_B,Site_ID_B,Frequency,Hardware,Status,Site_Engg,Remarks) VALUES  $import_data ;";
             $result = mysqli_query($connect ,$query);
             $message .="Data imported successfully.";
             fclose($getdata);
            }

        } else {
            $message .="Sorry, there was an error uploading your file.";
            $error=1;
        }
    }
}
$class="warning";
if($error!=1)
{
    $class="success";
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
</head>
<body>

<div class="container" style="margin-top:20px; margin-bottom:20px;padding:10px;">
<?php
    if(!empty($message))
    {
?>
<div class="btn-<?php echo $class;?>" style="width:30%;padding:10px;margin-bottom:20px;">
<?php
        echo $message;

 ?>
</div>
<?php } ?>

<form role="form" action="<?php echo $_SERVER['REQUEST_URI'];?>" method="post" enctype="multipart/form-data">
<fieldset class="form-group">
    <div class="form-group">
    <input type="file" name="fileToUpload" id="fileToUpload">
    <label for="image upload" class="control-label">Only .csv file is allowed. </label>
    </div>
    <div class="form-group">
    <input type="submit" class="btn btn-warning" value="Import Data" name="import">
    </div>
    </fieldset>
</form>
</div>
</body>
</html>

0 个答案:

没有答案