如何在 mssql 中成功或不删除数据后显示警报框

我想在 MSSQL 中成功或不删除数据后显示 JavaScript 警报。这该怎么做?我已经编写了这段代码,但它message=success每次只显示部分警报,即使由于“与引用冲突(foreign_key)”之类的错误而导致删除不起作用,因此当我单击此链接时。


 echo "<a class='activater'  href='ma_QualiOverviewloeschen.php?TestaufstellungID=".$row['TestaufstellungID'] ."&QualiID=".$row['QualiID'] ."' title='Qualitest l&ouml;schen' data-toggle='tooltip' onclick='confirm_delete()'>   <span class='glyphicon glyphicon-trash'></span></a>"; 

它调用以下处理 SQL 部分的 php 页面:


$QualiDelete =("DELETE FROM MyDB.dbo.Testaufstellung WHERE MyDB.dbo.Testaufstellung.TestaufstellungID = :TestaufstellungID");

$QualiDelete .=("DELETE FROM MyDB.dbo.AllgemeineAngaben WHERE MyDB.dbo.AllgemeineAngaben.QualiID = :QualiID");


$sth = $connection->prepare($QualiDelete);

$sth->execute(array(':TestaufstellungID' => $TestaufstellungID, ':QualiID:' => $QualiID));


                if($sth)

                {

                    header("location: ma_QualiOverview.php?message=success");   

                }

                else

                {

                    echo sqlsrv_errors();

                    header("location: ma_QualiOverview.php?message=failed");    

                }

                $connection = null;

返回单击链接的主页,以下ifelse考虑message应该向我显示正确的警报。


<?php

if($_GET['message']=='success'){

    echo '<script language="javascript">';

    echo 'alert("Erfolgreich gelöscht.");';

    echo '</script>';

} elseif($_GET['message']=='failed'){

    echo '<script language="javascript">';

    echo 'alert("Nicht gelöscht, da Quali "ongoing" ist.");';

    echo '</script>';

}

?>

我想念什么?


动漫人物
浏览 89回答 1
1回答

MMMHUHU

$sth永远不会falsy,您必须检查的返回值$sth->execute 另外,您应该在发送标头后回显错误。由于$sth始终是定义的,所以总是得到成功的结果,修改后的代码看这里$QualiDelete =("DELETE FROM MyDB.dbo.Testaufstellung WHERE MyDB.dbo.Testaufstellung.TestaufstellungID = :TestaufstellungID");$QualiDelete .=("DELETE FROM MyDB.dbo.AllgemeineAngaben WHERE MyDB.dbo.AllgemeineAngaben.QualiID = :QualiID");$sth = $connection->prepare($QualiDelete);//Check the value returned instead of $sth$result = $sth->execute(array(':TestaufstellungID' => $TestaufstellungID, ':QualiID:' => $QualiID));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($result )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header("location: ma_QualiOverview.php?message=success");&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header("location: ma_QualiOverview.php?message=failed");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo sqlsrv_errors();//Echo must be after header&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $connection = null;
打开App,查看更多内容
随时随地看视频慕课网APP