gpt4 book ai didi

php - 无法使用 mysqli 发布数据

转载 作者:行者123 更新时间:2023-11-29 17:34:42 26 4
gpt4 key购买 nike

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

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

<?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&unrecognised=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);
}
?>

最佳答案

Tigger 链接很有用

<?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();
?>

关于php - 无法使用 mysqli 发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50389259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com