我正在php表中显示来自数据库(dataTable)的一堆数据。表格标题为
Serial| Date | Voltage | Current | Select
每行都有一个选择按钮,如果用户按下一个选择按钮(假设该按钮对应于序列号10,则接下来的10个值(序列号10到20)将用于平均电流和电压计算。
如果该值令人满意,则用户可以按发送按钮。我想将用于计算的10个值(所有数据,如串行,电压和电流)发送到另一个数据库表(calcData)。
我管理了数据显示,选择和平均计算。如何发送此数据?我应该将数据保存在php数组中,然后发送该数组吗?有什么建议吗?
include ("DBconnect.php");
$conn= mysqli_connect( $dbhost, $dbuser, $dbpass, $db );
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Voltage, Current, Date From DataTable' ORDER BY Date DESC";
$result = $conn->query($sql);
$i=1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<form action = '' method =get >";
echo '<tr>';
echo"<td>" . $i. "</td>";
echo"<td>" . $row["Date"]. "</td>";
echo"<td>". $row["Voltage"] . "</td>";
echo"<td>". $row["Current"]. "</td>";
// I will use the time value of the row the corresponding to the row where user pressed the select button to select next 10 values for avg calculation.
echo "<td>".'<input type="hidden" name="Time" value="' . $row["Date"] . '" />'."</td>";
echo "<td>".'<input type=submit name=serial value=select id=button1 class = classSubmit />'."</td>";
echo '</tr>';
$i++;
echo "</form>";
}
echo "</table>";
} else { echo "0 results"; echo "<br>";}
$conn->close();
平均计算
if(isset ($_GET['serial']){
$selectedTime =$_GET['Time'];
$conn= mysqli_connect( $dbhost, $dbuser, $dbpass, $db );
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Date, Voltage, Current dataTable WHERE Date <= '$selectedTime' ORDER BY Date DESC Limit 10";
$result_10_values = $conn->query($sql);
$i=1;
$avgCurrent=0;
$avgVoltage=0;
//code to select 10 values
if ($result_10_values->num_rows > 0) {
while($row = $result_10_values->fetch_assoc()) {
$avg1Current += $row["Current"];
$avgVoltage += $row["Voltage"];
$i++;
}
$conn->close();
} else { echo "0 results"; }
}
//how to save these 10 rows data and send to another table when user press send button