Forum Moderators: coopster

Message Too Old, No Replies

Inserting specific data from a txt file to mysql

         

Rob29

5:22 pm on May 28, 2008 (gmt 0)

10+ Year Member



So what im trying to do is...

Have a submit form so you can submit a text file. This text file will have lots of information in it, but i only want the information that is located between " marks to be read and than inserted into a database, one entry at a time.. is that possible?

Also, the file that is submitted dosn't need to be stored, only read once so the information can be inserted to the database, than discarded.

A simple sample file would look like the following (but with more information on it of course). all id want is the entries in quotation marks inserted into the database, one entry at a time, the rest is useless information.


Sample Text file
------------------------------
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
------------------------------
Random text, etc etc
"entry 1" text text text
text text "entry 2" text text
"entry 3" text text
text text text
------------------------------
End of sample

So far, i have it so i can insert information per line into a database, if the file is already on the server. code is below. However, i dont even know if this is the correct way of doing it or not.


<?php
include('setup.php');
$query = 'insert into dbname (entry) values ("%s");';
$lines = file('text.txt');
for($i = 0; $i < count($lines); ++$i)
mysql_query(sprintf($query, mysql_real_escape_string($lines[$i]))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
?>

If anyone can give me a hand id be very grateful. Thanks

RonPK

7:21 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could use
preg_match()
[php.net] to detect any strings between quotation marks in each line ($lines[$i]). If any, loop through the matches and store them.

if (preg_match('/"(.*?)"/', $lines[$i], $matches)) { 
foreach($matches[1] as $match) {
// do query here, using $match
}
}

(disclaimer: untested..)

[edited by: coopster at 7:45 pm (utc) on May 28, 2008]
[edit reason] Added a smilestopper [/edit]

cameraman

7:24 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, Rob29!

You could use a regular expression to extract your data:
$lines = implode('',file('text.txt'));
$count = preg_match_all('#"(.*)"#m',$lines,$data);
echo "<pre>";
for($i = 0; $i < $count; $i++)
echo $data[1][$i] . "\n";
echo "</pre>";

The pattern says to look for a quote followed by whatever followed by a quote. The 'm' says to keep going for multiple lines.

Rats, RonPK is faster - the difference in our solutions is mine is doing all lines at once while RonPK's is sticking with your line-at-a-time.

RonPK

7:28 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



he he :)

file_get_contents() would be shorter than implode('',file('text.txt'));

Rob29

5:20 am on May 29, 2008 (gmt 0)

10+ Year Member



Cool, that makes sense. Im still learning PHP so im still not sure about some things.

Ill try and throw it together and see if i can get it working. and i need to stick with a line at a time. basicly each entry between quotes needs to be a new entry into the database.

How about adding a submit a file form so whats submitted can be read with this script? Making a submit form for a file isnt to hard for me but im not sure past just simple upload.

Thanks for the help and the warm welcome :)

P.S. , i just checked and the quotes are single ' not ", im asuming the script would still work the same if i just used '? or would i have to word it differently?

[edited by: Rob29 at 5:26 am (utc) on May 29, 2008]

cameraman

6:23 am on May 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since the pattern uses single quotes on the outside:
'/"(.*?)"/'

You would need to escape them on the inside:
'/\'(.*?)\'/'

OR you can switch to double-quotes on the outside (which I think is a little easier to read):
"/'(.*?)'/"