当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Insert into table from array

问题描述:

$valor = $_POST['valor'];

$post_vars = array('iphone3g1', 'iphone3g2', 'nome', 'iphone41', 'postal', 'apelido');

foreach($post_vars as $var) {

$$var = "'" . mysql_real_escape_string($_POST[$var]). "', ";

}

$sql = "INSERT INTO clientes (iphone3g1, iphone3g2, nome, iphone41, postal, apelido, valor) VALUES ($$var '$valor')";

$query= mysql_query($sql);

Thank You so much for your previous answers guys.

I manares do write down this bit of code, i think it males scense but os not working.

I hope You can help me.

Thank You!!!

网友答案:
$valor = $_POST['valor'];

$post_vars = array('iphone3g1', 'iphone3g2', 'nome', 'iphone41', 'postal', 'apelido');
$post_values = array();
foreach($post_vars as $var) {
    $post_values[$var] = "'" . mysql_real_escape_string($_POST[$var]). "'";
}

$sql = "INSERT INTO clientes (" . array_implode(',', array_keys($post_values)) . ") VALUES (" . array_implode(',', array_values($post_values)) . ")";
$query= mysql_query($sql);
网友答案:
$columns = array('iphone3g1', 'iphone3g2', 'nome', 'iphone41', 'postal', 'apelido', 'valor');
foreach($columns as $column)
  $values[$column] = "'" . mysql_real_escape_string($_POST[$column]) . "'";
$rs = mysql_query("INSERT INTO clients (" . implode(',', $columns) . ") VALUES (" . implode(',', $values) . ")");
分享给朋友:
您可能感兴趣的文章:
随机阅读: