Hi all i am getting error in the following code to upload files csv and txt files
i have to insert the following values only into database if firstname,lastname and email id is null it should not accept the data and skip that row. And for csv file if encounters headings it should skip that row
the fields are id,firstname,lastname,address,city,state,zip,email ,telephone number
in these above fields firstname,lastname and email should not be null. I wrote the following code the rows with empty values are also getting uploaded. please help me
If any file that doesnt contain any of the above fields it should throw an error
<?php
if(isset($_GET['module']) && !empty($_GET['module']))
{
$module = $_GET['module'];
switch ($module)
{
case 'upload' :
{
$target_path = '';
$csvfilename=$_FILES['userfile']['name'];
$ext = substr(strrchr($csvfilename, '.\.'), 1);
$userid = $_COOKIE["TestCookie"];
if($ext == "csv")
{
$target_path = "temp/". basename( $_FILES['userfile']['name']);
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['userfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
# $skipfirstline = true;
$delimiter = ',';
$handle = fopen($target_path, "r");
if(!$handle) die("unable to find file");
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
//foreach (array_keys($data) as $v)
//$data[$v] = mysql_real_escape_string($data[$v]);
foreach($data as $values)
{
$values=implode(",",$data);
$value=explode(",",$values);
$a=$value[1];
$b=$value[2];
$c=$value[3];
$d=$value[4];
$e=$value[5];
$f=$value[6];
$g=$value[7];
$h=$value[8];
}
$uploadqry = "insert into ecoup_coupon_subscribers(user_id,firstname,lastnam e,subscriber_address,city,state,zip, email,telephone) values(".$userid.", '" . $a." ','" . $b." ','" . $c." ','" . $d." ','" . $e." ','" . $f." ','" . $g." ','" . $h." ')";
$string= $db->Execute($uploadqry);
if($string===false)
$_SESSION['errMessage'] = "upload failed";
}
fclose($handle);
}elseif($ext == "txt")
{
$target_path = "temp/". basename( $_FILES['userfile']['name']);
//$target_path = $target_path . basename( $_FILES['userfile']['name']);
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['userfile']['name']). " has been uploaded and emails imported successfully";
} else{
echo "There was an error uploading the file, please try again!";
}
$fcontents = file ($target_path);
$arr = explode(',',$fcontents);
$tmp = array();
$j = 0;
$k = 0;
foreach($fcontents as $k=>$v)
{
$tmp = explode(",",$v);
#echo "<br><br>";
echo $tmp[0]." ";
if ( ($tmp[1] != "") && ($tmp[2] != "") && ($tmp[7] != "") )
{
# echo "valid row";
$uplsubscriber = "insert into ecoup_coupon_subscribers(user_id,firstname,lastnam e,subscriber_address,city,state,zip, email,telephone) values(".$userid.",'".$tmp[1]."','".$tmp[2]."','".$tmp[3]."','".$tmp[4]."','".$tmp[5]."','".$tmp[6]."','".$tmp[7]."','".$tmp[8]."')";
#echo $uploadsubscriber;
#die;
$result = $db->Execute($uplsubscriber);
}
else
{
#echo "Invalid row";
/* $uplsubscriber = "insert into ecoup_coupon_subscribers(user_id,firstname,lastnam e,subscriber_address,city,state,zip, email,telephone) values(".$userid.",'".$tmp[1]."','".$tmp[2]."','".$tmp[3]."','".$tmp[4]."','".$tmp[5]."','".$tmp[6]."','".$arr[7]."','".$tmp[8]."')";
echo $uploadsubscriber;
#die;
$result = $db->Execute($uplsubscriber);*/
}
}
for($i=0; $i<sizeof($fcontents); $i++)
{
$line = trim($fcontents[$i]);
#echo $line;
$arr = explode(",",$line);
foreach ( $arr as $row )
{
if( isset($arr[1]) && $arr[1] != '' && isset($arr[2]) && $arr[2] != '' && isset($arr[7]) && $arr[7] != '')
{
//replace double quotes with empty space
$str = str_replace(" ", "", htmlspecialchars($row[$field_count]));
#echo $str;
#$xtpl->assign("CELL",$str);
#$xtpl->parse("main.table.row.cell");
}
}
$a=trim($arr[1]);
$b=trim($arr[2]);
$c=trim($arr[3]);
$d=trim($arr[4]);
$e=trim($arr[5]);
$f=trim($arr[6]);
$g=trim($arr[7]);
$h=trim($arr[8]);
# print $a;
$uplsubscriber = "insert into ecoup_coupon_subscribers(user_id,firstname,lastnam e,subscriber_address,city,state,zip, email,telephone) values(".$userid.",'".trim($arr[1])."','".trim($arr[2])."','".trim($arr[3])."','".trim($arr[4])."','".trim($arr[5])."','".trim($arr[6])."','".trim($arr[7])."','".trim($arr[8])."')";
$result = $db->Execute($uplsubscriber);
if($result===false)
$_SESSION['errMessage'] = "upload failed";
}
}
$_SESSION['errMessage' ] = 'List uploaded successfully';
unlink($target_path);
}
}
}
function assign ($name,$val="") {
if (gettype($name)=="array")
while (list($k,$v)=each($name))
$this->VARS[$k]=$v;
else
$this->VARS[$name]=$val;
}
?>
<form enctype="multipart/form-data" name="Import" method="POST" action="welcomeuser.php?action=uploadsubscribers&m odule=upload">
<table width="100%" border="0" cellspacing="1" cellpadding="2">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="6" class="tabForm">
<tr class="mangtableyellow">
<th align="left" class="dataLabel" colspan="4">SELECT FILE TO IMPORT</th>
</tr>
<tr>
<td class="mangtablewhite">
<input type="hidden" />
<input size="60" name="userfile" type="file"/>
</td>
</tr>
<tr class="mangtablewhite">
<td align="left"><input title="IMPORT" accessKey="" class="button" type="submit" name="button" value="Import" onclick="return verify_data(this.form);"></td>
</tr>
</table> </td></table>
</form>
<table> <tr>
<td width="4%"><img src="images/mouse_icon.gif" width="15" height="19"></td>
<td width="96%" class="text4">Please Upload CSV,TXT files only. </td>
</tr>
</table>
Please correct my code and send me. Please help me