从HTML表单中将多行插入MySQL数据库

时间:2013-07-18 08:44:07

标签: php mysql

我想从MySQL数据库中提取2个字段,并在2个隐藏的表单字段中使用它们,并允许用户将数据输入到其他表单字段中。我使用以下代码来执行此操作:

$yesterday = date('d/m/Y',strtotime("-1 days"));

$sql="SELECT acct_backup_servers.server_desc, acct_customer.customer from acct_backup_servers, acct_customer
    where acct_backup_servers.cust_act=acct_customer.acct_no ";

$result=mysql_query($sql);

echo '<form action="http://www.bimotech.co.uk/admin/insert_backup_rec/index.php" method="post">
<table>
<tr>
<th width="20%" align="left" valign="top">Server</th>
<th width="20%" align="left" valign="top">Customer</th>
<th align="left" valign="top">Date</th>
<th width="25%" align="left" valign="top">Status</th>
<th width="30%" align="left" valign="top">Notes</th>
<th align="left" valign="top"></th>
</tr>';

while($rows=mysql_fetch_array($result)){

$server_notes = $rows['server_desc'];

echo "
<input type='hidden' name='server' value='".$rows['server_desc']."'>
<input type='hidden' name='customer' value='".$rows['customer']."'>
<tr>
<td valign='top'>".$rows['server_desc']."</td>
<td valign='top'>".$rows['customer']."</td>
<td valign='top'><input type='text' name='date' size='10' id='datepicker' value='".$yesterday."'></td>

<td valign='top'>
<select name='status'>";
$sql2 = mysql_query("SELECT cat_desc FROM acct_backup_category");
while ($row = mysql_fetch_array($sql2)){

echo "<option value='category'>" . $row['cat_desc'] . "</option>";

}

echo "</select></td>
<td valign='top'><textarea name='notes'></textarea></td>
<td valign='top'><input type='checkbox' name='completed'></td>
</tr>";

}

echo "</table>
    </br>
    <input type='submit'  class='btn' value='Submit'>
    </form>";

mysql_close();

我已设置此表单发布的页面并回显结果,但我收到的是最后一条记录。有没有办法让我可以获得每一行的所有结果并将它们一次性发布到数据库中?

编辑:PHP Echo下面。

$backup_date = $_POST['date'];
$server_desc = $_POST['server'];
$customer = $_POST['customer'];

echo $backup_date;
echo $server_desc;
echo $customer;

我非常喜欢PHP新手,所以感谢任何人的帮助。

进一步编辑:

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

foreach($customer as $key => $value) {
    $date = $backup_date[$key];
    $server = $server_desc[$key];
    $customer_id = $value;
    $backup_code = $status[$key];
    $notes_field = $notes[$key];
    $been_checked = $checked[$key];
    mysql_query("INSERT INTO acct_backups (backup_date, server_code, cust_act, backup_code, notes, checked) VALUES ($date, $server, $customer_id, $backup_code, $notes_field, $been_checked)");
}

mysql_close();

谢谢,

约翰

1 个答案:

答案 0 :(得分:0)

将隐藏输入作为数组。 喜欢,

<input type='hidden' name='server[]' value='".$rows['server_desc']."'>
<input type='hidden' name='customer[]' value='".$rows['customer']."'>

然后打印,

的print_r($ backup_date);

的print_r($ server_desc);

的print_r($顾客);

示例代码:

/* MySQL Conncection goes here*/

foreach($customer as $key => $value) {
    $customer_id = $value;
    $date = $backup_date[$key];
    $server = $server_desc[$key];
    mysql_query("INSERT INTO table_name VALUES($customer_id, $date, $server)");
}