Hi all,
I’m having some trouble with getting my data to play nice with MySQL.
Here is a line from my text doc:
“VISTORTA”,“MERLOT\”,“WINE-IMPORTED”,“”,13.9900
Here’s my php that reads the text and prepares the sql statement.
$handle = fopen($dir, "r");
$sql = "INSERT INTO inventory (name, description, type, size, price) VALUES ";
while(($data = fgetcsv($handle)) !== FALSE)
{
list($name, $description, $type, $size, $price) = $data;
$name = addslashes(ucwords(strtolower($name)));
$description = addslashes(ucwords(strtolower($description)));
$sql .= "('$name', '$description', '$type', '$size', $price), ";
}
$sql = substr_replace($sql, '', -2, 2);
fclose($handle);
$insertInv = $dbh->prepare($sql);
$invQuery = $insertInv->execute();
It throws an error, so when I echo out the sql statement, the original line in the text is changed to this in the sql.
('Vistorta', 'Merlot\\",wine-imported\\"', '', '13.9900', )
Instead of :
('Vistorta', 'Merlot\\', 'WINE-IMPORTED', '', 13.9900)
That slash is doing it and I’m having trouble figuring out how to allow it. I’ve tried adding parameters to the fgetcsv function
$data = fgetcsv($handle, '', ',','','')
To try to allow the backslash instead of using it as an escape character, but that seems to freeze the process.
Could I get some assistance please? Thanks!