【发布时间】:2016-10-19 06:57:34
【问题描述】:
表单提交的值存储在 $array 中。
$array= array("123","James","New York");
MySQL 表(客户)
ID-----NAME-----CITY
我是 PHP 新手。我想将数组数据插入到 MySQL 表中的相关列中。
mysql_query("INSERT INTO customer (ID,NAME,CITY)
VALUES ('123','James','New York')" );
可以使用,但是每次提交表单时数组元素的数量可以改变。
我怎样才能使用 PHP 做到这一点?
注意:
在选择表格页面中,用户从下拉菜单中选择表格,然后加载自定义表格。所以列名取决于用户选择的表。我可以在“$array”中获取表单中提交的值。但问题是每次值的数量都在变化。
mysql_query(INSERT INTO customer VALUES ($array)); //这种格式不能用
选择表格代码
<?php
include("functions.inc");
connectDatabase();
$queryseltable= "SHOW TABLES" or die("error query");
$result=mysql_query($queryseltable);
?>
<link href="style.css" rel="stylesheet" type="text/css" />
<form action="process.php" method="post">
<div id="wrapper">
<table>
<tr>
<td id="right">Select Table:</td>
<td id="input">
<select name="table">
<?php
while($row=mysql_fetch_array($result)){
extract($row);
echo "<option>".$Tables_in_database ."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Select" /></td>
</tr>
</table>
</div>
</form>
<?php
?>
Process.php 代码
<link href="style.css" rel="stylesheet" type="text/css" />
<form action="insert.php" method="post">
<?php
include("functions.inc");
connectDatabase();
$table= $_POST['table'];
$_SESSION['table'] = $table;
$query= "SELECT * FROM $table";
$result= mysql_query($query) or die("query failed");
$count=mysql_num_fields($result);
$fieldname = mysql_field_name($result, 0);
?>
<div id="wrapper">
<table>
<?php
for($x=0; $x<$count;$x++){
$fieldname = mysql_field_name($result, $x);
echo "
<tr>
<td id=\"right\">$fieldname</td>
<td id=\"input\"><input type=\"text\" name=\"test[]\" /></td>
</tr>
";
}
?>
<tr>
<td></td>
<td><input type="submit" value="Insert" /></td>
</tr>
</table>
</div>
</form>
【问题讨论】:
-
您的表单是什么样的?你试过做一个 print_r($_REQUEST)
标签: php mysql arrays insert html-table