doulan6150 2013-10-20 16:25
浏览 48
已采纳

PHP 5.4.12:未定义的索引错误

Hey there stackoverflow community!

I'm currently working on a inter-game + inter-server project but I'm getting stuck on my authentication system. When I login with some correct details I get the error

 Notice: Undefined index: myusername in C:\wamp\www
ew\panel.php on line 8
 Notice: Undefined index: mypassword in C:\wamp\www
ew\panel.php on line 9

 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www
ew\panel.php on line 13

I dont know how to fix this so what i'm asking is How do i fix this?

This is the code I am using to check the login details:

<?php

ob_start();
$host="localhost"; // Host name
$username="mindfulbank"; // Mysql username
$password=""; // Mysql password
$db_name="mindful_bank"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("Database cannot connect securely, please check back later!");
mysql_select_db("$db_name")or die("Database Cannot Be Selected");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$password=$_POST['mypassword'];
$sha1password=sha1($password);
$mypassword=md5($sha1password);

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername']=$myusername;
$_SESSION['mypassword']=$mypassword;
session_start();
header("location:panel.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

and the page that these errors acctually causes the error:

<?php
session_start();

if(isset($_SESSION['myusername'])){
header("location:logout.php");
}

$myusername=$_SESSION['myusername'];
$mypassword=$_SESSION['mypassword'];
$con=mysqli_connect("localhost","mindfulbank","","mindful_bank");
$result = mysqli_query($con,"SELECT level FROM members WHERE password = '$mypassword' and username = '$myusername'");
$fname = mysqli_query($con,"SELECT name FROM members WHERE password = '$mypassword' and username = '$myusername'");
while($row = mysqli_fetch_array($result))
{  
  if($row['admin'] == 1){
   require 'panel/admindash.php';
 }
 else
 {
   require 'panel/userdash.php';
}
}
?>
  • 写回答

3条回答 默认 最新

  • douhang1913 2013-10-20 16:33
    关注

    Update the connection on the script used to check login details to mysqli as mysql is depreciated.

    Once this is done change the mysql_real_escape_string($myusername); functions to mysqli_real_escape_string($your_connection, $myusername);.

    mysqli_real_escape_string expects a db connection as required parameter.

    http://php.net/manual/en/mysqli.real-escape-string.php

    Then add session_start(); to the top of the script used to check login details!

    http://php.net/manual/en/function.session-start.php

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。