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.

 

Edited by Christian F.
Link to comment
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
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
Share on other sites

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.