如何显示多个mysql查询的单个警报消息?

时间:2013-12-23 05:49:22

标签: javascript php mysql

我有一点疑问..我在这里发布了我的delete.php页面编码。

if(isset($_GET["id"]))
{
    $meal_query = mysql_query("DELETE FROM ebmealplans WHERE MealPlanID = '$id'");
    echo mysql_error();
    $room_query = mysql_query("DELETE FROM ebroomtypes WHERE RoomTypeID = '$id'");
    echo mysql_error();
    $tariff_query = mysql_query("DELETE FROM ebvouchertariffs WHERE VoucherID_Fk = '$id'");
    echo mysql_error();
    $query = mysql_query("DELETE FROM ebvouchers WHERE VoucherID = '$id'");
    echo mysql_error();
    if($query)
    {
        echo "<script> alert('Voucher deleted successfully'); </script>";
    }
    else
    {
        echo "<script> alert('Failed to delete this voucher'); </script>";
    }
    mysql_close($link);
    echo "<script> location.href='managevouchers.php'; </script>";
}

这里我使用这个php编码删除一些用户数据。它工作得很好。我创建了四个单独的表来存储记录。如果删除功能成功完成,我想向用户显示警告消息&#34;成功删除&#34;。您可以在我的编码中看到我只显示一个$query的提醒消息。我尝试了另一种方法..

if($query)
{
 alert function
}
else
{
 alert function
}
if($meal_query)
{
 alert function
}
else
{
 alert function
}
if($room_query)
{
 alert function
}
else
{
 alert function
}
if($tariff_query)
{
 alert function
}
else
{
 alert function
}

它会显示四次警报信息。我知道多个警报功能让用户烦恼。我的问题是如何显示mysql多个查询的单个警报消息?

1 个答案:

答案 0 :(得分:1)

只需将msg片段存储在某个变量中,并最终提醒所有人。

$msgs = array ();

if ($query) {
    $msgs [] = '.....';
} else {
    $msgs [] = '...';
}

if ($meal_query) {
    $msgs [] = '....';
} else {
    $msgs [] = '...';
}
//....


if ($msgs) {
    //join the msgs with line break
    $alert = join ( "\n", $msgs );

    //json encode will make sure it's like "..string..", no quotes problem
    echo '<script> alert(', json_encode ( $alert ), '); </script>';
}