循环中不能使用PHP变量

时间:2013-08-24 19:00:41

标签: php variables if-statement while-loop global

我有来自ajax调用的PHP变量。但是,当它们通过if语句和while循环时,它们是未定义的,我该如何解决这个问题。我无法工作。

$userID = $_POST['id'];
$clubID = $_POST['clubID'];
$type = $_POST['type'];
$delAtt = $_POST['delAtt'];

其余代码

if($delAtt == 1)
{
    $result = mysql_query('SELECT * FROM events WHERE clubID = "'.$clubID.'"');

    while ($row == mysql_fetch_assoc($result))
    {

        $eventID = $row['eventID'];
        echo $eventID;

         mysql_query('DELETE FROM eventmember WHERE userID = "'.$userID.'" AND eventID = "'.$eventID.'"');
         mysql_query('DELETE FROM eventmember2 WHERE userID = "'.$userID.'" AND eventID = "'.$eventID.'"');
         mysql_query('DELETE FROM eventmember3 WHERE userID = "'.$userID.'" AND eventID = "'.$eventID.'"');

         mysql_query('DELETE FROM attendance WHERE userID = "'.$userID.'" AND eventID = "'.$eventID.'"');
         mysql_query('DELETE FROM attendance2 WHERE userID = "'.$userID.'" AND eventID = "'.$eventID.'"');
         mysql_query('DELETE FROM attendance3 WHERE userID = "'.$userID.'" AND eventID = "'.$eventID.'"');

     }
}

所有变量都在if语句中工作,但不在while循环中

2 个答案:

答案 0 :(得分:2)

if(isset($_POST['delAtt'])){
    $userID = $_POST['id'];
    $clubID = $_POST['clubID'];
    $type = $_POST['type'];
    $delAtt = $_POST['delAtt'];
}
else{
    $delAtt = 0
}

// Rest of code

正确分配这一行:

while ($row == mysql_fetch_assoc($result)){
            ^

要:

while ($row = mysql_fetch_assoc($result)){

答案 1 :(得分:0)

您可以随时检查POST变量,如下所示:

if (isset($_POST["id"]) && !empty($_POST["id"])) { //TO DO }

因此,您可以更轻松地了解POST变量是否真正发布到接收页面。

以下代码可以显示POST变量数组中的内容。

print_r($_POST);