我遇到的问题是页面刷新的速度比执行查询的速度快。
我使用UPDATE查询在我的字段中使用某些值更新我的数据库。我在一个按钮下得到了这个。但是,当我按下此按钮(这将刷新我的网站)时,它仍会在我的字段中显示旧值。
当我再次刷新页面时,它会显示我填写的值。我添加了一张图片以便更好地解释。
我执行查询以在我的php代码中立即获取表的信息:
$dates = getStartAndEndTime($currentWeek, $currentYear);
$Query = "SELECT * FROM Productielijn1 WHERE Datum BETWEEN ? AND ? ORDER BY Datum";
if($stmt = $dbCon->prepare($Query))
{
$stmt->bind_param('ss', $dates[0], $dates[1]);
if( $stmt->execute() === TRUE)
{
$Result = $stmt->get_result();
while($row = $Result->fetch_assoc())
{
$ArrayGegevens[] = $row;
}
}
}
当我按下按钮时,我这样做:
//First edit button
if(isset($_POST['btnEdit1']))
{
$date = strip_tags($_POST['Datum1']);
$tempo = strip_tags($_POST['Tempo1']);
$proddescrip = strip_tags($_POST['Productomshrijving1']);
$amount = strip_tags($_POST['Aantal1']);
$displayDate = date("d-m-Y", strtotime($date));
$Query = "UPDATE productielijn1 SET Tempo= ?, Productomschrijving= ?, Aantal= ? WHERE Datum = ?";
if($stmt = $dbCon->prepare($Query))
{
$stmt->bind_param('isis', $tempo, $proddescrip, $amount, $date);
if( $stmt->execute() === TRUE)
{
$Message2 = "De waardes van " . $displayDate ." zijn aangepast.";
$MessageColor2 = "green";
}
else
{
$Message2 = "Het aanpassen is niet gelukt!";
$MessageColor2 = "red";
}
}
}
我像这样创建我的桌子:
<table class ="PlanningTable">
<tr>
<th colspan='5'>Planning aanpassen voor huidige week (<?php echo $currentWeek ?>)</th>
</tr>
<tr>
<th>Datum</th>
<th>Tempo</th>
<th>Product omschrijving</th>
<th>Aantal/Volume</th>
<th>Aanpassen</th>
</tr>
<?php
if (count($ArrayGegevens) != 0)
{
for ($k = 1; $k < count($ArrayGegevens)+1; $k++)
{
echo "<tr>";
echo "<td><input type='date' name='Datum".$k."' value='".$ArrayGegevens[$k-1]['Datum']."'readonly></td>";
echo "<td><input type='number' name='Tempo".$k."' value= '" .$ArrayGegevens[$k-1]['Tempo'] . "'></td>";
echo "<td><input type='text' name='Productomshrijving".$k."' value ='". $ArrayGegevens[$k-1]['Productomschrijving'] ."'></td>";
echo "<td><input type='number' name='Aantal".$k."' value='" . $ArrayGegevens[$k-1]['Aantal'] . "'></td>";
echo "<td><input type='submit' name='btnEdit".$k."' value='Aanpassen'></td>";
echo "</tr>";
}
}
else
{
echo"<tr>";
echo "<td colspan='5' style='color: red;'>Voor deze week is er nog geen planning!</td>";
echo "</tr>";
}
?>
<tr>
<td colspan=5><p class="Message" style='color: <?php echo $MessageColor2 ?>;'><?php echo $Message2 ?></p></td>
</tr>
</table>
那么有没有办法立即更新我的html值?
答案 0 :(得分:-1)
将您的代码更改为:
<!-- language: lang-html -->
if(isset($_POST['btnEdit1']))
{
$date = strip_tags($_POST['Datum1']);
$tempo = strip_tags($_POST['Tempo1']);
$proddescrip = strip_tags($_POST['Productomshrijving1']);
$amount = strip_tags($_POST['Aantal1']);
$displayDate = date("d-m-Y", strtotime($date));
$Query = "UPDATE productielijn1 SET Tempo= ?, Productomschrijving= ?, Aantal= ? WHERE Datum = ?";
if($stmt = $dbCon->prepare($Query))
{
$stmt->bind_param('isis', $tempo, $proddescrip, $amount, $date);
if( $stmt->execute() === TRUE)
{
$Message2 = "De waardes van " . $displayDate ." zijn aangepast.";
$MessageColor2 = "green";
}
else
{
$Message2 = "Het aanpassen is niet gelukt!";
$MessageColor2 = "red";
}
}
echo "<script>
setTimeout(function(){
location.reload();
}, 300);
</script>";
}
添加js代码。