PHP4 Array Manipulation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dave2008
Forum Newbie
Posts: 1
Joined: Mon Apr 28, 2008 3:46 am

PHP4 Array Manipulation

Post by dave2008 »

Hi,
I am using php4 to upload a Tab delimited text file into a mysql database. Each row of data is placed into an array and then appended the database. What I need to do is add another column/field into the array.

e.g I have my array (field1, field2, field3, field4)

What I need to do id insert a new field between field2 and field3

The code I am currently using is shown below:

Code: Select all

 
if($action == "insert")
{
  $fcontents = file("paypal.txt");
  # expects the csv file to be in the same dir as this script
 
  for($i=0; $i<sizeof($fcontents); $i++)
 {
     $line = trim($fcontents[$i]);
     $line=str_replace(",", "", $line);
     $arr = explode("\t", $arr); 
 
     $sql = "insert into mydb values ('".
     implode("','", $arr) ."')";
     mysql_query($sql);
   
     echo $sql ."<br>\n";
     if(mysql_error())
     {
         echo mysql_error() ."unable to load $fcontents<br>\n";
      }
 
      if(!mysql_error()) 
      {
         echo "<br>Database updated";
      }
 
}
 
Any help on this would be great

Thanks
Dave
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: PHP4 Array Manipulation

Post by nowaydown1 »

As far as I can tell, your script will already attempt to insert x number of values into your table. Meaning, in theory, you should be able to shove as many new "fields" in your tab separated file as you wanted. What might be causing you troubles however is that you aren't specifying what columns those fields should be inserted into. So, it's just going to use the default column order on the table.

If that's the case, you would need to change how your query is being built to also include the column names. Something like:

Code: Select all

INSERT INTO mydb (firstname, lastname, phone, email) VALUES ('bob', 'jenkins', '555-5555', 'bob@example.com')
Hopefully that makes sense to ya. If it doesn't, I blame me. Low on sleep :roll:
Post Reply