无法使用mysqli发布数据

时间:2018-05-17 10:31:39

标签: php android mysql mysqli

我正在尝试将数据插入到mysql数据库中,但某处发生了某些事情并且插入不会发生。

这是我的php skript(init2.php用于连接,我100确定它是否成功连接)

<?php
require "init2.php";
$model = $_POST["model"];
$total = $_POST["total"];
$dangerous = $_POST["dangerous"];
$unrecognised = $_POST["unrecognised"];

$sql = "insert into AndroidDatabase.ReportTable (MODEL,Total_Packages,Dangerous_Packages,Unrecognised) values ('$model',$total,$dangerous,$unrecognised);";

?>

这也是我的android代码:

URL url = new URL(REPORT_URL);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream OS =  httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
            String data = URLEncoder.encode("model","UTF-8") + "=" +  URLEncoder.encode(model,"UTF-8") +"&" +
                    URLEncoder.encode("total","UTF-8") + "=" +  URLEncoder.encode(total,"UTF-8") +"&" +
                    URLEncoder.encode("dangerous","UTF-8") + "=" +  URLEncoder.encode(dangerous,"UTF-8") +"&" +
                    URLEncoder.encode("unrecognised","UTF-8") + "=" +  URLEncoder.encode(unrecognised,"UTF-8") ;
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();
            InputStream IS = httpURLConnection.getInputStream();
            IS.close();

提到这是“数据”,请写缓冲:“model = GT-I9505&amp; total = 200&amp; dangerous = 0&amp; unrerecognized = 18”

编辑:

当我硬编码时,这有效:

<?php
require "init2.php";
$model = "test";
$total = 30;
$dangerous = 30;
$unrecognised = 30;

$sql = "insert into AndroidDatabase.ReportTable (MODEL,Total_Packages,Dangerous_Packages,Unrecognised) values ('$model',$total,$dangerous,$unrecognised)";

if(mysqli_query($con,$sql))
{
echo "success";
}
else
{
echo "error".mysqli_error($con);
}
?>

1 个答案:

答案 0 :(得分:0)

跳跳虎链接很有用

$(window).on('beforeunload', function() {
Utils.ajaxCall('GET', "/logout", '', '', '', false);