doushuo1989 2013-07-18 08:44
浏览 84
已采纳

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

I want to pull 2 fields from a MySQL database and them use them in 2 hidden form fields along with allowing the user to input data into additional form fields. I am using the following code to do so:

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

I have setup the page that this form post to and echo'd the results but all I am receiving is the last record. Is there a way that I can get all the results for each row and post them in one go to the database?

EDIT: PHP Echo below.

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

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

I am very much a PHP newbie so appreciate anyones help on this.

Further EDIT:

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

Thanks,

John

  • 写回答

1条回答 默认 最新

  • douyao1994 2013-07-18 09:10
    关注

    Make hidden input as an array. Like,

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

    Then print,

    print_r($backup_date);

    print_r($server_desc);

    print_r($customer);

    Sample Code:

    /* 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)");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作