I made a form which takes the attendance of around 11 people and what i need is to insert the daily attendance in a database. i want to use one single query to insert the attendance of all employees at once instead of writing 11 different queries for each of them. my table structure is ike this : attendance(date,eid,ename,attendance) I tried the following code for my bulk insert but it didnt work. any suggestions??
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbname = "gail";
$conn = mysql_connect($dbhost, $dbuser,"") or die ('Error connecting to mysql');
mysql_select_db($dbname);
$dbh->beginTransaction();
$stmt = $dbh->prepare("INSERT INTO attendance VALUES (curdate(),'$_POST[eid]','$_POST[ename]','$_POST[pora]')");
foreach($valuesToInsert as $insertRow)
{
// now loop through each inner array to match binded values
foreach($insertRow as $column => value)
{
$stmt->bindParam(":{$column}", value);
$stmt->execute();
}
}
$dbh->commit();
?>
I also tried this:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbname = "gail";
$conn = mysql_connect($dbhost, $dbuser,"") or die ('Error connecting to mysql');
mysql_select_db($dbname);
$employees=array();
while($row=mysql_fetch_assoc($result))
{
$employees[$row["eid"]]=array("ename"=>$row["ename"]);
$employees[$row["eid"]][$row["dated"]]=array();
$employees[$row["eid"]][$row["dated"]][$row["ename"]]=$row["pora"];
$inserts = array();
foreach($employees as $v)
{
$inserts[] = "(curdate(),'$_POST[eid]','$POST[ename]','$POST[pora]')";
$query = "INSERT INTO attendance VALUES ". implode(", ", $inserts);
echo "query = $query"; // for debugging purposes, remove this once it is working
mysql_query($query) or die(mysql_error());
}
}
?>
but all this does is insert current date in the table a large number of times. please help. thnks in advance :)
i did this one more thing. it inserts multiple rows in the db exactly the no of rows u want but all blank. any ideas on how to deal with that?
html file:
Beamline ID Flow
Beamline ID
Flow
Beamline ID Flow
Beamline ID Flow
Beamline ID Flow
php:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbname = "gail";
$conn = mysql_connect($dbhost, $dbuser,"") or die ('Error connecting to mysql');
mysql_select_db($dbname);
$cnt = count($_POST['bline_id']);
$cnt2 = count($_POST['flow']);
if ($cnt > 0 && $cnt == $cnt2) {
$insertArr = array();
for ($i=0; $i<$cnt; $i++) {
$insertArr[] = "('" . mysql_real_escape_string($_POST['bline_id'][$i]) . "', '" . mysql_real_escape_string($_POST['flow'][$i]) . "')";
}
$query = "INSERT INTO bltest (bline_id, flow) VALUES " . implode(", ", $insertArr);
mysql_query($query) or trigger_error("Insert failed: " . mysql_error());
}
echo("<pre>\n");
print_r($_POST);
echo("</pre>\n");
mysql_close($conn);
?>