Wählen Sie funktioniert, Insert in MySQL nicht mit PHP einfügen
Ich habe eine Tabelle wie folgt
CREATE TABLE IF NOT EXISTS `pictures` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`description` varchar(200) NOT NULL,
`url` varchar(1000) NOT NULL,
`users_id` bigint(20) NOT NULL,
`totalvoteup` int(11) NOT NULL,
`totalvotedown` int(11) NOT NULL,
`totalvoteneutral` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `users_id` (`users_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3
Ich kann folgende Werte in phpmyadmin mit SQL-Anweisung einfügen
INSERT INTO pictures(name, description, url, users_id) VALUES ('try','try','try', 12345)
Also kein Problem in der Datenbank
Aber wenn ich das folgende PHP-Skript verwende, um Interaktionen mit meiner Datenbank auszuwählen und einzufügen, fügt es nichts in die Datenbank ein
<?php
// Helper method to get a string description for an HTTP status code
// From http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
function getStatusCodeMessage($status)
{
// these could be stored in a .ini file and loaded
// via parse_ini_file()... however, this will suffice
// for an example
$codes = Array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);
return (isset($codes[$status])) ? $codes[$status] : '';
}
// Helper method to send a HTTP response code/message
function sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
$status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
header($status_header);
header('Content-type: ' . $content_type);
echo $body;
}
class RedeemAPI {
private $db;
// Constructor - open DB connection
function __construct() {
$this->db = new mysqli('localhost', 'root', 'password', 'testBasParmak');
$this->db->autocommit(FALSE);
}
// Destructor - close DB connection
function __destruct() {
$this->db->close();
}
// Main method to redeem a code
function redeem() {
// Check for required parameters
if (isset($_POST["users_id"]) && isset($_POST["name"]) && isset($_POST["url"])&& isset($_POST["description"])) {
// Put parameters into local variables
$users_id = $_POST["users_id"];
$name = $_POST["name"];
$url = $_POST["url"];
$description=$_POST["description"];
// insert i also tried with 'Insert .....' rather then "Insert...."
$stmt = $this->db->prepare("INSERT INTO pictures(name, description, url, users_id) VALUES (?, ?, ?, ?)");
$stmt->bind_param("sssi", $name, $description, $url, $users_id);
$stmt->execute();
$stmt->close();
//select
$stmt = $this->db->prepare('SELECT name, description, url, users_id FROM pictures');
$stmt->execute();
$stmt->bind_result($name, $description, $url, $users_id);
while ($stmt->fetch()) {
echo "Name of picture is $name url of picture is $url";
}
$stmt->close();
return true;
}
sendResponse(400, 'Invalid request');
return false;
}
}
// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();
?>
Wenn ich folgende Parameter in das Terminalfenster schreibe
curl -F "name=deneme" -F "description=deneme" -F "url=deneme" -F "users_id=705735067" http://website.local/index.php
Als Ergebnis sehe ich Aus Bildern auswählen funktioniert, aber wenn ich in die Datenbank gehe und prüfe, ob die neue Zeile vorhanden ist, kann ich die Zeile nicht sehen.
Was ist los mit dem PHP-Skript? Warum funktioniert die Auswahl, während das Einfügen nicht funktioniert?
------BEARBEITEN-----------------------
Ich habe eine Beilage angeordnet, um eine Fehlermeldung zu erhalten
// insert
$stmt = $this->db->prepare("INSERT INTO pictures(name, description, url, users_id) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $name, $description, $url, $users_id);
$stmt->execute();
printf("name: %s\n", $name);
printf("description: %s\n", $description);
printf("url: %s\n", $url);
printf("usersid: %d\n", $users_id);
printf("Errormessage: %s\n", $stmt->error);
$stmt->close();
Ausgabe ist
name: deneme
description: deneme
url: deneme
usersid: 705735067
Errormessage: