Hi all. I am fairly new to PHP but I have managed to solve problems that I have come across so far by reading helpful information in forums, but this problem has me really stumped!
My PHP code allows a user to update a database by uploading a CSV file. However if there are any special characters in a record (such as apostrophes) the database will not be updated properly.
I know I need to escape the special characters, and I have tried reading the CSV file into a string and used 'addslashes' but MySQL calls for it to be put back into an array which I am unsure of.
I am using PHP Version 5.2.10 so I fear that some of the possible solutions will not be available to me. :bemused:
Any suggestions you can offer will be greatly appreciated.
Many thanks in advance!
Rhianon
<?php
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
}
//Import uploaded file to Database
$row = 1;
$handle = fopen($_FILES['filename']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ( $row == 1){
$row++;
} else {
$import="REPLACE INTO carstock(stocknumber,make,model) values('$data[0]','$data[1]','$data[2]')";
mysql_query($import) or die(mysql_error());
}
}
fclose($handle);
}
?>
<form enctype="multipart/form-data" action="vehicle_import.php" method="post">
File to import:<br />
<input size='30' type='file' name='filename'>
<input type="submit" name="submit" value="Upload"></form>