【问题标题】:how to show the single alert message for multiple mysql queries?如何显示多个 mysql 查询的单个警报消息?
【发布时间】:2014-01-11 07:48:14
【问题描述】:

我有一点疑问..我在这里发布了我的 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 编码删除一些用户数据。它工作得很好。我创建了四个单独的表来存储记录。如果删除功能成功完成,我想向用户显示“已成功删除”的警报消息。您可以在我的编码中看到我只显示一个$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 多个查询显示单个警报消息?

【问题讨论】:

    标签: javascript php mysql


    【解决方案1】:

    只需将味精片段存储在某个变量中,并最终提醒它们。

    $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>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      相关资源
      最近更新 更多