0

我想在一个表中插入自动数据..一个数据将来自另一个我在这里称为 p_no(personal number) 的表,它来自另一个表(emp_info) n 它是另一个表出勤的外键。我想自动插入 1200 个数据,只需单击一个按钮 .1200 数据意味着 1200 p_no 和其他信息。

1.emp_info(p_no,name,designation..etc) P_no 是主键 2.attandance (aid(auto increment),attendance,tiffin,shift,ot date,p_no) p_no 是外键。现在我想通过单击一个按钮从 emp_info 表中访问 p_no 信息来自动插入 1200 个数据。

我正在使用dreamweaver、php、mysql。


 <?php require_once('Connections/bndock.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break; 
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

mysql_select_db($database_bndock, $bndock);
$query_Recordset1 = "SELECT p_no FROM emp_info";
$Recordset1 = mysql_query($query_Recordset1, $bndock) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);


$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$val=10;

for($row_Recordset1=0;$row_Recordset1<=$val;$row_Recordset1++){

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO attndance (aid, doa, attnc, stnd, ot, shift, p_no) VALUES (%s, now(), %s, %s, %s, %s,%s)",
                       GetSQLValueString($_POST['aid'], "int"),                    
                       GetSQLValueString($_POST['attnc'], "text"),//attendance//doa=date
                       GetSQLValueString($_POST['stnd'], "text"),//tiffin
                       GetSQLValueString($_POST['ot'], "text"),
                       GetSQLValueString($_POST['shift'], "text"),
                    GetSQLValueString($_POST['p_no'], "int"));

  mysql_select_db($database_bndock, $bndock);
  $Result1 = mysql_query($insertSQL, $bndock) or die(mysql_error());

  $insertGoTo = "attnd_show.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}



}

?>

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table width="296" height="190" align="center" bordercolor="#ECE9D8" bgcolor="#ECE9D8">
          <tr valign="baseline">
            <td colspan="2" align="right" nowrap="nowrap">&nbsp;</td>
            </tr>
          <tr valign="baseline">
            <td width="77" align="right" nowrap="nowrap"><strong>Attendance:</strong></td>
            <td width="193"><select name="attnc">
              <option value="P" selected="selected">P</option>
              <option value="A">A</option>
              <option value="L">L</option>
              <option value="Sick">Sick</option>
              <option value="Suspend">Suspend</option>
              <option value="TY">TY</option>
                                                                                                  </select>            </td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right"><strong>Stand Easy :</strong></td>
            <td><select name="stnd">
              <option value="Yes">Y</option>
              <option value="No">N</option>
                          </select>            </td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right"><strong>Over time:</strong></td>
            <td><select name="ot">
              <option value="Yes">Y</option>
              <option value="No">N</option>
                          </select>            </td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right"><strong>Shift:</strong></td>
            <td><select name="shift">
              <option value="A">A</option>
              <option value="B">B</option>
              <option value="C">C</option>
              <option selected="selected">-</option>
                                                                                                                          </select>            </td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right"><strong>P.No:</strong></td>
            <td><select name="p_no">
                <?php 
do {  
?>
                <option value="<?php echo $row_Recordset1['p_no']?>" <?php if (!(strcmp($row_Recordset1['p_no'], $row_Recordset1['p_no']))) {echo "SELECTED";} ?>><?php echo $row_Recordset1['p_no']?></option>
                <?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
?>
              </select>            </td>
          </tr>

          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td><input name="submit" type="submit" value="Insert record" /></td>
          </tr>
        </table>

<input type="hidden" name="aid" value="" />
        <input type="hidden" name="MM_insert" value="form1" />
      </form></td>
4

1 回答 1

0

我认为您想编写一个考勤系统来标记员工的失踪/病假天数。

我并不是说您不应该在出勤表中插入 1200 个值(在我看来,您希望在 4 年的时间跨度内每天都有一个条目)

但做类似的事情会是一个更好的主意:

a)如果“离开时间”(与出勤时间相反)中没有条目,则假装员工正在工作和工作,并以某种形式的 callendar 仅显示缺勤/病假天数(php 可以轻松生成类似的内容)

b)假装员工是邪恶的和生病的,只要他没有踩到并自己生成一个考勤条目

对于您的初始问题,如果您坚持使用 1200 个条目......只需使用 for 循环即可:

// mysql-query getting all employees (use PDO, i am old fashioned and still use mysql_ functions)
while( mysql_fetch_array($sql-query-getting-employees) )
{
$insert = "INSERT INTO ATTENDANCE (col1, col2...) VALUES ";
for( $i = 0; $i <= 2000; $++ )
{
if($i != 0) { $insert += ", "; } //append comma for more than 1 value
$insert += "( val1, val2 )";
}
}

然后通过 mysql_query($insert) 插入值

于 2012-08-23T06:56:36.767 回答