• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

PHP/MySQL INSERT, tab delimited file

hello all,

I have the following code and it seems right to me, but it isnt actually performing the insert.  any ideas?

CODE
------------------------

<?  
$server = mysql_connect('localhost', 'root') or die(mysql_error());
mysql_select_db ('paper') or die (mysql_error());

$readfile = file("customers.txt");

for ($k=0; $k<=count($readfile)-1; $k++) {
    $fields = split("\t",$readfile[$k]);
    //echo("$fields[0] and $fields[1] and $fields[3] and $fields[4] and $fields[5] and $fields[6] and $fields[7] and $fields[8] and $fields[9] and $fields[10] and $fields[11] and $fields[12] and $fields[13]<br>");
            
            $query = "INSERT INTO customers VALUES ('$fields[0]','$fields[1]','$fields[2]','$fields[3]','$fields[4]','$fields[5]','$fields[6]','$fields[7]','$fields[8]','$fields[9]','$fields[10]','$fields[11]','$fields[12]','$fields[13]')";
            //echo $query;
            mysql_query($query);
            mysql_close();

            die(mysql_error());
}
?>

---------------------------
END CODE
0
flow79
Asked:
flow79
  • 2
1 Solution
 
RoonaanCommented:
Try: mysql_query($query) or die(mysql_error());

-r-
0
 
sdmdjCommented:
Give this a go, works fine here.

Note: I've left my source as it was, so you'll need to change any passwords or usernames back to your own. You also might want to name your database columns a little better than in my example! :-)

<?php
$server=@mysql_connect("localhost","root","mysql") or die(mysql_error());

@mysql_select_db("test",$server) or die(mysql_error());

$readfile=file("customers.txt");

for($k=0; $k<=count($readfile)-1; $k++)
{
      $fields=explode("\t",$readfile[$k]);

      $query="insert into customers (id,field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11,field12,field13,field14) VALUES (null,'$fields[0]','$fields[1]','$fields[2]','$fields[3]','$fields[4]','$fields[5]','$fields[6]','$fields[7]','$fields[8]','$fields[9]','$fields[10]','$fields[11]','$fields[12]','$fields[13]')";

      @mysql_query($query,$server) or die(mysql_error());
}

@mysql_close() or die(mysql_error());
?>
0
 
sdmdjCommented:
Oops, change:

@mysql_close() or die(mysql_error());

to:

@mysql_close($server) or die(mysql_error());

To ensure it closes the connection we created :-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now