php - Inserting multiple rows into a MySQL database from a HTML Form -
i want pull 2 fields mysql database , them use them in 2 hidden form fields along allowing user input data additional form fields. using following code so:
$yesterday = date('d/m/y',strtotime("-1 days")); $sql="select acct_backup_servers.server_desc, acct_customer.customer acct_backup_servers, acct_customer 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 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 page form post , echo'd results receiving last record. there way can results each row , post them in 1 go 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 php newbie appreciate anyones 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 $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 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
make hidden input 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 $key => $value) { $customer_id = $value; $date = $backup_date[$key]; $server = $server_desc[$key]; mysql_query("insert table_name values($customer_id, $date, $server)"); }
Comments
Post a Comment