为什么不执行此echo命令?

时间:2014-11-15 20:10:44

标签: php html mysql forms

请有人帮我理解为什么回音命令,“会员号不正确,请再试一次。”不工作?

其他一切似乎都运转正常。

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'DBNAME');
define('DB_USER', 'USER');
define('DB_PASSWORD', 'PASS');
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db = mysql_select_db(DB_NAME, $con) or die("Failed to connect to MySQL: " . mysql_error());
$mem_no = $_POST['mem_no'];

function SignIn()
{
    session_start();
    if (!empty($_POST['mem_no'])) {
        $query = mysql_query("SELECT * FROM members where mem_no = '$_POST[mem_no]'") or die(mysql_error());
        $row = mysql_fetch_array($query) or die(mysql_error());
        if (!empty($row['mem_no'])) {
            $_SESSION['mem_no'] = $row['mem_no'];
            header("Location: " . $_POST['cat_link']);
        } else {
            echo "Incorrect Membership Number, please try again.";
        } // This line is not executing
    } else {
        echo "Please go back and enter a Membership Number";
    }
}

if (isset($_POST['submit'])) {
    SignIn();
}

HTML表格如下:

<form method="post" action="/check.php">
<p>Membership No.</p>
<input name="mem_no" type="text" id="mem_no">
<input name="cat_link" type="hidden" value="https://www.redirectlink.com">
<input name="submit" type="submit" id="submit" value="AELP Member Rate">
</form>

链接到测试:https://www.eiseverywhere.com/ehome/index.php?eventid=106953&tabid=239372,如果您想测试,则有效的“会员编号”为1234。

将表格留空会给出正确的错误消息,输入有效数字会导致我正确重定向,但输入无效的数字(例如9999)并不能给我正确的输出信息。

提前感谢您的回复。

此致 灰

4 个答案:

答案 0 :(得分:0)

您需要对行进行计数,因为即使sql查询没有结果,它也不是空的。算一算吧。

function SignIn()
{
    session_start();
    if (!empty($_POST['mem_no'])) {
        $query = mysql_query("SELECT * FROM members where mem_no = '". $_POST['mem_no'] ."'") or die(mysql_error());

        #count rows
        $count = mysql_num_rows($query);
        $row = mysql_fetch_array($query) or die(mysql_error());

        #check count
        if ($count != 0) {
            $_SESSION['mem_no'] = $row['mem_no'];
            header("Location: " . $_POST['cat_link']);
        } else {
            echo "Incorrect Membership Number, please try again.";
        } 

    } else {
        echo "Please go back and enter a Membership Number";
    }
}

只是一个小提醒。 PHP中的mysql_类已弃用,将在下一版本中删除,我建议您使用mysqli_或使用PDO&#39;

答案 1 :(得分:0)

您的脚本很难调试,原因如下:

  1. 它没有缩进(我在问题中为你解决了这个问题)
  2. 您正在使用已弃用的mysql函数(请参阅this)。切换到PDO或mysqli。
  3. 您的脚本中有几个退出点。每当你打电话给数据库时,你就会死掉#34;如果你失败了那不好
  4. 无论如何,我怀疑其中一个&#34;死了&#34;让你的脚本过早结束。而不是使用骰子,自己处理错误。

答案 2 :(得分:0)

我认为这应该有效:

(还将错误报告放在顶部!)

<?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
?>

<?php
    session_start();

    define('DB_HOST', 'localhost');
    define('DB_NAME', 'DBNAME');
    define('DB_USER', 'USER');
    define('DB_PASSWORD', 'PASS');

    $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
    //$db = mysql_select_db(DB_NAME, $con) or die("Failed to connect to MySQL: " . mysql_error()); //Useless

    $mem_no = $_POST['mem_no'];

    function SignIn() {

        if (!empty($_POST['mem_no'])) {
            $query = mysql_query("SELECT * FROM members WHERE mem_no = '" . $_POST['mem_no'] . "'") or die(mysql_error());
            $row = mysql_fetch_array($query) or die(mysql_error());
            $count = mysql_num_rows($query);

            if ($count >= 1) {
                $_SESSION['mem_no'] = $row['mem_no'];
                header("Location: " . $_POST['cat_link']);
            } else {
                echo "Incorrect Membership Number, please try again.";
            } // This line is not executing
        } else {
            echo "Please go back and enter a Membership Number";
        }
    }

    if (isset($_POST['submit'])) {
        SignIn();
    }
?>

答案 3 :(得分:-2)

将两个else语句连续放入是没有意义的。从内部if块

中取出最后一个else块
   if(!empty($_POST['mem_no'])) {
        //code

         }else{

             echo "Please go back and enter a Membership Number";
         } 

那应该执行得很好