Jump to content

Recommended Posts

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 by moosey_man1988
Link to comment
https://forums.phpfreaks.com/topic/296640-need-a-details-upload-page/
Share on other sites

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?

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 by moosey_man1988

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 by moosey_man1988

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.  :happy-04:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.