Hey guys not looking for explanation on why its inserting twice, I understand it loops from ['0'] to ['1'] just want to figure out how to stop my insert after the first try...
$trans=array('hello' => 'allo',
'bye' => 'salut'),
$sql = "INSERT INTO trans (en, fr) values ";
$valuesArr = array();
foreach ($trans as $key => $value){
$en = mysql_real_escape_string( $key );
$fr = mysql_real_escape_string( $value );
$valuesArr[] = "('$en', '$fr')";
}
$sql .= implode(',', $valuesArr);
mysql_query($sql) or exit(mysql_error());
My actual question is can i stop the foreach as soon as it goes through the array using break;?
mysql_queryshouldn't be used in new code because it's been deprecated, is dangerous if used incorrectly, and is being removed from future versions of PHP. A modern replacement like PDO is not hard to learn. A guide like PHP The Right Way has a number of recommendations as to how to keep your application up-to-date.