请有人帮我理解为什么回音命令,“会员号不正确,请再试一次。”不工作?
其他一切似乎都运转正常。
<?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)并不能给我正确的输出信息。
提前感谢您的回复。
此致 灰
答案 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)
您的脚本很难调试,原因如下:
答案 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";
}
那应该执行得很好