Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert help

Status
Not open for further replies.

way2many

Technical User
Nov 28, 2003
139
US
Hi,
I'm using code below that I found somewhere for inserting data from csv & txt files. Works fine.
The problem is that I have to insert also clientID (coming from Session) and current Date - these two last columns are empty in csv/txt files

Any help would be appreciated.

<?
$dbH = mysql_connect('localhost', 'root', '') or die('Could not connect to MySQL server.<br>' . mysql_error());

mysql_select_db('eprc') or die('Could not select database.<br>' . mysql_error());

$fcontents = file ('data4.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]);
$arr = explode(",", $line);
#if your data is comma separated
# instead of tab separated,
# change the '\t' above to ','

$sql = "insert into parts_copy values ('".
implode("','", $arr) ."')";
mysql_query($sql);
//echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}

mysql_close($dbH);
?>

Thank you
 
Would this work?
Code:
$sql  = "insert into parts_copy values ('";
$sql .= implode("','", $arr);
$sql .= $_SESSION['ClientID'] ."',now())"; 
mysql_query($sql) or die ('Error: ' . mysql_error());
I suggest you also echo $sql; before executing it to inspect if it looks like you want it to look.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top