从数组插入表

时间:2011-02-28 14:23:36

标签: php

$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);

非常感谢您以前的答案。

我马纳雷斯写下这段代码,我认为它是男性风格但是os不起作用。

我希望你能帮助我。

谢谢!!!

2 个答案:

答案 0 :(得分:0)

$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);

答案 1 :(得分:0)

$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) . ")");