gpt4 book ai didi

php - 如果 csv 值为 null,则插入 null 或其他内容

转载 作者:行者123 更新时间:2023-11-29 18:14:24 25 4
gpt4 key购买 nike

我在将 csv 上传到 mysql 时遇到问题。

我有以下 csv。我需要上传标记的字段,csv 可以包含的最大字段为 48,但有些 csv 仅包含 36 个或更少。我有一个表单,如果 csv 包含 48 个单元格,则会成功上传到 mysql,但如果 csv 小于 48,我会收到错误。

这是我的表格:

<div class="container">
<?php
if(isset($_POST['uploadBtn'])){
$fileName=$_FILES['myFile']['name'];
$fileTmpName=$_FILES['myFile']['tmp_name'];

$fileExtension=pathinfo($fileName,PATHINFO_EXTENSION);

$allowedType = array('csv');
if(!in_array($fileExtension,$allowedType)){?>

<div class="alert alert-danger">INVALID FILE</div>
<?php
}else{
$handle = fopen($fileTmpName, 'r');
$k = 0;
$hardness = array ();
while (($myData = fgetcsv($handle,1000,',')) !== FALSE) {
$k++;
if ( $k > 13 ) {
$hardness[] = $myData[3];
}
}

$query = "INSERT INTO metlab.resultados_dureza_junta
(a_od_r1, a_od_r2, a_od_r3, a_mod_r1, a_mod_r2,
a_mod_r3, a_mid_r1, a_mid_r2, a_mid_r3, a_id_r1,
a_id_r2, a_id_r3,b_od_r1,
b_od_r2, b_od_r3, b_mod_r1,
b_mod_r2, b_mod_r3, b_mid_r1, b_mid_r2, b_mid_r3,
b_id_r1, b_id_r2, b_id_r3,
c_od_r1, c_od_r2, c_od_r3, c_mod_r1, c_mod_r2, c_mod_r3,
c_mid_r1, c_mid_r2, c_mid_r3, c_id_r1, c_id_r2, c_id_r3,
d_od_r1)
VALUES ($hardness[0],$hardness[1],$hardness[2],$hardness[3],
$hardness[4],$hardness[5],$hardness[6],$hardness[7],
$hardness[8], $hardness[9],$hardness[10],$hardness[11],
$hardness[12],$hardness[13],$hardness[14],$hardness[15],
$hardness[16],$hardness[17], $hardness[18], $hardness[19],
$hardness[20], $hardness[21],$hardness[22],$hardness[23],
$hardness[24],$hardness[25],$hardness[26],$hardness[27],
$hardness[28],$hardness[29],$hardness[30],$hardness[31],
$hardness[32],$hardness[33],$hardness[34],$hardness[35],
IFNULL($hardness[36],DEFAULT(0)))";
//ON $hardness[36],DEFAULT I was trying to insert something so I can solve the problem but I cant.

var_dump($hardness);
$run = mysql_query($query);

if(!$run){
die("error in uploading file".mysql_error());
}else{ ?>
<div class="alert alert-success">SUCCESS</div>
<?php
}
}
}
?>

<form action="" method="post" enctype="multipart/form-data">
<h3 class="text-center">
RESULTS
</h3></hr>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="file" name="myFile" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="submit" name ="uploadBtn" class="btn btn-info">
</div>
</div>
</div>
</form>

CSV 示例:

csv example

错误:

error

最佳答案

我只是尝试用 null 填充您的 $myData

$myData = array_pad( $myData, 48, null );

实际上,您应该改进 SQL 查询以不处理丢失的数据,或者检查默认的 null 值。

关于php - 如果 csv 值为 null,则插入 null 或其他内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47146986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com