今天写一个程序的时候遇到一个很有意思的问题,这个和php独特的语法有关,首先我们看一下代码是怎么写的。 ? php $db = mysql_connect ('localhost','root','Ctrip07185419') or die ('can not connect to databas' ); mysql_select_db ('moviesite', $db )
今天写一个程序的时候遇到一个很有意思的问题,这个和php独特的语法有关,首先我们看一下代码是怎么写的。
<span>php </span><span>$db</span> = <span>mysql_connect</span>('localhost','root','Ctrip07185419') or <span>die</span>('can not connect to databas'<span>); </span><span>mysql_select_db</span>('moviesite',<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>)); </span><span>if</span>(!<span>isset</span>(<span>$_GET</span>['do']) || <span>$_GET</span>['do'] != 1<span>) { </span><span>switch</span>(<span>$_GET</span>['type'<span>]) { </span><span>case</span> 'movie': <span>echo</span> 'Are you sure you want to delete this movie?<br>'<span>; </span><span>break</span><span>; </span><span>case</span> 'people': <span>echo</span> 'Are you sure you want to delete this people?<br>'<span>; </span><span>default</span>: <span>break</span><span>; } </span><span>echo</span> '<a href="'.<span>%24_SERVER</span>%5B'REQUEST_URI'%5D.'&do=1">Yes</a> or <a href="admin.php">no</a>'<span>; } </span><span>else</span><span> { </span><span>switch</span>(<span>$_GET</span>['type'<span>]) { </span><span>case</span> 'people': <span>$query</span> = ' update people set movie_leadactor = 0 where movie_leadactor = '.<span>$_GET</span>['id'<span>]; </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>)); </span><span>$query</span> = ' delete from people where people_id='.<span>$_GET</span>['id'<span>]; </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>)); </span>?> <p style="text-align:'center'">Your person has been deleted</p> <a href="admin.php"><span>Return</span> to Index</a> <span>php </span><span>break</span><span>; </span><span>case</span> 'movie': <span>$query</span> = 'delete from movie where movie_id='.<span>$_GET</span>['id'<span>]; </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span> (<span>mysql_error</span>(<span>$db</span><span>)); </span>?> <p style="text-align:'center'">Your movie has been deleted</p> <a href="admin.php"><span>Return</span> to Index</a> <span>php </span><span>break</span><span>; </span><span>default</span>: <span>break</span><span>; } } </span>?>
注意这里的switc语句,在删除movie或people之后我们要输出成功信息并返回列表页面,一般情况下我们习惯把break语句紧跟在switch下面的主体部分,于是开始的时候我写成这样了
<span>else</span><span> { </span><span>switch</span>(<span>$_GET</span>['type'<span>]) { </span><span>case</span> 'people': <span>$query</span> = ' update people set movie_leadactor = 0 where movie_leadactor = '.<span>$_GET</span>['id'<span>]; </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>)); </span><span>$query</span> = ' delete from people where people_id='.<span>$_GET</span>['id'<span>]; </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span> , <span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>)); </span><span>break</span><span>; </span>?> <p style="text-align:'center'">Your person has been deleted</p> <a href="admin.php"><span>Return</span> to Index</a>
把break放在了case下面的语句下面,这样会导致不能输出删除成功信息,
Your pers
Return to Index
这两句是可以当成case语句的一部分的,如果把它放在外面将永远也不会执行,所以要break要放在下一个case的开头,将上面两行html片段放在case语句中,这样就可以正常显示删除成功的信息了。
php面条语句的特性在这里表现的淋漓尽致。