Im having a bit of trouble getting my PHP script working.
What I'm trying to accomplish: On a subpage I want a very simple CSV fileupload to a MySQL database. It needs to either DROP or TRUNCATE a table and either CREATE or INSERT INTO, depending on previous solution, some data within this CSV file.
What my problem is now: I'm quite a beginner with PHP and MySQL, and as such are having trouble figuring out why my script is not working.
My code:
The PHP:
$connect = mysql_connect("host","user","pass");
mysql_select_db("database",$connect); //select the table
if ($_FILES[csv][size] > 0) {
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
mysql_query ("
CREATE TABLE IF NOT EXISTS `database`.`table` (
`item_number` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_danish_ci NOT NULL ,
`item_desq` VARCHAR( 200 ) CHARACTER SET utf8 COLLATE utf8_danish_ci NOT NULL ,
`item_img_path` VARCHAR( 200 ) CHARACTER SET utf8 COLLATE utf8_danish_ci NULL ,
`item_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
PRIMARY KEY ( `item_id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_danish_ci
");
do {
if ($data[0]) {
mysql_query("
INSERT INTO strand_items1 (item_number, item_desq, item_img_path)
VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."
)
");
}
} while ($data = fgetcsv($handle,1000,";","\""));
header('Location: import_old.php?success=1'); die;
}
The HTML:
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
The CSV: Your ordinary line in the CSV looks like this
"13371337";"Some description of the item";"NULL";
And now, the results: From this, I get a "succesfull" flow, yet no data in the table, and from what I can guess, an infinite loop of some sort on the database, since it creates an increasing amount of empty entries in the table with only the item_id auto-incrementing on and on. Where am I going wrong and is there an easy way to fix this?
This is my first question on here, and I hope I meet all requirements. (To point out my newb-ness; question was first posted on meta.stackoverflow.com ;) ) If not, please be patient - I will monitor this question closely and answer quickly on any replies and requiests.
Thank you in advance :)