Please help me improve my code. I have this html form to select a .csv file extension only, to upload it in my sql database. How can I improve this? like make the button not clickable if there is no file chosen.
<form class="ui input" enctype="multipart/form-data" method = "POST" role = "form">
<input type = "file" name ="file" id="file" size = "150">
<button class="ui small red button" type = "submit" class = "btn btn-default" name ="submit" value = "submit">Upload CSV</button>
</form>
Then this is my php code to upload the csv, How can I make a popup message saying "your file is uploading please wait" and "finished uploading". And my problem here is that the headers of my csv file is also uploaded how can I exclude the first row of my csv?
<?php
if(isset($_POST['submit'])) {
$host = 'localhost';
$user = 'root';
$password = '';
$db = 'jeremy_db';
$con = mysqli_connect($host,$user,$password) or die('Could not' .mysqli_error($con));
mysqli_select_db($con, $db) or die ('Could not' .mysqli_error($con));
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
while(($csvdata = fgetcsv($handle,1000,","))!== FALSE){
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$sql = "INSERT INTO jeremy_table_trend (sha1,vsdt,trendx) VALUES ('$sha1','$vsdt','$trendx')";
$query = mysqli_query($con , $sql);
$c = $c+1;
}
if($query){
echo "SABRE";
}
else {
echo "SLAM";
}
}
?>
I'm using php 7 guys, any suggestions?
Initially you can keep the submit button disable. Then oclick to input file you can call a js function which will validate file extension and will enable the submit button accordingly.
For showing message - you can write a javascript function onclick to submit button.
For processing big file on fly this is not a good practice. It will be timed out to execute all queries(number of queries = number of rows in csv). You can do this by following below steps -
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.