我正在尝试创建一个表单,我在这些行中出错。
else
{
//report the errors.
echo '<h1> Err... </h1>
<p> The following error(s) have occured</p>';
foreach ($errors as $msg)
{
echo "--$msg<br />\n";
}
echo '</p><p>Please Try Again.</p><p><br/></p>';
}
那么,有什么不对?这是错误消息 -
... ERR
发生了以下错误 -
注意:未定义的变量:错误 第107行的C:\ wamp \ www \ password.php
警告:提供的参数无效 foreach()在C:\ wamp \ www \ password.php中 在107行,请再试一次。
我将错误设置为数组。
我上面的代码 -
如果(isset($ _ POST [ '提交'])) {
require_once('C:\wamp\www\connect.php');
//connecting to db
$errors = array();
if (empty($_POST['email']))
{
$errors[]='Please enter a valid email address.';
}
这是我的完整代码 -
//forgot password update
include('C:\wamp\www\header.html');
//check if form has been submitted
require_once('C:\wamp\www\connect.php');
//connecting to db
if(isset($_POST['submitted'])) {
$errors = array();
if (empty($_POST['email']))
{
$errors[]='Please enter a valid email address.';
}
else
{
$e = mysqli_real_escape_string($db_name,trim($_POST['email']));
}
//check for current password
if (empty($_POST['password']))
{
$errors[]='Current password does not match.';
}
else
{
$p = mysqli_real_escape_string($db_name,trim($_POST['password']));
}
//check for a new password and match with confirm pass.
if(!empty($_POST['password1']))
{
if($_POST['password1'] != $_POST['cpass'])
{
$errors[] = 'The entered password and confirm password do not match.';
}
else
{
$np=mysqli_real_escape_string($db_name,trim($_POST['password1']));
}
}
if(empty($errors))
//if everything is fine.
//verify the entered email address and password.
$q="SELECT username FROM users WHERE (email='$e' AND password=SHA1('$p'))";
$r=@mysqli_query($db_name,$q);
$num = @mysqli_num_rows($r);
if($num==1)
//if it matches.
//get user id
{
$row=mysqli_fetch_array($r, MYSQLI_NUM);
//udpdate query.
$q="UPDATE users SET password= SHA1('$np') WHERE username=$row[0]";
$r=@mysqli_query($db_name, $q);
if (mysqli_affected_rows($db_name) ==1)
{
echo '<h3>Your password has been updated.</h3>';
}
else {
echo '<h3>Whops! Your password cannot be changed due a system error. Try again later. Sorry</h3>';
echo '<p>' .mysqli_error($db_name). 'Query:' . $q.'</p>';
}
exit();
}
else
{
//invalid email and password
echo 'The email address and password do not match';
}
}
else
{
//report the errors.
echo '<h1> Err... </h1>
<p> The following error(s) have occured</p>';
foreach ($errors as $msg)
{
echo "--$msg<br />\n";
}
echo '</p><p>Please Try Again.</p><p><br/></p>';
}
?>
答案 0 :(得分:3)
没有名为$errors
的数组。你必须进一步查看你的脚本。
您可以使用
修复错误消息if (!empty($errors) and (is_array($errors)))
foreach ($errors as $msg)
答案 1 :(得分:2)
foreach
循环超出了定义$error
数组的范围。
您的代码简而言之:
if(isset($_POST['submitted'])) {
$errors = array();
} else {
foreach($errors as $error)
}
如果未设置$_POST
,则未定义$errors
。
答案 2 :(得分:2)
将“$ errors = array()”的声明移到行“if(isset($ _ POST ['submitted'])){{ “一切都应该运转正常!
答案 3 :(得分:1)
你有两个问题。第一个是空的/不存在的数组的原因,第二个是缺乏对它的测试。
首先,您正在测试if块内的错误,然后在else块内循环它们。
if (isset($_POST['submitted'])) {
// create errors array and set errors
} else {
// loop through array of errors
}
因此,如果设置了错误,脚本就不会进入循环。如果脚本进入循环,则不会设置错误。
第二个是你应该在测试数组后才进入foreach循环:
if (!empty($errors) && is_array($errors)) { // use this line and get rid of the else.
foreach ($errors as $msg) {
echo "--$msg<br />\n";
}
echo '</p><p>Please Try Again.</p><p><br/></p>';
} // and close it.
答案 4 :(得分:0)
基本上,这里发生的是你在定义之前使用$ errors。
可能需要在脚本顶部附近设置“$ errors = array()”,以便它始终至少为空数组。