循环和未定义的索引php

时间:2012-04-05 15:43:35

标签: php loops checkbox

这是我第一次使用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 个答案:

答案 0 :(得分: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查询中的座位号。

答案 1 :(得分:0)

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

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

它无论如何都没有用 - 你从不在代码中的任何地方使用变量$ch1等。

如果你确实想要保留变量,你可以将各行移动到相关的if (isset())块 - 如果这样做,你就赢了;在你拥有之前尝试使用$_POST键检查它们是否已设定。

答案 2 :(得分:0)

我认为问题是你要检查一个post变量,你需要将它放入一个isset或类似的东西

e.g。 $ ch1 = null; 如果(isset($ _ POST [ 'CH1']){ $ ch1 = $ _POST ['ch1']; }

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

你需要为每个_POST

答案 3 :(得分: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';
    }
}
?>