我是php的新手,在运行时我几乎在每个页面都出现错误“Undefined index .........”不知道该怎么办
以下是登录页面的示例之一 -
感谢您的提示......
<?php
ob_start();//Starting output buffering to avoid the Headers Already Sent error on some installations
//Todo: "e-mail forgotten password" function
//Todo: make the logo uploadable in the admin panel
//Localisation: none
//Adding the page header
include 'includes/page_header_simple.php';
//Load the language file
include 'includes/language.php';
//Assigning values already entered (if any) to variables
$login_username = strip_tags($_POST['login_username']);
$login_password = strip_tags($_POST['login_password']);
$button_pressed = $_POST['action_button'];
if ($button_pressed == "Login") {
//***************************************************************************************************************************************
//Validate the form if the "Submit" button was pressed in the Registration Form
//***************************************************************************************************************************************
//Check if the user has entered values in both input fields
if((!$login_username) || (!$login_password)) {
//Javascript redirect function is used as the PHP header() function creates errors in some installations
?>
<script type="text/javascript">
<!--
window.location = "frm_login.php"
-->
</script>
<?php
exit();
}
//Find the username in the database
//Connect to the database
include 'includes/config.php';
include 'includes/opendb.php';
$sql = "SELECT user_id, tp_username, user_password, first_name, last_name, usergroup FROM users WHERE tp_username = '$login_username' AND enabled = '1'";
$result = mysql_query($sql) or die('Sorry, could not complete the login process. If the problem persists, please send the following error report to the administrator: ' . mysql_error());
if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_array($result);
$user_password = $row['user_password'];
//Carry out password check
if(crypt($login_password, $user_password) == $user_password) {
//Set the Session and move to the the Prospects page if the login is successful
//Start the session
session_start();
$_SESSION["logged_in"] = true;
$_SESSION['first_name'] = mysql_result($result, 0, "first_name");
$_SESSION['last_name'] = mysql_result($result, 0, "last_name");
$_SESSION['usergroup'] = mysql_result($result, 0, "usergroup");
$_SESSION['user_id'] = mysql_result($result, 0 , "user_id");
$_SESSION['sort_by'] = "pipeline_items.probability";
$_SESSION['sort_order'] = "DESC";
$_SESSION['geography_filter'] ="%";
$_SESSION['product_service_filter'] ="%";
$_SESSION['value_filter'] = "1";
$_SESSION['sales_stage_filter'] = "%" ;
$_SESSION['probability_filter'] = "%";
//Javascript redirect function is used as the PHP header() function creates errors in some installations
?>
<script type="text/javascript">
<!--
window.location = "tbl_pipeline_items.php"
-->
</script>
<?php
exit();
} else {
//If the user authentication failed, display an error message and reload the Login Form
include 'includes/closedb.php';
echo("<p align='center' class='gen_text'>The username/password combination you have used is invalid or your account has not yet been activated by the administrator.</p>");
//This form includes a single button that allows users to return to the registration form after an unsuccessful registration -->
echo ("<form name='back_to_login' method='post' action='frm_login.php'>");
echo ("<p align='center'><input type='submit' name='submit' value='Return to Login'></p>");
//Insert the footer
echo ("<table border='0' width='100%' align='center'>");
echo ("<tr>");
echo ("<td colspan='2'><hr size='1'></td>");
echo ("</tr>");
echo ("<tr>");
echo ("<td><p align='left' class='sm_gen_footer'>$VERSION</p></td>");
echo ("<td><div align='right'><a href='http://theplumber.whsites.net' target='_blank'><img src='images/poweredby.gif' alt='Powered by thePlumber' width='80' height='15' border='0'></a></div></td>");
echo ("</tr>");
echo ("</table>");
}
} else {
//Display an error message if the username entered in the Login Form does not exist in the database
include 'includes/closedb.php';
echo("<p align='center' class='gen_text'>The username/password combination you have used is invalid or your account has not yet been activated by the administrator.</p>");
//This form includes a single button that allows users to return to the registration form after an unsuccessful registration -->
echo ("<form name='back_to_login' method='post' action='frm_login.php'>");
echo ("<p align='center'><input type='submit' name='submit' value='Return to Login'></p>");
//Insert the footer
echo ("<table border='0' width='100%' align='center'>");
echo ("<tr>");
echo ("<td colspan='2'><hr size='1'></td>");
echo ("</tr>");
echo ("<tr>");
echo ("<td><p align='left' class='sm_gen_footer'>$VERSION</p></td>");
echo ("<td><div align='right'><a href='http://theplumber.whsites.net' target='_blank'><img src='images/poweredby.gif' alt='Powered by thePlumber' width='80' height='15' border='0'></a></div></td>");
echo ("</tr>");
echo ("</table>");
}
}
if ($button_pressed == "") {
//***************************************************************************************************************************************
//Display the Login Form if the "$button_pressed" variable is empty
//***************************************************************************************************************************************
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> thePlumber </title>
<meta name="Generator" content="Dev-PHP 2.2.2">
<link rel="stylesheet" href="styles/default.css">
<style type="text/css">
</style>
</head>
<body>
<!-- This file displays the Login form -->
<table border="0" width="80%" border="0" align="center" cellpadding="0" cellspacing="3">
<tr>
<td>
<form name="user_login" method="post" action="frm_login.php">
<table border="0" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
<tr height="20" bgcolor="b51109">
<td valign="top"><div align="left"><img src="images/red_tl.png" width="17" height="17"></div></td>
<td></td>
<td></td>
<td></td>
<td valign="top"><div align="right"><img src="images/red_tr.png" width="17" height="17"></div></td>
</tr>
<tr height="40" bgcolor="b51109">
<td></td>
<td rowspan="2"><img src='images/login.png' width='64' height='64' border='0'></td>
<td valign="middle"><div align="left" class="login_text"><label for="login_username">Username:</label></div></td>
<td valign="middle"><div align="left"><input type="text" size="20" length="25" name="login_username" value=""></div></td>
<td></td>
</tr>
<tr height="40" bordercolor="b51109" bgcolor="#b51109">
<td></td>
<td valign="middle"><div align="left" class="login_text"><label for="login_password">Password:</label></div></td>
<td valign="middle"><div align="left"><input type="password" size="20" length="25" name="login_password" value=""></div></td>
<td></td>
</tr>
<tr height="40" bordercolor="b51109" bgcolor="b51109">
<td valign="bottom"><div align="left"><img src="images/red_bl.png" width="17" height="17"></div></td>
<td></td>
<td></td>
<td valign="middle"><div align="right"><input type="submit" name="action_button" value="Login"></div></td>
<td valign="bottom"><div align="right"><img src="images/red_br.png" width="17" height="17"></div></td>
</tr>
</table>
</form>
<p align="left"><a class="login_small_links" href="frm_registration.php">Need a new user account? Register here.</a>
</td>
<td width="10"></td>
<td width="100%" valign="top"><p align="left" class="gen_text">Welcome to the sales pipeline management system. Please login to continue.</p></td>
</tr>
</table>
<?php
//Inserting the footer
echo ("<table border='0' width='100%' align='center'>");
echo ("<tr>");
echo ("<td colspan='2'><hr size='1'></td>");
echo ("</tr>");
echo ("<tr>");
echo ("<td><p align='left' class='sm_gen_footer'>$VERSION</p></td>");
echo ("<td><div align='right'><a href='http://theplumber.whsites.net' target='_blank'><img src='images/poweredby.gif' alt='Powered by thePlumber' width='80' height='15' border='0'></a></div></td>");
echo ("</tr>");
echo ("</table>");
}
ob_flush();//End output buffering
?>
</body>
</html>
答案 0 :(得分:1)
我注意到你没有在$_REQUEST
/ $_POST
变量上使用任何isset ...我建议你阅读本指南并使用isset()来调试哪一个可能导致问题!<登记/>
http://siliconstation.com/how-fix-php-notice-undefined-index/
答案 1 :(得分:0)
NOTICE
错误在PHP中并不重要,但它们可以帮助调试旧代码。
此错误意味着您在代码中使用了之前未定义的变量,您有两种方法可以解决它:
在错误报告中关闭通知(我建议这样做)
将error_reporting(E_ALL ^ E_NOTICE);
放在应用程序的第一行
在使用之前定义变量,或在使用之前设置条件,如:
if(isset($variable)) echo $variable;