I have been looking for how to inserting multiple key->value into mysql - database. I can insert single objects, but not in an foreach-loop i must be doing something wrong. Considering i know my java code works, hence I am able to store individual objects in a singe-coloumn table in the database, it must be my php-syntax that is wrong - even though I follow a tutorial. please help me Here is my php-code, which must have the error
<?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'; connects to the database, and i get the data from a remote java application. Here is my javacode - just leaving out my database-URL, and method-creation
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();
}
}
And this is the error I get in the console in IDE - eclipse ":Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt" however, I don´t know what to do with it
If the sql failed the $st won't be a mysqli statement object, I normally use mysqli as an object and any typos in the statement usually show up as an error calling bind param on a non object, I suspect you may have a similar issue. You can test for errors with a procedural style some thing like this:
if($st == false){
error_log(mysqli_error($conn));
}
mysqli_prepare returns false if the prepare fails, then you can call mysqli_error passing in the mysqli_connection to get the details of the last error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.