我一直在寻找如何将多个key->值插入mysql-database。 我可以插入单个对象,但不能在foreach循环中插入我必须做错的事情。考虑到我知道我的java代码有效,因此我能够将单个对象存储在数据库中的单个coloumn表中,它必须是我的php语法错误 - 即使我遵循教程。请帮我 这是我的php代码,必须有错误
<?php
require_once 'db.php';
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$st = mysqli_prepare($con, 'INSERT INTO movies(title,year,genre,director) VALUES(?,?,?,?)');
mysqli_stmt_bind_param($st, 'sdss', $title, $year, $genre, $director);
foreach ($obj as $row){
$title = $obj['title'];
$year = $obj['year'];
$genre = $obj['genre'];
$director = $obj['director'];
mysqli_stmt_execute($st);
}
?>
my require_once'db_php';连接到数据库,我从远程Java应用程序获取数据。 这是我的javacode - 只是省略了我的数据库URL和方法创建
HttpURLConnection connection= null;
try {
URL obj = new URL(url2);
connection = (HttpURLConnection) obj.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
// connection.setRequestProperty("Accept", "application/json");
connection.connect();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
JSONObject request = new JSONObject();
request.put("action","login");
request.put("title","The 40 year old virgin");
request.put("year","2004");
request.put("genre","Horror");
request.put("director","Takashi Shimizu");
String output =request.toString();
writer.write(output);
writer.flush();
writer.close();
InputStream input = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
int responseCode = connection.getResponseCode();
System.out.println("ResponseCode: " + responseCode);
while ((line = reader.readLine()) != null) {
result.append(line);
}
System.out.println("result:" + result.toString());
}
catch (IOException e) {
} finally {
connection.disconnect();
}
}
这是我在IDE中的控制台中出现的错误 - eclipse“:警告:mysqli_stmt_bind_param()期望参数1为mysqli_stmt”但是,我不知道如何处理它
答案 0 :(得分:0)
如果sql失败,$ st将不是mysqli语句对象,我通常使用mysqli作为对象,语句中的任何拼写错误通常都会显示为在非对象上调用bind param的错误,我怀疑你可能有一个类似的问题。您可以使用程序样式测试错误,如下所示:
if($st == false){
error_log(mysqli_error($conn));
}
如果准备失败,mysqli_prepare返回false,那么你可以调用mysqli_error传递mysqli_erction以获取最后一个错误的详细信息。