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,lastn ame 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,la stname,address, city,state,zip, email,telephone number
in these above fields firstname,lastn ame 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_uploade d_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_p ath, "r");
if(!$handle) die("unable to find file");
while (($data = fgetcsv($handle , 1000, $delimiter)) !== FALSE)
{
//foreach (array_keys($da ta) as $v)
//$data[$v] = mysql_real_esca pe_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_su bscribers(user_ id,firstname,la stname,subscrib er_address,city ,state,zip, email,telephone ) values(".$useri d.", '" . $a." ','" . $b." ','" . $c." ','" . $d." ','" . $e." ','" . $f." ','" . $g." ','" . $h." ')";
$string= $db->Execute($uploa dqry);
if($string===fa lse)
$_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_uploade d_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(',',$fc ontents);
$tmp = array();
$j = 0;
$k = 0;
foreach($fconte nts 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_su bscribers(user_ id,firstname,la stname,subscrib er_address,city ,state,zip, email,telephone ) values(".$useri d.",'".$tmp[1]."','".$tmp[2]."','".$tmp[3]."','".$tmp[4]."','".$tmp[5]."','".$tmp[6]."','".$tmp[7]."','".$tmp[8]."')";
#echo $uploadsubscrib er;
#die;
$result = $db->Execute($uplsu bscriber);
}
else
{
#echo "Invalid row";
/* $uplsubscriber = "insert into ecoup_coupon_su bscribers(user_ id,firstname,la stname,subscrib er_address,city ,state,zip, email,telephone ) values(".$useri d.",'".$tmp[1]."','".$tmp[2]."','".$tmp[3]."','".$tmp[4]."','".$tmp[5]."','".$tmp[6]."','".$arr[7]."','".$tmp[8]."')";
echo $uploadsubscrib er;
#die;
$result = $db->Execute($uplsu bscriber);*/
}
}
for($i=0; $i<sizeof($fcon tents); $i++)
{
$line = trim($fcontents[$i]);
#echo $line;
$arr = explode(",",$li ne);
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(" ", "", htmlspecialchar s($row[$field_count]));
#echo $str;
#$xtpl->assign("CELL", $str);
#$xtpl->parse("main.ta ble.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_su bscribers(user_ id,firstname,la stname,subscrib er_address,city ,state,zip, email,telephone ) values(".$useri d.",'".trim($ar r[1])."','".trim($a rr[2])."','".trim($a rr[3])."','".trim($a rr[4])."','".trim($a rr[5])."','".trim($a rr[6])."','".trim($a rr[7])."','".trim($a rr[8])."')";
$result = $db->Execute($uplsu bscriber);
if($result===fa lse)
$_SESSION['errMessage'] = "upload failed";
}
}
$_SESSION['errMessage' ] = 'List uploaded successfully';
unlink($target_ path);
}
}
}
function assign ($name,$val="") {
if (gettype($name) =="array")
while (list($k,$v)=ea ch($name))
$this->VARS[$k]=$v;
else
$this->VARS[$name]=$val;
}
?>
<form enctype="multip art/form-data" name="Import" method="POST" action="welcome user.php?action =uploadsubscrib ers&module=uplo ad">
<table width="100%" border="0" cellspacing="1" cellpadding="2" >
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="6" class="tabForm" >
<tr class="mangtabl eyellow">
<th align="left" class="dataLabe l" colspan="4">SEL ECT FILE TO IMPORT</th>
</tr>
<tr>
<td class="mangtabl ewhite">
<input type="hidden" />
<input size="60" name="userfile" type="file"/>
</td>
</tr>
<tr class="mangtabl ewhite">
<td align="left"><i nput title="IMPORT" accessKey="" class="button" type="submit" name="button" value="Import" onclick="return verify_data(thi s.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">P lease Upload CSV,TXT files only. </td>
</tr>
</table>
Please correct my code and send me. Please help me
i have to insert the following values only into database if firstname,lastn ame 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,la stname,address, city,state,zip, email,telephone number
in these above fields firstname,lastn ame 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_uploade d_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_p ath, "r");
if(!$handle) die("unable to find file");
while (($data = fgetcsv($handle , 1000, $delimiter)) !== FALSE)
{
//foreach (array_keys($da ta) as $v)
//$data[$v] = mysql_real_esca pe_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_su bscribers(user_ id,firstname,la stname,subscrib er_address,city ,state,zip, email,telephone ) values(".$useri d.", '" . $a." ','" . $b." ','" . $c." ','" . $d." ','" . $e." ','" . $f." ','" . $g." ','" . $h." ')";
$string= $db->Execute($uploa dqry);
if($string===fa lse)
$_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_uploade d_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(',',$fc ontents);
$tmp = array();
$j = 0;
$k = 0;
foreach($fconte nts 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_su bscribers(user_ id,firstname,la stname,subscrib er_address,city ,state,zip, email,telephone ) values(".$useri d.",'".$tmp[1]."','".$tmp[2]."','".$tmp[3]."','".$tmp[4]."','".$tmp[5]."','".$tmp[6]."','".$tmp[7]."','".$tmp[8]."')";
#echo $uploadsubscrib er;
#die;
$result = $db->Execute($uplsu bscriber);
}
else
{
#echo "Invalid row";
/* $uplsubscriber = "insert into ecoup_coupon_su bscribers(user_ id,firstname,la stname,subscrib er_address,city ,state,zip, email,telephone ) values(".$useri d.",'".$tmp[1]."','".$tmp[2]."','".$tmp[3]."','".$tmp[4]."','".$tmp[5]."','".$tmp[6]."','".$arr[7]."','".$tmp[8]."')";
echo $uploadsubscrib er;
#die;
$result = $db->Execute($uplsu bscriber);*/
}
}
for($i=0; $i<sizeof($fcon tents); $i++)
{
$line = trim($fcontents[$i]);
#echo $line;
$arr = explode(",",$li ne);
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(" ", "", htmlspecialchar s($row[$field_count]));
#echo $str;
#$xtpl->assign("CELL", $str);
#$xtpl->parse("main.ta ble.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_su bscribers(user_ id,firstname,la stname,subscrib er_address,city ,state,zip, email,telephone ) values(".$useri d.",'".trim($ar r[1])."','".trim($a rr[2])."','".trim($a rr[3])."','".trim($a rr[4])."','".trim($a rr[5])."','".trim($a rr[6])."','".trim($a rr[7])."','".trim($a rr[8])."')";
$result = $db->Execute($uplsu bscriber);
if($result===fa lse)
$_SESSION['errMessage'] = "upload failed";
}
}
$_SESSION['errMessage' ] = 'List uploaded successfully';
unlink($target_ path);
}
}
}
function assign ($name,$val="") {
if (gettype($name) =="array")
while (list($k,$v)=ea ch($name))
$this->VARS[$k]=$v;
else
$this->VARS[$name]=$val;
}
?>
<form enctype="multip art/form-data" name="Import" method="POST" action="welcome user.php?action =uploadsubscrib ers&module=uplo ad">
<table width="100%" border="0" cellspacing="1" cellpadding="2" >
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="6" class="tabForm" >
<tr class="mangtabl eyellow">
<th align="left" class="dataLabe l" colspan="4">SEL ECT FILE TO IMPORT</th>
</tr>
<tr>
<td class="mangtabl ewhite">
<input type="hidden" />
<input size="60" name="userfile" type="file"/>
</td>
</tr>
<tr class="mangtabl ewhite">
<td align="left"><i nput title="IMPORT" accessKey="" class="button" type="submit" name="button" value="Import" onclick="return verify_data(thi s.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">P lease Upload CSV,TXT files only. </td>
</tr>
</table>
Please correct my code and send me. Please help me
Comment