lalthung lalthung - 1 year ago 64
MySQL Question

multiple row insert inserts 2 extra rows with 0

guys im having issue inserting multiple rows all is ok but 3 extra rows with 0 value gets inserted into employee_id

if (isset($_POST['submit'])) {
$i = 0;
foreach ($_POST as $val) {
$user_id = $_POST['user_id'][$i];
$employee_name = $_POST['employee_name'][$i];
$remarks = $_POST['remarks'][$i];
$time_in = $_POST['time_in'][$i];
$time_out = $_POST['time_out'][$i];
$team = $_POST['team'][$i];
mysql_query("INSERT INTO attendance (employee_id, employee_name, remarks, time_in, time_out, team) VALUES ('$user_id', '$employee_name', '$remarks','$time_in', '$time_out', '$team')");

and this is the form

$time_in = date('Y-m-d H:i');
$result=mysql_query("SELECT * FROM members WHERE login != 'admin'");
$rowNo = 1; //Increment Row Number
while($row = mysql_fetch_array($result))
$id = $row['member_id'];
$user = $row['login'];
$team = $row['team'];
echo "<tr align='left'>";
echo"<td width='10px'><font color='white'>" .$rowNo++."</font>.</td>";
echo"<td><font color='black'><input type='hidden' name='user_id[]' value='".$id."'/> <input type='hidden' name='employee_name[]' value='".$user."'/> " .$row['login']."</font></td>";
echo"<td><font color='black'>
<select name='remarks[]'>
<option valur='Present'>Present</option>
<option valur='Absent'>Absent</option>
<option valur='Half Day'>Half Day</option>
echo"<td><font color='black'><input type='text' name='time_in[]' value='".$time_in."'/> <input type='text' name='time_out[]' value='".$time_out."'/> <input type='hidden' name='team[]' value='".$team."'/></font></td>";
echo "<p align='right'>



how could this be corrected?


Answer Source

What's your form look like?

Also, you shouldn't be using mysql_ commands. Deprecated. Should be using mysqli procedural or object-oriented.

Editing to provide likely answer...

Instead of looping over $_POST, name one of your form inputs form_value[] or similar and instead loop: foreach($_POST['form_value'] as $val).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download