//super-duper function for insert
function insert_query($array, $table = "",$show_query = false)
{
if($show_query)
{
echo "INSERT INTO ".$table." (".join(",",array_keys($array)).") VALUES (".join(",",$array).")<br /><br />";
}
if(!is_array($array) or sizeof($array) == 0)
{
//echo 'Array cannot be empty!';
return false;
}
else
{
if(mysql_query("INSERT INTO ".$table." (".join(",",array_keys($array)).") VALUES (".join(",",$array).")"))
{
return true;
}
else
{
return false;
//just in case.
//echo/return mysql_error();
}
}
unset($array);
}
O teu código alterado
$valor = $_POST['valor'];
$post_vars = array('iphone3g1', 'iphone3g2', 'nome', 'iphone41', 'postal', 'apelido','valor');
$arr = array();
foreach($post_vars as $var)
{
$arr['`'.$var.'`'] => mysql_real_escape_string(trim($_POST[$var]));
}
$query = insert_query($arr,'clientes');
if($query == true)
{
echo 'All cool, ty for add!';
}
else
{
echo 'There was some problem for add this product.';
}
se quizeres ver o query formado, basta usar 3ª opção da função
//TRUE
insert_query($arr,'clientes',TRUE);
Também podes fazer array desse tipo
//example of array for 'insert_query' function
$arr = array(
'`id`' => '"'.$_POST['id'].'"',
'`name`' => '"'.$_POST['name'].'"',
'`text`' => '"'.$_POST['text'].'"',
);
insert_query($arr,'some_table');