正如SleepyCod所说,你的乳房赌注是
foreach
,但答案需要扩展到:
表单字段是否与表等效?
每种形式都是用来
UPDATE
或
INSERT
不只是桌子?
我问的原因是因为这是我为我的网页做的事情。我创建了一个动态表单结构,在该结构中查询有问题的表,检索模式并
前额
+
switch()
用于确定要使用的字段。
但这不是你要的。
所以,我给你:
// Assuming one table:one form, and each input-name = column name.
//strip array $_POST into its key and value.
foreach($_POST as $key => $val) {
$vals .= "'$val', ";
$keys .= "`$key`, ";
}
// Lest we want to generate errors, shave off the trailing comma and whitespace.
$keys_strip = substr($keys, 0, -2);
$vals_strip = substr($vals, 0, -2);
$sql = mysql_query("INSERT INTO t1 ($keys_strip) VALUES ($vals_strip)");
对于多张桌子来说,这有点棘手。我会选择在每个表单的开头使用快速标识,这样我们就可以做到以下几点:
// Assuming two+ table:one form, for each input, name='t1:column_name'; assume tables are defined in an array per form for easy reference.
//strip array $_POST into key and value, then separate the key into two separate fields. This will ONLY work for t1:column-name set up; an if statement can be put in to deal with the remaining information.
foreach ($_POST as $key as val) {
// This will result in keys_t1 = key and vals_t1 = val.
if (preg_match('/^(\w.+):(\w.+)$/', $key, $t_key)) {
${"keys_".$t_key[1]} = "$t_key[2], ";
${"vals_".$t_key[1]} = "$var, ";
}
}
// Assume $tables array, containing tables for THIS insert.
foreach ($tables AS $table) {
$keys_strip = substr(${"keys_".$table}, 0, -2);
$vals_strip = substr(${"vals_".$table}, 0, -2);
$sql = mysql_query("INSERT INTO table ($keys_strip) VALUES ($vals_strip)");
}
可能需要一些工具来让它正常工作,但它应该能让您到达需要的地方。记住这只会
插入
基于表单的信息。如果你需要什么…好。。。我强烈建议使用隐藏的输入类型。