Java Android使用php连接到mysql,字符串查询错误

Java Android使用php连接到mysql,字符串查询错误,java,php,android,mysql,http-post,Java,Php,Android,Mysql,Http Post,我正在编写一个android程序,它使用HTTPPOST连接到一个php脚本,连接到mysql数据库。query是要发送的变量 以下是我的部分代码: String query = "select `retailer`.`name` as `retailer`, `product`.`name` as `product`, `product`.price, `promotion`.discount, `promotion`.discount_type"; query = query

我正在编写一个android程序,它使用HTTPPOST连接到一个php脚本,连接到mysql数据库。query是要发送的变量

以下是我的部分代码:

String query = "select `retailer`.`name` as `retailer`, `product`.`name` as `product`, `product`.price, `promotion`.discount, `promotion`.discount_type";
        query = query.concat(" from `retailer`, `consumer`, `product`, `promotion`, `sell`, `proximity`");
        query = query.concat(" where `consumer`.`email` = '"+consumer_email+"'");
        query = query.concat(" and `proximity`.`retailer_id` = `retailer`.`id`" +
                " and `proximity`.`consumer_id` = `consumer`.`id`" +
                " and `sell`.`retailer_id` = `retailer`.`id`" +
                " and `sell`.`product_id` = `product`.`id`" +
                " and `product`.`promotion_id` = `promotion`.`id`");
请注意,consumer_email是一个字符串,包含我要查询的电子邮件。然而,我不断得到这个语法错误。如果我去掉行
消费者
电子邮件
='“+consumer\u email+”,它就能够毫无错误地进行处理


我想问的是,是否有办法解决这个问题?是不是电子邮件中的一句话导致了这个问题?

像这样使用预先准备好的语句

这将消除您看到的错误。它也是安全的,可以防止SQL注入攻击

不要直接替换查询中的电子邮件地址,而是使用“?”。然后在运行时将值绑定到它。

字符串查询=。。。。。。
query=query.concat(“where`consumer`.`email`=?);
......
$db=newmysqli('localhost','username','password','database');
//创建语句对象
$stmt=$db->stmt_init();
//创建准备好的语句
如果($stmt->prepare(查询)){
$stmt->bind_参数('s',$email_id);
//设置变量
$email_id=”example@exp.com";
//执行查询
$stmt->execute();
}

像这样使用预先准备好的语句

这将消除您看到的错误。它也是安全的,可以防止SQL注入攻击

使用“?”代替直接替换查询中的电子邮件地址。然后在运行时将值绑定到它。

字符串查询=。。。。。。
query=query.concat(“where`consumer`.`email`=?);
......
$db=newmysqli('localhost','username','password','database');
//创建语句对象
$stmt=$db->stmt_init();
//创建准备好的语句
如果($stmt->prepare(查询)){
$stmt->bind_参数('s',$email_id);
//设置变量
$email_id=”example@exp.com";
//执行查询
$stmt->execute();
}

您遇到了什么错误?返回错误的是什么,php脚本、sql server、android程序?哇,您刚刚实现了一个简单意义上的sql注入功能。幸运的是,这个功能这么早就中断了,因为您真的必须扔掉代码,改变告诉php脚本要获取哪些数据的方式参数本身(不是SQL查询!)也许我最后的评论有点不友好,你可能在寻找这个:你得到了什么错误?返回错误的是什么,php脚本,sql服务器,android程序?哇,你刚刚实现了一个叫做sql注入的功能,它的意思很简单。你很幸运,它这么早就中断了,因为你真的必须扔掉你的代码并改变告诉PHP脚本通过参数本身获取哪些数据的方式(不是SQL查询!)可能我的最后一条评论有点不友好,您可能在寻找以下内容:
   String query = ......<rest of query>
     query = query.concat(" where `consumer`.`email` = ? );
    ......<rest of query>

    $db = new mysqli('localhost', 'username', 'password', 'database');
    // Create statement object
    $stmt = $db->stmt_init();


// Create a prepared statement
if($stmt->prepare(query )) {
    $stmt->bind_param('s', $email_id);

    // Set your variable    
    $email_id= "example@exp.com";

    // Execute query
    $stmt->execute();
}