简体   繁体   English

为什么不执行此echo命令?

[英]Why isn't this echo command being executed?

Please could someone help me understand why the echo command, 'Incorrect Membership Number, please try again.' 请有人帮我理解为什么echo命令“会员编号不正确,请重试”。 isn't working? 不工作吗

Everything else seems to be functioning okay. 其他一切似乎都正常。

<?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 as follows: 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>

Link to test: https://www.eiseverywhere.com/ehome/index.php?eventid=106953&tabid=239372 and a valid 'Membership Number' is 1234 if you wish to test. 链接到测试: https : //www.eiseverywhere.com/ehome/index.php?eventid= 106953&tabid=239372,如果您想测试,则有效的“会员编号”为1234。

Leaving the form blank does give the correct error message and entering a valid number does redirect me correctly, but inputting an invalid number (9999 for eg) doesn't give me the correct output message. 将表单保留为空白不会给出正确的错误消息,而输入有效的数字也可以正确地重定向我,但是输入无效的数字(例如9999)不会给出正确的输出消息。

Thank you in advance for any responses. 预先感谢您的任何答复。

Regards, Ash 问候,灰

You need to count rows, because even when a sql query has no results it is not empty. 您需要对行进行计数,因为即使SQL查询没有结果,它也不为空。 So count it. 所以算吧。

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";
    }
}

Just a small reminder. 只是一个小提醒。 mysql_ class in PHP is deprecated and will be removed in the next versions, I suggest you going to use mysqli_ or work with PDO 's PHP中的mysql_类已被弃用,在下一版本中将被删除,我建议您使用mysqli_或与PDO一起使用

Your script is hard to debug for several reasons: 您的脚本难以调试有几个原因:

  1. It's unindented (I've fixed that for you in the question) 它是不缩进的(我已为您解决了该问题)
  2. You're using deprecated mysql functions (see this ). 您正在使用不推荐使用的mysql函数(请参阅参考资料 )。 Switch to PDO or mysqli. 切换到PDO或mysqli。
  3. You have several exit points in your script. 您的脚本中有几个出口点。 Everytime you make a call to the database, you "die" if you fail. 每次调用数据库时,如果失败,都将“死亡”。 that's not good 这不好

regardless, I suspect that one of those "die" making your script end prematurely. 无论如何,我怀疑其中一个“死”使您的脚本过早结束。 Instead of using die, handle the errors yourself. 不用自己死,而要自己处理错误。

I think this should work: 我认为这应该工作:

(Also put error reporting at the top!) (也将错误报告放在顶部!)

<?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();
    }
?>

It doesnt make sense to put two else statements in a row. 连续放置两个其他语句是没有意义的。 Take the last else block out of the inner if block 从内部if块中取出最后一个else块

   if(!empty($_POST['mem_no'])) {
        //code

         }else{

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

That should execute just fine 那应该执行得很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM