首先创建一个数据库表:
create table emp_table {
id int(10) auto_increment primary key,
emp_name varchar(100),
country varchar(50),
position varchar(50)
}
现在创建一个包含以下代码的 php 文件:
<h2>Insert new records</h2>
<form name="emp" method="post">
<table border="1">
<tr>
<td>Employee name</td>
<td>Country</td>
<td>Position</td>
</tr>
<tr>
<td><input type="text" name="data[emp_name][]" value="" /></td>
<td><input type="text" name="data[country][]" value="" /></td>
<td><input type="text" name="data[position][]" value="" /></td>
</tr>
<tr>
<td><input type="text" name="data[emp_name][]" value="" /></td>
<td><input type="text" name="data[country][]" value="" /></td>
<td><input type="text" name="data[position][]" value="" /></td>
</tr>
<tr>
<td><input type="text" name="data[emp_name][]" value="" /></td>
<td><input type="text" name="data[country][]" value="" /></td>
<td><input type="text" name="data[position][]" value="" /></td>
</tr>
<tr>
<td><input type="text" name="data[emp_name][]" value="" /></td>
<td><input type="text" name="data[country][]" value="" /></td>
<td><input type="text" name="data[position][]" value="" /></td>
</tr>
</table>
</form>
<?php
mysql_connect('localhost', 'dbuser', 'dbpassword');
mysql_select_db('db_name');
if(isset($_POST['data'])) {
$cnt = $_POST['data']['emp_name'];
for($i=0; $i<$cnt; $i++) {
$name = $arr['emp_name'][$i];
$country = $arr['country'][$i];
$position = $arr['position'][$i];
// insert sql
$sql = "insert into emp_table(name, country, position) values('".$name."', '".$country."', '".$position."')";
mysql_query($sql);
}
}
?>
<br/><br/>
<h2>Existing Records</h2>
<table border="1">
<tr>
<td>Employee name</td>
<td>Country</td>
<td>Position</td>
</tr>
<?php
$str = '';
$objQuery = mysql_query('select * from emp_table');
while($result = mysql_fetch_array($objQuery))
{
$str .= '<tr>';
$str .= '<td>'.$result['emp_name'].'</td><td>'.$result['country'].'</td><td>'.$result['position'].'</td>';
$str .= '</tr>';
}
echo $str;
?>
</table>
现在运行您的应用程序。您可以在同一页面上看到结果(带有现有记录)