这是我第一次使用 PHP,我从 w3school 学习一切。我的问题 :
1 个循环
我有一个座位预订数据库,例如:A1 A2 A3 A4 A5
我尝试做一些循环来减少相同代码的重复但失败了。
2 复选框的未定义索引。
我做了一些复选框,但出现错误:如果我提交表单时未选中某些复选框,则出现未定义索引。我谷歌但我不知道如何用他们的解决方案改变我的编码,因为我不理解他们的解决方案。
下面是我的编码。
<html>
<?php
$connect = mysql_connect("localhost","root","") or die ("Coulnt connect!");
mysql_select_db("book") or die ("Couldnt find db");
$A1 = 'enable';
$A2 = 'enable';
$A3 = 'enable';
$A4 = 'enable';
$A5 = 'enable';
$query = mysql_query("SELECT * FROM seats WHERE Seat_Number = 1 AND Alphabet = 'A'");
$row = mysql_fetch_array($query);
$check = $row['Availability'];
if($check >0)
{
$A1 = 'disabled';
}
$query = mysql_query("SELECT * FROM seats WHERE Seat_Number = 2 AND Alphabet = 'A'");
$row = mysql_fetch_array($query);
$check = $row['Availability'];
if($check >0)
{
$A2 = 'disabled';
}
$query = mysql_query("SELECT * FROM seats WHERE Seat_Number = 3 AND Alphabet = 'A'");
$row = mysql_fetch_array($query);
$check = $row['Availability'];
if($check >0)
{
$A3 = 'disabled';
}
$query = mysql_query("SELECT * FROM seats WHERE Seat_Number = 4 AND Alphabet = 'A'");
$row = mysql_fetch_array($query);
$check = $row['Availability'];
if($check >0)
{
$A4 = 'disabled';
}
$query = mysql_query("SELECT * FROM seats WHERE Seat_Number = 5 AND Alphabet = 'A'");
$row = mysql_fetch_array($query);
$check = $row['Availability'];
if($check >0)
{
$A5 = 'disabled';
}
if(isset($_POST ['submit']))
{
$ch1 = $_POST["ch1"];
$ch2 = $_POST["ch2"];
$ch3 = $_POST["ch3"];
$ch4 = $_POST["ch4"];
$ch5 = $_POST["ch5"];
if(isset($_POST['ch1']))
{
echo 'You have select ch1';
mysql_query("UPDATE seats SET Availability = 1 WHERE Alphabet = 'A' AND Seat_Number = 1");
}
if(isset($_POST['ch2']))
{
echo 'You have select ch2 <br>';
mysql_query("UPDATE seats SET Availability = 1 WHERE Alphabet = 'A' AND Seat_Number = 2");
}
if(isset($_POST['ch3']))
{
echo 'You have select ch3 <br>';
mysql_query("UPDATE seats SET Availability = 1 WHERE Alphabet = 'A' AND Seat_Number = 3");
}
if(isset($_POST['ch4']))
{
echo 'You have select ch4 <br>';
mysql_query("UPDATE seats SET Availability = 1 WHERE Alphabet = 'A' AND Seat_Number = 4");
}
if(isset($_POST['ch5']))
{
echo 'You have select ch5 <br>';
mysql_query("UPDATE seats SET Availability = 1 WHERE Alphabet = 'A' AND Seat_Number = 5");
}
}
?>
<center>
<body>
<form method="post" >
<p>
<input name="ch1" type="checkbox" id="A1" value="A1"<?php echo $A1; ?>/>
<input name="ch2" type="checkbox" id="A2" value="A2"<?php echo $A2; ?>/>
<input name="ch3" type="checkbox" id="A3" value="A3"<?php echo $A3; ?>/>
<input name="ch4" type="checkbox" id="A4" value="A4"<?php echo $A4; ?>/>
<input name="ch5" type="checkbox" id="A5" value="A5"<?php echo $A5; ?>/>
</p>
<input type='submit' name='submit' value='Book Selected !' />
<input name="none" type="reset" value="Clear">
</form>
</body>
</center>
</html>