0

オンラインで見つけたPHPログインで問題が発生しました-

 <?php

$host="localhost"; // Host name 
$username="SOME_USERNAME"; // Mysql username 
$password="SOME_PASSWORD"; // Mysql password 
$db_name="b00556019"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// 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_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
 }
else {
echo "Wrong Username or Password";
}
 ?>

エラーは 16 ~ 17 行目にあり、次のように表示されます。

Notice: Undefined index: myusername in E:\home\students\2137\B00556019\public_html\workspace\login\checklogin.php on line 16

Notice: Undefined index: mypassword in E:\home\students\2137\B00556019\public_html\workspace\login\checklogin.php on line 17

間違ったユーザー名またはパスワード

これは機能していない2行です:

 $myusername=$_POST['myusername']; 
 $mypassword=$_POST['mypassword']; 

ここにHTMLがあります

<!DOCTYPE HTML>
<html>
<head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>#

アイデアやより良いチュートリアルの例を持っている人なら誰でも大歓迎です:)

4

3 に答える 3

0

使ってください

if(isset($_POST['myusername']) && $_POST['myusername']!=''){
    $myusername=$_POST['myusername']; 
   $mypassword=$_POST['mypassword']; 

    // 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_register("myusername");
   session_register("mypassword"); 
  header("location:login_success.php");
 }
else {
 echo "Wrong Username or Password";
}


}

これで問題が解決します

于 2014-03-12T14:26:43.990 に答える
0

Add form start tag and it will start working

<!DOCTYPE HTML>
<html>
<head>
<body>
<form action="" method="post">
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1"               bgcolor="#CCCCCC">
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr> 
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td> 
 </tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>

</form>
</body>

specify action as per your requirement

于 2014-03-12T14:41:36.350 に答える