Reputation: 56754
This is my set.php
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("rdbms.strato.de", "user", "password", "DBXXXXYYYY");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$result = $mysqli->prepare("INSERT INTO `defuse_scores` (ip, name, seconds, difficulty, actions) VALUES (?,?,?,?,?)");
$result->bind_param('ssisi', $_SERVER['REMOTE_ADDR'], $_POST['name'], $_POST['seconds'], $_POST['difficulty'], $_POST['actions']);
if (!$result->execute()) {
printf("Error: %s\n", $mysqli->error);
};
$mysqli->close();
Calling the page in a browser returns a blank page and a response status 200 OK
(even though all the $_POST
stuff supposedly is null).
Calling the page from Postman using POST and this body
{
"ip": "127.0.0.119",
"name": "ulli",
"seconds": "23",
"difficulty": "easy",
"actions": "22"
}
gives me and empty response and also status 200 OK
.
Yet I don't find any new records in my defuse_scores
table. What is going on?
I tried adding various lines of code (first three lines) to make any errors visible, yet the page stays blank.
Btw. getting the data works:
http://connexo.de/defuse/defuse-api/get.php
and this is where I'm trying to store using POST:
http://connexo.de/defuse/defuse-api/set.php
This is what my table looks like:
Upvotes: 1
Views: 852
Reputation: 74217
Your column sequences are off and your query failed silently. Why? Because of what you're trying to insert for the IP address is VARCHAR and is in the position used for the name
column.
The order is important when using the mysqli_
API, as opposed to PDO using named placeholders doesn't care for the order sequence.
So change your:
$result = $mysqli->prepare("INSERT INTO `defuse_scores` (ip, name, seconds, difficulty, actions) VALUES (?,?,?,?,?)");
$result->bind_param('ssisi', $_SERVER['REMOTE_ADDR'], $_POST['name'], $_POST['seconds'], $_POST['difficulty'], $_POST['actions']);
to:
$result = $mysqli->prepare("INSERT INTO `defuse_scores` (name, seconds, difficulty, actions, ip) VALUES (?,?,?,?,?)");
$result->bind_param('sisis', $_POST['name'], $_POST['seconds'], $_POST['difficulty'], $_POST['actions'], $_SERVER['REMOTE_ADDR']);
Or using a blank value for the AI'd column which is sometimes needed, and I've seen this happen before:
$result = $mysqli->prepare("INSERT INTO `defuse_scores` (id, name, seconds, difficulty, actions, ip) VALUES ('', ?,?,?,?,?)");
$result->bind_param('sisis', $_POST['name'], $_POST['seconds'], $_POST['difficulty'], $_POST['actions'], $_SERVER['REMOTE_ADDR']);
Another thing that would make your query fail silently, would be a column length not being long enough.
Upvotes: 1