当我运行此php代码时,计数器变量仅变为1和2,但我想使其变为5,这是怎么回事,有人可以帮助我吗?为什么反剂量从2上升?
<?php
session_start();
include "sql.php";
$date = $_POST['date'];
$email = $_SESSION[email];
$counter = 0;
$check = " select * from reservations where date = '$date'";
$result = mysqli_query($con, $check);
$num = mysqli_num_rows($result);
if($num = 1){
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
echo $counter;
if ($counter < 5){
$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
}
else{
echo "no tables available";
}
}
else{
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
}
答案 0 :(得分:1)
我认为您应该将其循环播放。
检查下面的代码:
<?php
session_start();
include "sql.php";
$date = $_POST['date'];
$email = $_SESSION[email];
$counter = 0;
$check = " select * from reservations where date = '$date'";
$result = mysqli_query($con, $check);
$num = mysqli_num_rows($result);
if($num = 1){
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
echo $counter;
if ($counter < 5){
while($counter <= 5){
//$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
$counter++;
}
}
else{
echo "no tables available";
}
}
else{
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
}