我制作了一个表格,它需要大约 11 人的出勤率,我需要在数据库中插入每日出勤率。我想使用一个查询一次插入所有员工的出勤率,而不是为每个员工编写 11 个不同的查询。我的表结构是这样的:出勤(日期,开斋节,姓名,出勤) 我为批量插入尝试了以下代码,但它没有用。有什么建议么??
<?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();
?>
我也试过这个:
<?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());
}
}
?>
但这所做的只是在表中多次插入当前日期。请帮忙。提前谢谢:)
我又做了一件事。它在数据库中插入多行,正是你想要的行数,但都是空白的。关于如何处理的任何想法?
.html 文件:
光束线 ID 流
光束线 ID
流
光束线 ID 流
光束线 ID 流
光束线 ID 流
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);
?>