else语句不显示数据

时间:2010-09-23 11:37:18

标签: php

我有一些代码在提交数据之前检查数据库中的重复项,但是我的语句不起作用。如果找到重复项,则会向表单返回一条消息。但是,它不起作用,我确信它与文件底部附近的其他语句不正确有关。假设所有连接都正常。错误在哪里?非常感谢

<?php
$array = split('[,]', $_POST['fileno']);

if (isset($_POST['submit'])) {
    foreach ($array as $fileno) {
        if ($fileno == '' && $box == '')
            {
                echo '<div style="background-color:#ffa; padding:2px; color:#ff0000;font-size:12px;font-weight:normal">' . 'You must incude a box and a file' . '</div>';

            }
        elseif ($fileno == '')
            {

                echo '<div style="background-color:#ffa; padding:2px; color:#ff0000;font-size:12px;font-weight:normal">' . 'You must enter a file reference' . '</div>';
            }
        elseif ($box == '')
            {
                //echo error;
                echo '<div style="background-color:#ffa; padding:2px; color:#ff0000;font-size:12px;font-weight:normal">' . 'You must enter a box' . '</div>';
            }
        else
            {
                $sql = "SELECT custref FROM files WHERE custref = '$fileno' ";
                $result = runSQL($sql) or die(mysql_error());
                if (mysql_num_rows($result)>0){
                    echo '<div style="background-color:#ffa; padding:2px; color:#ff0000;font-size:12px;font-weight:normal">' . $fileno . '  is already in the database. No duplicates' . '</div>';

                }
                $sql = "SELECT custref FROM boxes WHERE custref = '$box' ";
                $result = runSQL($sql) or die(mysql_error());
                if (mysql_num_rows($result)>0){
                    echo '<div style="background-color:#ffa; padding:2px; color:#ff0000;font-size:12px;font-weight:normal">' . $box . '  is already in the database. No duplicates' . '</div>';
                }

                else
                    {
                        //insert into db;
                        echo '<div style="background-color:#ffa; padding:2px; color:#33CC33;font-size:12px;font-weight:normal">' . $fileno . "Box: " . $box . $authorised . 'Successfull' . '</div>';
                        $sql = "INSERT INTO `files` (customer, authorisation, boxstatus, boxref, custref, filestatus) VALUES ('$customer', '$authorised', '$boxstatus', '$box', '$fileno', $filestatus)";
                        $result = runSQL($sql) or die(mysql_error());
                        //echo 'This record is valid';
                        //header("Location: http://localhost/sample/admin/files/index.php");
                        //exit();
                    }
            }
    }
}
?>

3 个答案:

答案 0 :(得分:0)

请检查是否出现在本节中: -

// Some logic
if (...) {
    // some more logic
}
...
elseif ($box == '') {
    //echo error;
    echo '<div style="background-color:#ffa; padding:2px; color:#ff0000;font-size:12px;font-weight:normal">' . 'You must enter a box' . '</div>';
}
else {
    echo 'check it here please';
    // some more logic
}
...

希望它有所帮助。

答案 1 :(得分:0)

放一些回声来检查值:

foreach ($array as $fileno) {
    echo "fileno='$fileno' , box='$box'<br>";
  ......

然后在if / elseif

的每个块中
echo "i'm running line : ",__LINE__,"<br>";

你获得了什么?

答案 2 :(得分:0)

你应该尝试使用exit(); / die();在您的回显错误中(不是错误处理的最佳方式! - try exceptions out)而不是将依赖代码封装在else中。我怀疑这可能会解决您的问题,因为您只是在针对您的一个错误陈述添加行。