无法使用mysqli发布数据

jjhzyzn0  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(275)

我正在尝试将数据插入mysql数据库,但是在某个地方发生了一些事情,而insert没有发生。
这是我的php脚本(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&total=200&dangerous=0&unrecogned=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);
}
?>
ia2d9nvy

ia2d9nvy1#

跳跳虎链接是有用的

<?php
require "init2.php";
$model = $_POST["model"];
$total = $_POST["total"];
$dangerous = $_POST["dangerous"];
$unrecognised = $_POST["unrecognised"];
$query = "insert into AndroidDatabase.ReportTable (MODEL,Total_Packages,Dangerous_Packages,Unrecognised) VALUES (?,?,?,?)";
$stmt = $con->prepare($query);
$stmt->bind_param("siii", $model,$total,$dangerous,$unrecognised);
$stmt->execute();
$stmt->close();
$con->close();
?>

相关问题