我现在已经搜索了几个小时但仍然遇到了这个问题(这让我发疯了)。正如主题已经说明的那样,我尝试在数据库中插入一行,该数据库失败而没有错误。在phpMyAdmin上执行语句工作正常。
我已在此处找到this question并应用了该解决方案。我怀疑character settings on the database并设置了所有内容(表格加上列加上db到utf8_bin)。我还设置了autocommit to false and I do my own commit。数据库连接就在那里,因为我可以在数据库表上选择几行(下面没有显示)并获得一个不错的结果。我甚至检查过我正在运行该语句(如建议here)。
function getConnection() {
$mysqli = new mysqli("localhost", "myuser", "mypass", "mydb");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
return $mysqli;
}
//this function is responsible for inserting new records
function insertNewRecord($tableAndFolder) {
$debug = 1;
if ($debug) {
error_reporting(E_ALL);
ini_set('display_errors', '1');
}
$mysqli = getConnection();
if ($debug) {
echo 'The database connection is: <br/>';
var_dump($mysqli);
echo '<br/><br/>';
}
if ($debug) {
echo '<br/>Exploding the whole POST-Array: <br/>';
var_dump($_POST);
echo '<br/><br/>';
echo "This Is Your Absolute Path: ";
$path = getcwd();
echo $path;
echo '<br/><br/>';
echo 'Inserting new Record into ' . $tableAndFolder;
echo '<br/><br/>';
}
$description = $_POST['text'];
$headline = $_POST['title'];
$target_dir = "../galerie_" . $tableAndFolder . "/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if (isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["tmp_name"]);
if ($check !== false) {
if ($debug) {
echo "File is an image - " . $check["mime"] . ".";
echo '<br/><br/>';
}
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 300000) {
echo "Sorry, your file is too large. Current Size: " . round($_FILES['file']['size'] / 1024);
$uploadOk = 0;
}
// Allow certain file formats
echo'image is ' . $target_file;
if ($imageFileType != "jpg" && $imageFileType != "jpeg") {
echo "Sorry, only JPG & JPEG files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if ($debug) {
echo '<br/>Temporary Name is: ' . $_FILES["file"]["tmp_name"] . '<br/>';
echo 'Target Name is: ' . $target_file . '<br/>';
echo '<br/><br/>';
}
if (is_dir($target_dir)) {
if (is_writable($target_dir)) {
//move file on webserver
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file " . basename($_FILES["file"]["name"]) . " has been uploaded.";
settype($tableAndFolder, 'string');
settype($description, 'string');
settype($headline, 'string');
if ($debug) {
if ($mysqli->connect_errno) {
printf("<br/>Connect failed: %s\n", $mysqli->connect_error);
echo '<br/><br/>';
}
/* check if server is alive */
if ($mysqli->ping()) {
printf("Our connection is ok!\n");
echo '<br/><br/>';
} else {
printf("Error: %s\n", $mysqli->error);
echo '<br/><br/>';
}
}
//select last ordering and insert new entry behind (max(ordering)+1)
$ordering = 0;
$getNextOrdering = sprintf("SELECT MAX(ORDERING)+1 as ord FROM `mydb`.`galerie_%s`", $tableAndFolder);
$getNextOrdering = $mysqli->real_escape_string($getNextOrdering);
$result = $mysqli->query($getNextOrdering);
if ($debug) {
echo '<br/>The query to get the next ordering is: ' . $getNextOrdering;
}
if ($debug) {
echo '<br/>Nr of rows: ' . $result->num_rows;
}
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
if ($debug) {
echo '<br/>The result is: ' . $row;
var_dump($row);
echo '<br/><br/>';
}
extract($row);
$ordering = $row["ord"];
if ($debug) {
echo '<br/>The result is: ' . $ordering;
echo '<br/><br/>';
}
}
} else {
if ($debug) {
echo "0 results";
}
}
settype($ordering, 'integer');
$sql = sprintf("INSERT INTO `mydb`.`galerie_%s` (`headline`, `text`, `ordering`) VALUES ('%s', '%s', '%d' )", $tableAndFolder, $headline, $description, $ordering);
if ($debug) {
echo 'The SQL statement is: ' . $sql;
echo '<br/><br/>';
}
$mysqli->autocommit(FALSE);
$sql = $mysqli->real_escape_string($sql);
$erg = $mysqli->query($sql);
$mysqli->commit();
$num = $mysqli->affected_rows;
if ($num > 0) {
echo 'Insert into Database was successful!';
} else {
echo 'Insert into Database was NOT successful!';
echo $mysqli->error;
}
$mysqli->close();
} else {
echo "Sorry, there was an error uploading your file.";
}
} else {
echo 'Upload directory not writable.';
}
} else {
echo 'Upload directory does not exist.';
}
}
我总是遇到if-Statement的else-branch,但是没有收到来自$ mysqli-&gt;错误的错误消息(如suggested here)。任何人都可以给我一些可能出错的提示吗?