function upload_excel1 (){
global $conn;
$filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
ini_set("auto_detect_line_endings", true);
$file = fopen($filename, "r");
$flag = true;
$row = 1;
while (($getData = fgetcsv($file, 10000, ";")) !== FALSE)
{
if($flag) { $flag = false; continue; }
$sql = "INSERT into analisi (id, id_profili, id_orizzonte, rap_prova,campione,scheletro,sabbia, limo, argilla, tessitura, reazione, ec12, calcare_tot, calcare_att, sostanza_org, azoto_tot, fosforo_p, calcio_mg, magnesio_mg, potassio_mg, sodio_mg, csc_meq, calcio_meq, magnesio_meq, potassio_meq, sodio_meq, saturazione_bas, rapporto_mgk, rapporto_cak, rapporto_camg)
values (DEFAULT,'".pg_escape_string($getData[0])."','".pg_escape_string(($getData[1]))."','".pg_escape_string(($getData[2]))."','".pg_escape_string(($getData[3]))."','".pg_escape_string(($getData[4]))."','".pg_escape_string(($getData[5]))."','".pg_escape_string(($getData[6]))."','".pg_escape_string(($getData[7]))."','".pg_escape_string(($getData[8]))."','".pg_escape_string(($getData[9]))."','".pg_escape_string(($getData[10]))."','".pg_escape_string(($getData[11]))."','".pg_escape_string(($getData[12]))."','".pg_escape_string(($getData[13]))."','".pg_escape_string(($getData[14]))."','".pg_escape_string(($getData[15]))."','".pg_escape_string(($getData[16]))."','".pg_escape_string(($getData[17]))."','".pg_escape_string(($getData[18]))."','".pg_escape_string(($getData[19]))."','".pg_escape_string(($getData[20]))."','".pg_escape_string(($getData[21]))."','".pg_escape_string(($getData[22]))."','".pg_escape_string(($getData[23]))."','".pg_escape_string(($getData[24]))."','".pg_escape_string(($getData[25]))."','".pg_escape_string(($getData[26]))."','".pg_escape_string(($getData[27]))."','".pg_escape_string(($getData[28]))."')";
$stmt = $conn->prepare($sql);
$stmt->execute();
}
if(!isset($stmt))
{
echo "<script type=\"text/javascript\">
alert(\"Invalid File:Please Upload CSV File.\");
window.location = \"index.php\"
</script>";
}
else {
echo "<script type=\"text/javascript\">
alert(\"CSV File has been successfully Imported.\");
window.location = \"index.php\"
</script>";
}
fclose($file);
ini_set("auto_detect_line_endings", false);
}
}
Hello i am using this script to load a csv file, it works perfectly, except in case the numeric fields turn out not filled. How can I enter a NULL value for the unfilled fields of the file?
Now it's work :)
if(!empty($getData[15])) { $getData[28]= "'$getData[28]'"; }else{ $getData[28]= 'NULL';}
$sql = "INSERT into analisi (id, id_profili, id_orizzonte, rap_prova,campione,scheletro,sabbia, limo, argilla, tessitura, reazione, ec12, calcare_tot, calcare_att, sostanza_org, azoto_tot, fosforo_p, calcio_mg, magnesio_mg, potassio_mg, sodio_mg, csc_meq, calcio_meq, magnesio_meq, potassio_meq, sodio_meq, saturazione_bas, rapporto_mgk, rapporto_cak, rapporto_camg)
values (DEFAULT,$getData[0],$getData[1],$getData[2],$getData[3],$getData[4],$getData[5],$getData[6],$getData[7],$getData[8],$getData[9],$getData[10],$getData[11],$getData[12],$getData[13],$getData[14],$getData[15],$getData[16],$getData[17],$getData[18],$getData[19],$getData[20],$getData[21],$getData[22],$getData[23],$getData[24],$getData[25],$getData[26],$getData[27],$getData[28])";
Correct me if I'm wrong, but I think empty column should return an empty string when reading a csv file, in that case you can use method in PHP like isset or empty and if you want to assign the null value just use a one liner if statement like
if $getData[0] is empty then return null else return $getData[0]
empty($getData[0]) ? null : pg_escape_string($getData[0])
or
isset($getData[0]) ? null : pg_escape_string($getData[0])
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.