Jump to content

is this possible?


coolphpdude

Recommended Posts

for example i have an SQL table that requires the following fields:-

 

Fist Name                  Second Name              date of birth

 

 

but i have 600 users to add to this database. If i had a spreadsheet of 600 rows containing, first name, surname and DOB, could i come up with something that will automatically extract the information?

Link to comment
Share on other sites

heres what you do...

 

copy & paste the names out of the spread sheet onto seperate lines in a txt file..

 

FirstName LastName Birth

FIrstName LastName Birth ect....

 

then add stoppers so you know in your code where each section stops...

 

FirstName | LastName | Birth

 

with a decent txt editor this is a breeze

Link to comment
Share on other sites

from that point is simply setting up a loop to go through all the lines and pull out each string and put into database..

 

$file = fopen("test.txt","r");

echo fgets($file);

 

$filez = explode('|',$file,3);

 

$QUERY="INSERT INTO datatable (FirstName, LastName, Birth) VALUES ('".$filez[0]."', '".$filez[1]."', '".$filez[2]."');"

 

if($QUERY)

{

//SUCCESS

}

else

{

//FAIL

}

 

fclose($file);

Link to comment
Share on other sites

sry here it is within a loop

 

 

$file = fopen("test.txt","r");

while(! feof($file))
{
 echo fgets($file);

 $filez = explode('|',$file,3);

$QUERY="INSERT INTO datatable (FirstName, LastName, Birth) VALUES ('".$filez[0]."', '".$filez[1]."', '".$filez[2]."');"

if($QUERY)
{
 //SUCCESS
}
else
{
 //FAIL
}
}
fclose($file);

Link to comment
Share on other sites

so do a simple upload then if upload is successful get the file from the server (i.e /users/user.txt), run that script, if its successful delete user.txt from the server and respond completed otherwise respond fail?? Just to make sure i understand it properly...

 

 

As I already have a spreadsheet with users, is it possible for sumthing similar to be created with a spreadsheet?

Link to comment
Share on other sites

yes but I don't know how the spread sheet looks in a text editor and thats how php will read it.

 

(as far as i kno)

 

it will take nothing to put the names into a txt file.. easy as COPY&PASTE

 

then read through the txt file with the info line by line...

 

then explode the line into an array of 3 parts "First,Last,Birth"

 

then plug those variables into an insert statement and run the statement..

 

thus entering the values into the database..

 

then repeat.. (the loop)

Link to comment
Share on other sites

What the posts above are stating is correct but there is a hell of a lot more needed (if I were making this) to consider.

The first is a proper error routine. If an error is detected do you stop at that point or continue inserting the data?

Each field must be validated to ensure that the correct data types are inserted into the correct mysql fields. So if the database is expecting an email address to go into an email field then the xls value must be an email address and not say a numeric value or non-email address value. As in coolphpdude's post you may be better using pipe delimiters rather than csv values as text often contains commas and can cause your data to be improperly separated.

Link to comment
Share on other sites

yeah i get what you mean. So validate the fields within the spreadsheet the same as i have validated the structure when creating the initial table? Then save as .csv. i have just taken a look in myphpadmin and isee what you mean... CSV using LOAD DATA. one question, with it being a spreadsheet does it automatically but the next column into the next field??

 

Cheers for all your help you lot, its appreciated.

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.