Check Box

Hi, i am an absolute novice and would really appreciate some help.
I am running a very simple script to upload a csv to a table in MySQL.
It is working very well, but the first function in it is to empty the table contents first like below
[php]$deleterecords = “TRUNCATE TABLE feed”; //empty the table of its current records if selected
mysql_query($deleterecords);[/php]

Entire Form
[php]<?php

include “connect.php”; //Connect to Database

$deleterecords = “TRUNCATE TABLE feed”; //empty the table of its current records if selected
mysql_query($deleterecords);

//Upload File
if (isset($_POST[‘submit’])) {
if (is_uploaded_file($_FILES[‘filename’][‘tmp_name’])) {
echo “

” . “File “. $_FILES[‘filename’][‘name’] .” uploaded successfully.” . “

”;
}
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

	$import="INSERT into feed(ProductName,Brand,ProductURL,ImageURL) values('$data[0]','$data[1]','$data[2]','$data[3]')";

	mysql_query($import) or die(mysql_error());
}

fclose($handle);

print "Import done";

//view upload form

}else {

print "Upload new csv by browsing to file and clicking on Upload<br />\n";

print "<form enctype='multipart/form-data' action='upload.php' method='post'>";

print "File name to import:<br />\n";

print "<input size='50' type='file' name='filename'><br />\n";

print "<input type='submit' name='submit' value='Upload'></form>";

}

?>[/php]

What i would like to do is add a checkbox to the TRUNCATE TABLE feed … so that the table will only be emptied if the checkbox is selected, otherwise the csv contents should be added to the table …

I hope that makes sense.
Thanks for any assistance.

you can do this in the form…

[php]
print “Empty Table?”;
[/php]

and then this in the beginning of the script…
[php]
if($_POST[‘emptyTable’] === “true”){
$deleterecords = “TRUNCATE TABLE feed”; //empty the table of its current records if selected
mysql_query($deleterecords);
}
[/php]

I believe that should work just fine. Let me know if it doesn’t

Hi thanks very much, that worked … i had to make a little modification to code for the form, i’m not sure why but your original code was causing an error but i changed it to this :

[php]print “Empty Table ?”;[/php]

And it worked, not exactly sure what was wrong but i just copied some of my existing form field and modified it to work with your suggestion … Thanks !

I don’t know if you can help me with something else or may be i should start a new thread ? But i will ask anyway … I was just wondering if there is something simple that can be done to ignore the first row of the CSV, being that the first row id usually Column headers … not the end of the world but would be great if someone could point me in the right direction

Thanks again

In response to skipping the first line of the csv file, you can simply implement a flag to skip the first line as so:

[php]
//Import uploaded file to Database
$handle = fopen($_FILES[‘filename’][‘tmp_name’], “r”);
$first_line_flag = true;

while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE) {
if ($first_line_flag)
{
$first_line_flag = false;
continue;
}
$import=“INSERT into feed(ProductName,Brand,ProductURL,ImageURL) values(’$data[0]’,’$data[1]’,’$data[2]’,’$data[3]’)”;
mysql_query($import) or die(mysql_error());
}

fclose($handle);
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service