Jump to content

CSV Import Page


SalientAnimal

Recommended Posts

This can be an easy question, a hard one, or something in the middle. All depending upon how the data in said CSV looks like.

For most cases it's not as simple as just dumping the data directly into the database, as you'd end up with a lot of duplicated data in your database. So what I recommend you to do first, is to read up on database normalization. The video below is a great starting point, to show the principle behind normalization.

 

Also, to get the CSV data from the file, you'll need the fgetscv () function.

 

Link to comment
https://forums.phpfreaks.com/topic/273229-csv-import-page/#findComment-1406065
Share on other sites

That's what escaping is for, yes.

 

SalientAnimal: I just noticed that you have posted this in the "PHP coding help" section. It would have been better suited for the "Application design" section, as this section is for getting help with code you have written (yourself). Not how to plan the design of the code you are going to write. ;)

Link to comment
https://forums.phpfreaks.com/topic/273229-csv-import-page/#findComment-1406076
Share on other sites

Ok, well I eventually managed to find some thing that did the trick :-). Yes I am guild of the cardinal sin of copy and pasting code without actually fully understanding it. The problem I have not, and what I want to add to this code is:

 

1. Ignore the first line of the csv file (i.e. Headings)

2. On the success page, display the total line written to the database.

 

The Code:

(FORM)

<html>
<body>
<form action="submit/submit_sales_upload.php" method="post" enctype="multipart/form-data">
<label for="file">Source File:</label>
<input name="csv" type="file" id="csv" />
<input type="submit" name="submit" value="Upload File">
</form>
</body>
</html>

 

(SUBMIT)

<?php
$con = mysql_connect("localhost","root","mypassword");
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }
mysql_select_db("mydatabase", $con);
if ($_FILES[csv][size] > 0) {
if ($row == 1){ $row++; continue; }
   //get the csv file
   $file = $_FILES[csv][tmp_name];
   $handle = fopen($file,"r");

   //loop through the csv file and insert into database
   do {
    if ($data[0]) {
	    mysql_query("INSERT INTO database.salesleads_ctr
  (champ
  ,msisdn
  ,alt_number
  ,id_number
  ,account_number
  ,sub_id
  ,customer_type
  ,campaign_code
  ,max_rrp_renew
  ,max_rrp_upgrade
  ,current_package
  ,cancellation_date)

  VALUES
		    (
			    '".addslashes($data[0])."',
			    '".addslashes($data[1])."',
 '".addslashes($data[2])."',
 '".addslashes($data[3])."',
 '".addslashes($data[4])."',
 '".addslashes($data[5])."',
 '".addslashes($data[6])."',
 '".addslashes($data[7])."',
 '".addslashes($data[8])."',
 '".addslashes($data[9])."',
 '".addslashes($data[10])."',
			    '".addslashes($data[11])."'
		    )
	    ");
    }
   } while ($data = fgetcsv($handle,1000,",","'"));
   //
   //redirect
   header('Location: redirect_import.php?success=1'); die;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/273229-csv-import-page/#findComment-1406115
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.