php中未定义的索引错误

时间:2015-01-25 08:48:39

标签: php compiler-errors syntax-error

我在IntejiIdea中编写了以下PHP代码,但是当我在本地主机中运行此代码时,会显示以下错误:

 Notice: Undefined index: username in B:\dev\project\web\ted\login.php on line 6
Notice: Undefined index: password in B:\dev\project\web\ted\login.php on line 7

我的PHP代码:

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("ted_db",$con);


$user=$_POST['username'];
$pass=$_POST['password'];


$sqlQ="select * from users where Username='$user' ";
$result=mysql_Query($sqlQ);
$row=mysql_fetch_array($result);



if($row[2]==""){
    print "no user";
}
else if($pass!=$row[3])
{
    print "wrong password";
}else{
    print "ok";
}


mysql_close($con);

?>

我很困惑,请帮助我。我的朋友们。

2 个答案:

答案 0 :(得分:0)

首先检查您是否传递值用户名和密码如果您不希望显示错误,请检查 试试这个

以前的答案

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("ted_db",$con);

if(isset($_POST['username']) && isset($_POST['password'])){
$user=$_POST['username'];
$pass=$_POST['password'];


$sqlQ="select * from users where Username='$user' ";
$result=mysql_Query($sqlQ);
$row=mysql_fetch_array($result);



if($row[2]==""){
    print "no user";
}
else if($pass!=$row[3])
{
    print "wrong password";
}else{
    print "ok";
}


mysql_close($con);
}
else{
echo "No username or password passed..";
}
?>

这应该没有错误

修改

我相信你是从JAVA发送GET请求而不是POST请求,因为我对java不是很熟悉,代码中有一点点变化 检查下面

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("ted_db",$con);

if(isset($_REQUEST['username']) && isset($_REQUEST['password'])){
$user=$_REQUEST['username'];
$pass=$_REQUEST['password'];


$sqlQ="select * from users where Username='$user' ";
$result=mysql_Query($sqlQ);
$row=mysql_fetch_array($result);



if($row[2]==""){
    print "no user";
}
else if($pass!=$row[3])
{
    print "wrong password";
}else{
    print "ok";
}


mysql_close($con);
}
else{
echo "No username or password passed..";
}
?>

让我知道这是怎么回事

答案 1 :(得分:0)

&#13;
&#13;
package com.acharapp.ted;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.os.AsyncTask;


public class loginServer extends AsyncTask {

    private String Link = "";
    private String User = "";
    private String Pass = "";


    public loginServer(String link, String user, String pass) {

        Link = link;
        User = user;
        Pass = pass;
    }


    @Override
    protected String doInBackground(Object... arg0) {

        try {

            String data = URLEncoder.encode("username", "UTF8") + "=" + URLEncoder.encode(User, "UTF8");
            data += "&" + URLEncoder.encode("password", "UTF8") + "=" + URLEncoder.encode(Pass, "UTF8");

            URL mylink = new URL(Link);
            URLConnection connect = mylink.openConnection();

            connect.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(connect.getOutputStream());
            wr.write(data);
            wr.flush();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
            StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null) {

                sb.append(line);

            }

            TEDActivity.res = sb.toString();

        }
        catch (Exception e) {

        }

        return "";
    }

}
&#13;
&#13;
&#13;