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#的问题:需要 MATLAB 运行程序,对比四个多址通信方式(TDMA,FDMA,CDMA,OFDMA)的抗噪声性能
  • ¥15 有没有复现过PFENet的,如何解决?
  • ¥15 FDMA,OFDMA,TDMA 和 CDMA 系统容量对比仿真
  • ¥15 图神经网络的入侵检测
  • ¥15 工业数据采集技术+存储架构推荐
  • ¥15 GPT-SOVITS如何训练少数民族语言支持合成语音
  • ¥20 树莓派4b使用Camera Module 3时出现the system should be configured for the legacy camera stack问题
  • ¥200 GitHub开源程序配置在VScode调试
  • ¥15 爬虫保存的scv文件0kb
  • ¥20 如何实现基于强化学习的带电作业机械臂的运动规划与控制