Ridgejp
asked on
MySQL CSV Data Import - Clear down Variables
I'm using the attached php to import data into mysql db. I need to include within the php an unset command to clear the variable as currently when the browser is refreshed it reruns the mysql queries and re-inserts data from the previous submission.
I though I could use
Please help?
J
<?php include("connection_1.php");
if(isset($_POST['submit']))
{
// purge existing table data
$query = "DELETE from holding_ebay";
mysqli_query($link, $query);
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file,"r");
// fgets = misses the header row.
fgets($handle);
while(($fileop = fgetcsv($handle,1000,",")) !== false)
{
$sales_record_number = $fileop[0];
$user_id = $fileop[1];
$buyer_email = $fileop[3];
$buyer_name = $fileop[2];
$add_1 = $fileop[4];
$add_2 = $fileop[5];
$city = $fileop[6];
$state = $fileop[7];
$post_code = $fileop[8];
$country = $fileop[9];
$item_number = $fileop[11];
$item_title = $fileop[13];
$custom_label = $fileop[32];
$qty = $fileop[14];
$sale_price = $fileop[15];
$pp = $fileop[17];
$insurance = $fileop[18];
$total_price = $fileop[19];
$payment_method = $fileop[20];
$postage_service = $fileop[28];
$sale_date = $fileop[24];
$despatch_date = $fileop[27];
$newpost = $fileop[8];
$query = "INSERT into holding_ebay
( sales_record_number
,user_id
,buyer_email
,buyer_name
,add_1
,add_2
,city
,state
,post_code
,country
,item_number
,item_title
,custom_label
,qty
,sale_price
,pp
,insurance
,total_price
,payment_method
,postage_service
,sale_date
,despatch_date
,newpost)
VALUES
( '$sales_record_number'
,'$user_id'
,'$buyer_email'
,'$buyer_name'
,'$add_1'
,'$add_2'
,'$city'
,'$state'
,'$post_code'
,'$country'
,'$item_number'
,'$item_title'
,'$custom_label'
,'$qty'
,'$sale_price'
,'$pp'
,'$insurance'
,'$total_price'
,'$payment_method'
,'$postage_service'
,'$sale_date'
,'$despatch_date'
,'$newpost')";
mysqli_query($link, $query);
}
unset($fileop);
if (mysqli_query($link, $query))
{
echo "Query passed through..";
}
else {
echo "Query failed with error : " . mysqli_error($link);
}
}
?>
I though I could use
unset($fileop)
to reset the variable if placed outside the while loop but that isn't working.Please help?
J
ASKER
Thanks Ray - will review this info now. Was my initial thoughts about unsetting the array inappropriate at this point?
J
J
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again Ray.
J
J
Open in new window
Although you didn't ask about it, you might want to rethink the design strategy that has a script reading from $_FILES. This is a security hole. A better strategy would be to use move_uploaded_file() to copy the uploaded file(s) from the temporary server storage to your own storage. This will help ensure that you're actually using an uploaded file, and not something that is bogus.