...

View Full Version : INSERT MySQL error



Erindesign
04-29-2009, 07:30 PM
Hey all,

came up with a little string here that:

Uploads a text file
Gets data from text file one line at a time
Explodes each line by the TAB delimiter.
Inserts into a mysql database, and outputs.


It uploads the file, reads the file, outputs the results, but does not insert into mysql. Here is my mysql statement:



while($theData = fgets($fh)){

$part=explode("\t", $theData);

$sql="INSERT INTO items (customerid, itemname, modelnr, serialnr, condition, location, status, replacement, damage, diagnosis, fstate, labor, parts) VALUES ('$custid', '$part[0]', '$part[1]', '$part[2]', '$part[3]', '$part[4]', '$part[5]', '$part[6]', '$part[7]', '$part[8]', '$part[9]', '$part[10]', '$part[11]')";

mysql_query($sql, $conn) or die mysql_error();
....


Been messing with it for the past hour and cannot find whats wrong.

Thx in advance, Erind

bdl
04-29-2009, 08:34 PM
Hmm. You're not intending on using this script over once, are you? The reason I ask is, you're running N query calls in an undetermined loop based on how many lines are in a given file. If that's not a way to kill a server, I don't know what is.

Anyway, to your problem. Basic troubleshooting steps, blah blah blah. I see you've got mysql_error() there, so the query is obviously valid and not failing, but you say there is nothing in the database when you're done with the script execution? Have you tried to display the value stored in $sql to see that there is anything in the query itself? Or that $part is a valid array and contains what you think it does?

Fumigator
04-29-2009, 10:15 PM
Is that the exact code you are using? Because or die mysql_error(); gives me a parse error. You have to enclose mysql_error() in parenthesis.

bdl
04-30-2009, 01:31 AM
Is that the exact code you are using? Because or die mysql_error(); gives me a parse error. You have to enclose mysql_error() in parenthesis.

Good eye. While die() (or exit() for that matter) are not functions, but language constructs, they don't necessarily require the parens. In this case since you're actually passing something into die(), I'm sure that's correct.

Erindesign
04-30-2009, 02:41 PM
Hmm. You're not intending on using this script over once, are you? The reason I ask is, you're running N query calls in an undetermined loop based on how many lines are in a given file. If that's not a way to kill a server, I don't know what is.

Anyway, to your problem. Basic troubleshooting steps, blah blah blah. I see you've got mysql_error() there, so the query is obviously valid and not failing, but you say there is nothing in the database when you're done with the script execution? Have you tried to display the value stored in $sql to see that there is anything in the query itself? Or that $part is a valid array and contains what you think it does?


My files have a set amount of lines. Usually no more than 50-70. Is that not a finite loop?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum