【问题标题】:Notice: Undefined index: username in C:\xampp\htdocs\gpt\includes.php on line 3注意:未定义索引:第 3 行 C:\xampp\htdocs\gpt\includes.php 中的用户名
【发布时间】:2013-09-03 13:17:55
【问题描述】:

当谈到 PHP 和其他开发人员时,我是个菜鸟。无论如何,自从过去两天以来,我一直试图找到解决方案。我几乎到处搜索。因此,我尝试使用 XAMPP 将设计集成到 GPT 脚本中,但每次尝试访问文件所在的 localhost/gpt 时都会显示一堆错误。页面顶部显示:-

"; }}}} if(isset($_SESSION['username']) && isset($_SESSION['password'])){ header("Location: members.php"); } ?>
Notice: Undefined index: username in C:\xampp\htdocs\gpt\includes.php on line 3

Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\gpt\includes.php on line 3

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\gpt\includes.php on line 3

Notice: Trying to get property of non-object in C:\xampp\htdocs\gpt\includes.php on line 9

Notice: Trying to get property of non-object in C:\xampp\htdocs\gpt\includes.php on line 14

Notice: Trying to get property of non-object in C:\xampp\htdocs\gpt\includes.php on line 15

Notice: Trying to get property of non-object in C:\xampp\htdocs\gpt\includes.php on line 16

我尝试将error_reporting (E_ALL ^ E_NOTICE); 添加到config.php 和includes.php 的顶部,然后错误减少为:-

"; }}}} if(isset($_SESSION['username']) && isset($_SESSION['password'])){ header("Location: members.php"); } ?>
Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\gpt\includes.php on line 4

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\gpt\includes.php on line 4

我还尝试将session_start(); 添加到每个页面。 无论如何,这些是涉及的页面:-

config.php

<?
session_start();ob_start();
$hostname = "localhost"; //your hostname (normally localhost)
$data_username = "root"; //database username
$data_password = ""; //database password
$data_basename = "gpt"; //database name
$conn = mysql_connect("".$hostname."","".$data_username."","".$data_password."");  
mysql_select_db("".$data_basename."") or die(mysql_error());  
$bonuspoints=10; //bonus points awarded for new users
$mainpointsneeded=200; //max number of points needed before user can request voucher
?>

包括.php

<?php 
error_reporting (E_ALL ^ E_NOTICE); 
session_start();
$fetch_users_data = mysql_fetch_object(mysql_query("SELECT * FROM `members` WHERE username='".$_SESSION['username']."'"));
$title= "My Voucher Geek";  //your site title
$yourdomain="http://localhost/gpt"; //your domain name where script is installed - do not use trailing slash
$tweetmsg="Get Amazon and ASOS gift vouchers for free at http://www.myvouchergeek.com"; //set text for tweet this button on homepage
$bonuspoints= 10;    //amount of bonus points to give to users
$refer_points=2; //amount of points a user receives if one of their referred users completes any survey
$ref_id=$fetch_users_data->id;
if(isset($_GET['join'])){
    $referral_ID = $_GET['join'];
    $referral_string= "?join=".$referral_ID;
}
$membername= $fetch_users_data->username; //don't change
$memberpoints=$fetch_users_data->points; //don't change
$membersurveys=$fetch_users_data->completed_surveys; //don't change
$earnedpoints = $memberpoints - $bonuspoints;//if you want to display how many points user has earned (as opposed to bonus points)
$mainpointsneeded = 200; //total points needed before user can request a voucher
$pointsneeded= $mainpointsneeded - $memberpoints; //points left before they can request voucher
$contactemail = "YOUR_EMAIL_ADDRESS"; //contact form messages will be sent here
$requestemail = "THE_SAME_OR_ANOTHER_EMAIL_ADDRESS"; //request a voucher messages will be sent here
?>

index.php

<? 
session_start();
include_once"config.php";
if(isset($_POST['login'])){
$username= trim($_POST['username']);
$password = trim($_POST['password']);
if($username == NULL OR $password == NULL){
$final_report.="Please complete both fields";
}else{
$check_user_data = mysql_query("SELECT * FROM `members` WHERE `username` = '$username'") or die(mysql_error());
if(mysql_num_rows($check_user_data) == 0){
$final_report.="This username does not exist";
}else{
$get_user_data = mysql_fetch_array($check_user_data);
if($get_user_data['password'] == $password){
$start_idsess = $_SESSION['username'] = "".$get_user_data['username']."";
$start_passsess = $_SESSION['password'] = "".$get_user_data['password']."";
$final_report.="<meta http-equiv='Refresh' content='0; URL=members.php'/>";
}}}}
     if(isset($_SESSION['username']) && isset($_SESSION['password'])){ 
    header("Location: members.php");
    }

?> 
<?php include("includes.php");?>
<HTML>HTML_CONTENT</HTML>

【问题讨论】:

  • 在我看来您禁用了short_open_tag,并且第一个&lt;? 没有被解析...因此,您的PHP 代码被转储到浏览器中。查看页面源代码,您会在那里看到它。 php.net/manual/en/ini.core.php#ini.short-open-tag你应该把那些从&lt;?改成&lt;?php
  • 我试过了。没用。
  • 需要明确的是,删除所有短标签 &lt;? 不会让其他错误消失。这些是以下答案中提到的弃用的结果。但是,它会导致这些标签内的代码实际被执行,并且您不应再在浏览器页面源中看到 PHP 代码。
  • 谢谢,先生。根据您的建议,一些问题已得到解决,例如我现在可以使用测试帐户登录。我是菜鸟,请尝试理解。你能告诉我关于“MySQL 已弃用”问题可以做些什么吗?因为这似乎是主要问题

标签: php mysql xampp


【解决方案1】:

这些警告表明所有mysql_* 函数都已弃用,您不应再使用它们。如果您不希望出现警告,则需要在使用这些功能之前移动错误报告设置。现在你在includes.php 的顶部有error_reporting (E_ALL ^ E_NOTICE);,这是最后一个被调用的东西。你应该把它移到config.php 的顶部。此外,使用您现在拥有的错误设置,您将显示除通知之外的所有错误,因此仍会显示警告。如果您想隐藏警告和/或已弃用的错误条目,您需要将其更改为:

error_reporting(E_ERROR);

你也应该改变这些行,你不需要引号:

//$conn = mysql_connect("".$hostname."","".$data_username."","".$data_password."");
//mysql_select_db("".$data_basename."") or die(mysql_error());
$conn = mysql_connect($hostname,$data_username,$data_password);
mysql_select_db($data_basename) or die(mysql_error());

另外,现在您的脚本非常不安全。 $username 在发送到数据库之前不会被过滤。

你有其他地方你有$variable1 = "".$variable2.""这些双引号你一文不值。你可以说$variable = $variable2;

【讨论】:

  • 感谢您的评论。但是,我不想隐藏错误。实际上,我希望整个脚本在 XAMPP 上工作,因为它在上传到网站主机时工作。我已经在托管帐户上尝试过脚本,它工作正常。我对 XAMPP 没有太多经验。那么,如果我要向某人提供脚本,他们可以在 XAMPP 上检查它并修复错误吗?如果它不违反 TOS 并且有人愿意提供帮助。
  • 如果你想修复脚本,你应该做的第一件事就是去掉所有的mysql_*函数。比如mysql_connectmysql_select_dbmysql_query等等……
  • 还有,我可以用什么来代替它们,因为这些是脚本正常运行所必需的。
  • 我认为问题已经暂时解决了。我实际上将
  • 正如通知所说:“改用mysqli或PDO”
猜你喜欢
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多