I have written the following script to upload the given csv into a table of the following structure
id | prod_id | att_cid | att_sid | att_count
The main issue is I need to provide the logic that will error out and print an error mssg is someone tries to upload 2 rows with the same combination of prod_id,att_cid and att_sid. Any help on this will be greatly appreciated. Thanks
Sulen
$uploaddir = "csv";
$allowed_ext = "csv,xls";
$max_size = "10000000";
$extension = pathinfo($_FILES['file']['
name']);
$extension = $extension[extension];
$allowed_paths = explode(",", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}
if ($ok == "1") {
if (is_uploaded_file($_FILES[
'file']['t
mp_name'])
) {
move_uploaded_file($_FILES
['file']['
tmp_name']
,$uploaddi
r.'/'.$_FI
LES['file'
]['name'])
;
$filename=$_FILES['file'][
'name'];
chmod($uploaddir.'/'.$file
name, 0755);
$handle = fopen ($uploaddir.'/'.$filename,
"r");
$flag = 0;
while ($data = fgetcsv ($handle, 1000, ",")) {
$flag++;
if ($flag==1) continue;
if ($data[4]=='') {
$prodsql = "SELECT prod_id FROM products WHERE prod_sku='{$data[0]}'";
$prodresult = mysql_query($prodsql) or die("Invalid query: " . mysql_error().__LINE__.__F
ILE__);
$prodnum = mysql_num_rows($prodresult
);
if ($prodnum == 0) { ?>
<table width="700" border="0" cellspacing="0" cellpadding="2">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="6" id="admin">
<tr>
<td width="6%" align="center"><img src="images/warning_icon.g
if" width="35" height="35"></td>
<td width="94%" class="system_msg">The product SKU <? echo $data[0]; ?> is INVALID.</td>
</tr>
</table>
</td>
</tr>
</table>
<br />
<?
} else {
while ($prodrs = mysql_fetch_assoc($prodres
ult)) {
$acsql = "SELECT att_cid FROM attribute_categories WHERE att_cname='{$data[1]}'";
$acresult = mysql_query($acsql) or die("Invalid query: " . mysql_error().__LINE__.__F
ILE__);
while ($attcrs = mysql_fetch_assoc($acresul
t)) {
$assql = "SELECT att_sid FROM attribute_subcat WHERE att_sname='{$data[2]}'";
$asresult = mysql_query($assql) or die("Invalid query: " . mysql_error().__LINE__.__F
ILE__);
while ($attsrs = mysql_fetch_assoc($asresul
t)) {
$sql = "UPDATE prod_attributes SET att_count = att_count + $data[3] WHERE prod_id='".$prodrs['prod_i
d']."' AND att_cid='".$attcrs['att_ci
d']."' AND att_sid='".$attsrs['att_si
d']."'";
$result = mysql_query($sql) or die("Invalid query: " . mysql_error().__LINE__.__F
ILE__);
}
}
}
}
}