moosey_man1988 Posted June 4, 2015 Share Posted June 4, 2015 (edited) Hi There I'm trying to create an upload csv page which detects duplicates and tells me how many are duplicates, but i just cant my head around it, this is my code so far //Upload File if (isset($_POST['submit'])) { if (is_uploaded_file($_FILES['fileName']['tmp_name'])) { echo "<h1>" . "File ". $_FILES['fileName']['name'] ." uploaded successfully." . "</h1>"; //echo "<h2>Displaying contents:</h2>"; //readfile($_FILES['fileName']['tmp_name']); $upload_amount = 0; $duplicates = 0; } //Import uploaded file to Database $handle = fopen($_FILES['fileName']['tmp_name'], "r"); $filename = $_FILES['fileName']['name']; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $import="INSERT IGNORE into Leads(number,date,used,fileName) values('$data[0]','$data[1]','$used','$filename')"; $query= mysql_query($import); //mysql_query($import) or die(mysql_error()); if ($query){ if (mysql_affected_rows() > 0){ $upload_amount++; } else { $duplicates++; } } else { die (mysql_error()); } } if ($upload_amount > 1){ $todays_date= date ('Y-m-d H:i:s'); $sql_status="INSERT INTO LeadsStatus (filename,imported,user,Date) VALUES ('$filename','$upload_amount','$login_session','$todays_date')"; mysql_query($sql_status) or die (mysql_error()); } fclose($handle); print "Import done </br>"; print $upload_amount." Imported"; echo "</br>"; print $duplicates." Duplicates Not Imported"; print "<a href='Leads.php'> Upload More</a>"; any help is greatly appreciated thank you Edited June 4, 2015 by moosey_man1988 Quote Link to comment https://forums.phpfreaks.com/topic/296640-need-a-details-upload-page/ Share on other sites More sharing options...
ginerjm Posted June 4, 2015 Share Posted June 4, 2015 Somewhat rudimentary but seems like it should do what you want to do - if I understand your question. What do you see as the problem? ps - stop using the MySQL_* functions. Simply read the manual and notice all the warnings about them. Really. Also - where is $used defined? Is error checking turned on so you will catch errors while developing this? Quote Link to comment https://forums.phpfreaks.com/topic/296640-need-a-details-upload-page/#findComment-1513163 Share on other sites More sharing options...
moosey_man1988 Posted June 4, 2015 Author Share Posted June 4, 2015 (edited) apologies $used is defined as 0 above, so if I import 300 rows from a CSV, it will say 300 imported, 0 duplicates because i told it to echo $upload_amount and $duplicates, but when i upload the same again it doesn't say 300 duplicates what would i use rather than mysql_* functions? Edited June 4, 2015 by moosey_man1988 Quote Link to comment https://forums.phpfreaks.com/topic/296640-need-a-details-upload-page/#findComment-1513181 Share on other sites More sharing options...
cyberRobot Posted June 4, 2015 Share Posted June 4, 2015 what would i use rather than mysql_* functions? You could use PDO or MySQL Improved (aka MySQLi). More information about choosing an API (and examples) can be found here: http://php.net/manual/en/mysqlinfo.api.choosing.php Quote Link to comment https://forums.phpfreaks.com/topic/296640-need-a-details-upload-page/#findComment-1513186 Share on other sites More sharing options...
moosey_man1988 Posted June 4, 2015 Author Share Posted June 4, 2015 (edited) ah sorry I have mysqli functions also, are they much better or just updated? what actually are the benefits? oh also just to add to that, it was the double else stopping it from telling me how many dups. Edited June 4, 2015 by moosey_man1988 Quote Link to comment https://forums.phpfreaks.com/topic/296640-need-a-details-upload-page/#findComment-1513189 Share on other sites More sharing options...
cyberRobot Posted June 4, 2015 Share Posted June 4, 2015 ah sorry I have mysqli functions also, are they much better or just updated? what actually are the benefits? For what it's worth, the link I provided earlier has a "Feature comparison" section for the three APIs (MySQL, PDO, and MySQLi). The main reason for switching is that the old MySQL API has been deprecated. And you'll want to be using one of the newer APIs before MySQL is removed from PHP. Of course, who knows when that's going to happen. But it's better to upgrade at your own pace then rush it when you are forced to switch. The other benefit of using PDO or MySQLi is that you can use Prepared Statements. A quick Google search should give you an idea of what those are. Quote Link to comment https://forums.phpfreaks.com/topic/296640-need-a-details-upload-page/#findComment-1513190 Share on other sites More sharing options...
moosey_man1988 Posted June 4, 2015 Author Share Posted June 4, 2015 Ah thanks for the advice! I now have a new issue, I need to make sure there is a filename and to make sure that the file is a CSV otherwise, It gets stuck in an infinate loop :| Quote Link to comment https://forums.phpfreaks.com/topic/296640-need-a-details-upload-page/#findComment-1513191 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.