Hello all i have here my code :Code:
<?php
$insertValues = array(); //Temp array to store values
foreach($array as $id => $record)
{
$name = $record[0];
$age = $record[1];
$addr = $record[2];
$weight = $record[3];
$height = $record[4];
$insertValues[] = "('{$name}', '{$age}', '{$addr}', '{$weight}', '{$height}')";
}
//Create/run insert quer
$query = "INSERT INTO table_name
(`name`, `age`, `address`, `weight`, `height`)
VALUES " . implode(', ', $insertValues);
$result = mysql_query($query);
?>
The code works fine but have problems when a field contain quotes, double quotes and commas.
Ex : when $addr = "address 1, sample's street."
This would cause an error when i try to save it into the database. the query string would give me this. Please see belowCode:
<?php
/* for example the implode results would be ('thomas','17','5 feet','60 kilograms','address 1, sample's street.') */
$query = "INSERT INTO table_name
(`name`, `age`, `height`, `weight`, `address`)
VALUES ('thomas','17','5 feet','60 kilograms','address 1, sample's street.');
?>
as you can see the string on Values has an error since the bold information has a wrong format.. Please see belowCode:
<?php VALUES ('thomas','17','5 feet','60 kilograms','address 1, sample's street.'); ?>
can someone help me modify the code so that i could save my information whether or not it has a comma, quote and double quote?
thanks guys