将包含一些输入字段的动态生成的表保存到mysql表中

时间:2013-06-28 11:00:53

标签: php mysql save

Salary Calculator.php

<html>
<body>

<form name="SalarySaver" method="POST" action="SalarySaverModel.php">
<table>
<tr>
    <td>Period : </td>
    <td><input type="text" name="SYear" id="SYear"/></td>
    <td><select name="SMonth" id="SMonth">
            <option value="January">January</option>
            <option value="February">February</option>
            <option value="March">March</option>
            <option value="April">April</option>
            <option value="May">May</option>
            <option value="June">June</option>
            <option value="July">July</option>
            <option value="August">August</option>
            <option value="September">September</option>
            <option value="October">October</option>
            <option value="November">November</option>
            <option value="December">December</option>
        </select>
    </td>
</tr>
</table>

  <table border="1">
    <tr> 
      <td rowspan="2">Employee Name</td>
      <td rowspan="2">Days</td>
      <td rowspan="2">Wages</td>
      <td rowspan="2">Kg's</td>
      <td rowspan="2">Kg's/Days</td>
      <td rowspan="2">Extra Kg's</td>
      <td rowspan="2">Wage for Extra Kg's</td>
      <td rowspan="2">Total Wages</td>
      <td colspan="3">EPF/ETF</td>
      <td rowspan="2">Advances</td>
      <td rowspan="2">Festival Advances</td>
      <td rowspan="2">Balance Wages</td>
    </tr>
    <tr> 
      <td>8%</td>
      <td>12%</td>
      <td>3%</td>
    </tr>
    <?php include 'SalaryCalculatorModel.php'; ?>
  </table>
  <input name="Save" type="submit" value="Save"><a href="ScrapCalculator.php" target="_self">Scrap Calculator</a>
</form>

</body>
</html>


SalaryCalculatorModel.php

<?php 

include 'dbconnection.php'; 

 $Days=0; $Kg=0.00; $Wage=0.00; 
 $Rate=0.00;  $KgDivDays=0.00; $ExtraKgs = 0.00;
 $WageExtraKgs=0.00; $TotalWages=0.00;
 $BalanceWage=0.00; $EightPerc=0.00; $TwelvePerc=0.00; $ThreePerc=0.00;
 $Advances=0.00; $FestAdvances=0.00;

//execute the SQL query and return records
$result = mysql_query("SELECT EmployeeName,DailyRate FROM employees");


//fetch the data from the database 
$i = 0;
while ($row = mysql_fetch_array($result)) {
    echo "<tr><td>".$row{'EmployeeName'}."</td>";
    $Rate=$row{'DailyRate'};
    //echo "<td>".$Rate."</td>";
    //echo "<td style = 'display:none'>".$row{'DailyRate'}."</td>";
    //echo $Rate;
    echo "<td><input type='text' name='Days' id='Days".$i."' onChange='CalcWage(".$i.",".$Rate.");'/></td>";
    echo "<td id='Wage".$i."'>".$Wage."</td>";
    echo "<td><input type='text' name='Kg' id='Kg".$i."' onChange='CalcAmount(".$i.",".$Rate.");'/></td>";
    echo "<td id='KgDivDays".$i."'>".$KgDivDays."</td>";
    echo "<td id='ExtraKgs".$i."'>".$ExtraKgs."</td>";
    echo "<td id='WageExtraKgs".$i."'>".$WageExtraKgs."</td>";
    echo "<td id='TotalWages".$i."'>".$TotalWages."</td>";
    echo "<td id='EightPerc".$i."'>".$EightPerc."</td>";
    echo "<td id='TwelvePerc".$i."'>".$TwelvePerc."</td>";
    echo "<td id='ThreePerc".$i."'>".$ThreePerc."</td>";
    echo "<td><input type='text' name='Advances' id='Advances".$i."'/></td>";
    echo "<td><input type='text' name='FestAdvances' id='FestAdvances".$i."' onChange='CalcBalanceWage(".$i.",".$Rate.");'/></td>";
    echo "<td id='BalanceWage".$i."'>".$BalanceWage."</td>";
    $i++;
}

?>

<script type="text/javascript">

function CalcWage(i,php_rate){
    var Days=document.getElementById('Days'+i).value;
    //var php_rate = "<?php echo $Rate; ?>";
    //alert(php_rate);
    var Wage=parseFloat(Days*php_rate);
    document.getElementById('Wage'+i).innerHTML= Wage;
    return Wage;
}

function CalcAmount(i,php_rate){
    var Kg=document.getElementById('Kg'+i).value;
    var Days=document.getElementById('Days'+i).value;
    var KgDivDays = parseFloat(Days*4);
    document.getElementById('KgDivDays'+i).innerHTML= KgDivDays;
    var ExtraKgs=Kg-KgDivDays;
    document.getElementById('ExtraKgs'+i).innerHTML= ExtraKgs;
    var WageExtraKgs=ExtraKgs*50;
    document.getElementById('WageExtraKgs'+i).innerHTML= WageExtraKgs;
    var BasicSal = CalcWage(i,php_rate);
    var TotalWages= BasicSal + WageExtraKgs;
    document.getElementById('TotalWages'+i).innerHTML= TotalWages;
    var EightPerc= parseFloat(BasicSal * 0.08);
    document.getElementById('EightPerc'+i).innerHTML= EightPerc;
    var TwelvePerc= parseFloat(BasicSal * 0.12);
    document.getElementById('TwelvePerc'+i).innerHTML= TwelvePerc;
    var ThreePerc= parseFloat(BasicSal * 0.03);
    document.getElementById('ThreePerc'+i).innerHTML= ThreePerc;
    return TotalWages;
}

function CalcBalanceWage(i,php_rate){
    var Advances=parseFloat(document.getElementById('Advances'+i).value);
    var FestAdvances=parseFloat(document.getElementById('FestAdvances'+i).value);
    var TotWage = CalcAmount(i,php_rate); 
    var BasSal =  CalcWage(i,php_rate); 
    var Deductions = parseFloat(Advances+FestAdvances+(BasSal*0.08));
    var BalanceWage=parseFloat(TotWage-Deductions);
    document.getElementById('BalanceWage'+i).innerHTML= BalanceWage;
    return BalanceWage;
}

</script>

<?php
//close the connection
mysql_close($dbhandle);
?>

上面的SalaryCalculatorModel.php代码生成并填充SalaryCalculator.php上的列。

我想使用SalarySaver.php保存渲染页面上的所有信息(输入/计算后)

我的问题是我在Salary Calculator.php上有表单字段以及从SalaryCalculatorModel.php填写的列。如何将动态html表中的值保存到mysql数据库表中?

1 个答案:

答案 0 :(得分:0)

我不知道我是否理解这个问题。您想将此表中的数据插入到mysql db表中吗?   如果是这样你应该

<form method="POST" action="page_where_inserting_in_mysql">
    ...
</form>

进入新页面你应该

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
mysql_query("INSERT INTO table_name (field1, field2, ...) VALUES('".$_POST['value1']."', '".$_POST['value2']."',... ) ") or die(mysql_error()); 

希望它有所帮助。如果您的意思是其他问题,请告诉我。如果可以,我会帮助你的。