1

这是我第一次使用 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>
4

4 回答 4

1

这里有一些关于循环和函数的帮助:

    $A1 = 'enable';
    $A2 = 'enable';
    $A3 = 'enable';
    $A4 = 'enable';
    $A5 = 'enable';

    $seats = array(1,2,3,4,5);

    function check_availability($seat)
    {
        $query = mysql_query("SELECT * FROM seats WHERE Seat_Number = $seat AND Alphabet = 'A'");
        $row = mysql_fetch_array($query);
        $check = $row['Availability'];

        if($check >0)
        {
            return 'disabled';
        }
        else{
            return 'available';
        }

    }

此功能的作用是为您检查座位并相应地返回禁用或可用。我们可以这样称呼它:

foreach($seats as $seat){
         ${'A'.$seat} = check_availability($seat);
    }

此代码将函数的返回值分配给 $A1、$A2 等......这里是 POST 的循环:

if(isset($_POST ['submit']))
    {
        for($e=1 ; $e <=5 ; $e++){
            if(isset($_POST['ch'.$e])) 
            {
                echo 'You have select ch'.$e;
                mysql_query("UPDATE seats SET Availability = 1 WHERE Alphabet = 'A' AND Seat_Number = $e");
            }

        }
    }

基本上我们重复检查 isset() 5 次 - var $e 从 1 开始,然后比较它是否仍然小于或等于 5,如果不是则增加 1

同时我们使用 $e 来更改 mysql 查询中的座位号。

于 2012-04-05T16:08:33.817 回答
0

删除错误所需要做的就是删除此块:

    $ch1 = $_POST["ch1"];
    $ch2 = $_POST["ch2"];
    $ch3 = $_POST["ch3"];
    $ch4 = $_POST["ch4"];
    $ch5 = $_POST["ch5"];

无论如何,它并没有做任何有用的事情——你永远不会$ch1在代码中的任何地方使用变量等。

如果您确实想保留变量,您可以将各个行移动到相关if (isset())块 - 如果您这样做,您将不会在检查它们是否已设置之前尝试使用这些$_POST键。

于 2012-04-05T15:47:30.733 回答
0

我认为问题在于您检查 post 变量,您需要将其放入 isset 或类似的东西

例如 $ch1 = null; if(isset($_POST['ch1']){ $ch1 = $_POST['ch1']; }

这样它就不会尝试访问数组的 ch1 部分,如果它没有设置抛出未定义的索引异常。希望解释也有助于了解您哪里出错了。

你需要为每个 _POST 做

于 2012-04-05T15:49:52.250 回答
0

复选框的未定义索引可以通过简单地使用来消除

$ch1 = isset($_POST["ch1"])?$_POST['ch1']:null;
$ch2 = isset($_POST["ch2"])?$_POST['ch2']:null;
$ch3 = isset($_POST["ch3"])?$_POST['ch3']:null;
$ch4 = isset($_POST["ch4"])?$_POST['ch4']:null;
$ch5 = isset($_POST["ch5"])?$_POST['ch5']:null;

而不是使用

$ch1 = $_POST["ch1"];
$ch2 = $_POST["ch2"];
$ch3 = $_POST["ch3"];
$ch4 = $_POST["ch4"];
$ch5 = $_POST["ch5"];

对于下一个关于循环的查询,您可以使用

<?php
$A[1] = 'enable';
$A[2] = 'enable';
$A[3] = 'enable';
$A[4] = 'enable';
$A[5] = 'enable';
for ($i=1, $i<=5, $i++) {
 $query = mysql_query("SELECT * FROM seats WHERE Seat_Number = ". $i ." AND Alphabet = 'A'");
 $row = mysql_fetch_array($query);
 $check = $row['Availability'];

    if($check >0)
    {
        $A[$i] = 'disabled';
    }
}
?>
于 2012-04-05T16:30:27.580 回答