Hi all
please find my following code. I am able to upload csv and text file containg csv file with header and txt file. What i want now is if the uploaded csv file is not containing header it should ask for check the header warning. Please see the code
<?php
// connect to DB
$host_name = "localhost";
$db_user = "root";
$db_password = "sachi";
$db_name = "fundgroup";
$table = 'ecoup_coupon_subscribers';
switch ($_POST['act']) {
case 'insert':
$db = mysql_connect("$host_name","$db_user","$db_password") or die("Could not connect to database!");
$result = mysql_select_db($db_name) or die("Could not select database!");
// open file
$fp = fopen($_FILES['userfile']['tmp_name'], 'r') or die('Could not open file!');
$x=0;
while ($data = fgetcsv($fp, 1024)) {
$sql_ = "SELECT user_id FROM $table WHERE user_id=$data[0] ";
$result_=mysql_query($sql_, $db) or die("This selected file Could not contain required fields! Please Select the file again");
$id_=mysql_fetch_row($result_);
if($id_[0]!="") {
// update record in case id was already existed/
$update = "UPDATE $table SET firstname='$data[1]', lastname='$data[2]',subscriber_address='$data[3]',city='$data[4]',state='$data[5]',zip='$data[6]',email='$data[7]',telephone='$data[8]' WHERE user_id=$data[0] ";
$res_update = mysql_query($update, $db) or die("Could not update DB!");
echo "Row #".$num." was updated!<p>";
$num++;
}
else
{
if($x>0){
if($data[1]!="" && $data[2]!="" && $data[7]!=""){
// insert new record into database
$insert = "INSERT INTO $table (user_id, firstname, lastname,subscriber_address,city,state,zip,email,telephone) VALUES( '$data[0]', '$data[1]' , '$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]') ";
$res_insert = mysql_query($insert, $db) or die("Could not INSERT query!");
echo "Row #".$num." was inserted!<p>";
$num++;
}
}
$x++;
}
}
fclose($fp);
break;
default:
echo <<<EOD
<center>
<h3>CSV file upload form</h3>
<form enctype="multipart/form-data" method="POST">
<input type="hidden" name="act" value="insert">
CSV file <input name="userfile" type="file">
<input type="submit" value="Submit">
</form>
</center>
EOD;
break;
}
?>