Page 1 of 1

PHP4 Array Manipulation

Posted: Mon Apr 28, 2008 4:00 am
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

Re: PHP4 Array Manipulation

Posted: Tue Apr 29, 2008 12:39 am
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: