为什么这种情况不起作用?

时间:2010-06-12 09:40:58

标签: php

我注意到第一个条件不起作用

if (empty($ss)) {
  echo "please write your search words";
}

但是第二个

else if ($num < 1)   {
   echo  "not found any like ";

完整代码:

<?php
require_once "conf.php";
$ss= $_POST["ss"];
$sql2=("SELECT * FROM student WHERE snum = $ss");
$rs2 = mysql_query($sql2) or die(mysql_error());
$num = mysql_num_rows($rs2);

if (empty($ss)) {
    echo "please write your search words";
}
else if ($num < 1 ) {
    echo  "not found any like ";
}else {
    $sql=("SELECT * FROM student WHERE snum = $ss ");
    $rs = mysql_query($sql) or die(mysql_error());

    while($data=mysql_fetch_array($rs)) {
?>
<div id="name">
    <table align="center"  border="3" bgcolor="#FF6666">
        <tr>
            <td><? echo $data ["sname"]." "."نتيجة الطالب"; ?></td>
        </tr>
    </table>
</div>
<div id="ahmed">
    <table width="50%" height="50" align="center" border="2px" bgcolor="#BCD5F8">
        <tr>
            <td width="18%"><strong>جيولوجي</strong></td>
            <td width="13%"><strong>تاريخ</strong></td>
            <td width="13%"><strong>رياضة</strong></td>
            <td width="14%"><strong>عربي</strong></td>
            <td width="12%"><strong>علوم</strong></td>
            <td width="30%"><strong>المادة</strong></td>
        </tr>
        <tr>
            <td>100</td>
            <td>100</td>
            <td>100</td>
            <td>100</td>
            <td>100</td>
            <td><strong>الدرجة النهائية</strong></td>
        </tr>
        <td><? echo $data['geo']; ?></td>
        <td><? echo $data['snum']; ?></td>
        <td><? echo $data['math']; ?></td>
        <td><? echo $data['arab']; ?></td>
        <td><? echo $data['history']; ?></td>
        <td><strong>درجات الطالب</strong></td>
        </tr>
        <tr>
            <td colspan="5" align="center" valign="middle">
                        <? $sum= $data['geo'] + $data['snum'] +
                            $data['math'] + $data['arab'] +
                            $data['history'];
                        echo $sum ;
                        ?>
            </td>
            <td><strong>مجموع الدرجات</strong></td>
        </tr>
        <tr>
            <td colspan="5" align="center">
                        <?
                        $all=500 ;
                        $sum= $data['geo'] + $data['snum'] +
                            $data['math'] + $data['arab'] +
                            $data['history'];
                        $av=$sum/$all*100 ;
                        echo $av."%" ;
                        ?>
            </td>
            <td><strong>
                    النسبة المئوية
                </strong></td>
        </tr>
    </table>

</tr>
</div>
        <?
    }
};

完整的代码链接是:

http://www.mediafire.com/?2d4yzdjiym0

4 个答案:

答案 0 :(得分:1)

使用

if (!isset($ss)) {
    echo "please write your search words";
}

而不是

if (empty($ss)) {
    echo "please write your search words";
}

答案 1 :(得分:1)

你确定$ ss isset?即有一个名为ss的POST变量。你可以测试一下。

if (empty($ss) || !isset($ss)) {
    echo "please write your search words";
}

或在阅读后变量

时测试它
if ((!isset($_POST["ss"]) || (!is_numeric($_POST["ss"])))
{
    $ss = 0;//empty considers 0 as been empty
}
else
{
    $ss = $_POST["ss"];
}

ss看起来也是一个数字,因此使用is_numeric检查它是否为数字将有助于阻止SQL错误,如果由于某种原因它不是。

答案 2 :(得分:0)

尝试将条件更改为

if(empty($_POST['ss'])){ ... }

并继续使用$ _POST ['ss']代替$ ss。

BTW开发时始终使用error_reporting(E_ALL)。

答案 3 :(得分:0)

$ sql =(“SELECT * FROM student WHERE snum = $ ss”);

删除包含查询的变量($ sql1和$ sql2)中的括号

应该是

$ sql =“SELECT * FROM student WHERE snum = $ ss”;

我认为这就是为什么它告诉你附近有错误的原因

相关问题