可能重复:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
我正在尝试使用mysqli和PHP连接到MySQL服务器。这是我的代码1-index.html 2-db.php和3-logedin.php
enter code here
<form id="myForm" action="PHP/logedin.php" method="POST">
<div class="single-field">
<label for="eMail">Email:<span class="Star"> *</span></label>
<input name="eMail" type="text"></input>
</div>
<div class="single-field">
<label for="psword">Password:</label>
<input name="psword" type="text"></input>
</div>
<button type="button" name="" value="" class="css3button">Login</button>
</form>
和db.php
<?php
$DB_NAME = 'tblTest';
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL:" . $mysqli->connect_error;
exit();
}
?>
最后logedin.php如下:
<?php
include_once('db.php');
$eMail = mysql_real_escape_string($_POST["eMail"]);
$passWord = mysql_real_escape_string(md5($_POST["psword"]));
$query = "SELECT count(*) FROM users WHERE (email ='$eMail' AND pass ='$passWord')";
if( $query>0 ) {
echo "Yes Your Area Welcome.";
}
else{
echo "No Your Area Not Welcome!.";
}
$mysqli->close();
?>
我有一个名为users的表,我有两列电子邮件并通过那里,但在运行代码后,我收到此消息: 注意:未定义的索引:第3行的C:\ wamp \ www \ tt \ PHP \ logedin.php中的电子邮件 并注意:未定义的索引:第4行的C:\ wamp \ www \ tt \ PHP \ logedin.php中的psword 你能告诉我我做错了吗?
答案 0 :(得分:1)
尝试使用三元:
$eMail = (!empty($_POST['eMail'])) ? mysql_real_escape_string($_POST['eMail']) : null;
$passWord = (!empty($_POST['psword'])) ? mysql_real_escape_string(md5($_POST['psword'])): null;
警告消失了,应该在像
这样的条件中使用if(NULL !== $eMail) // eMail present
您还可以使用以下命令调试帖子变量:
echo '<pre>', print_r($_POST, TRUE), '</pre>'; exit();
答案 1 :(得分:1)
他们只是注意到那些变量没有被发现将函数应用于它们,那是因为请求已经是空的,它应该在你发出请求时消失,但是你可以通过添加{{1来隐藏这些通知在变量之前
@
或者在顶部
添加此隐藏所有通知/错误<?php
include_once('db.php');
$eMail = mysql_real_escape_string(@$_POST["eMail"]);
$passWord = mysql_real_escape_string(md5(@$_POST["psword"]));
$query = "SELECT count(*) FROM users WHERE (email ='$eMail' AND pass ='$passWord')";
if( $query>0 ) {
echo "Yes Your Area Welcome.";
}
else{
echo "No Your Area Not Welcome!.";
}
$mysqli->close();
?>