将SQL查询插入我的数据库php时出现问题

时间:2019-01-20 18:09:15

标签: php sql

我试图在数据库表中插入一行,但我不明白为什么不插入该表,我从表格中很好地收集了数据,连接很好,而且我认为查询格式正确。 / p>

已正确设置我已打印的数据,连接未显示错误,并且控制台未显示任何故障。

我不明白这是什么问题。

$servername = "localhost";
$username   = "root";
$password   = "";
$dbname     = "recepop";

// Create connection
$connection = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

$ingredientes    = $_POST['ingrediente'];
$titulo          = $_POST['buscador'];
$idusuario       = NULL;
$elaboracion     = $_POST['paso'];
$categorias      = $_POST['categorias'];
$imagenPrincipal = $_POST['imaPrin'];
$imagenes        = $_POST['ima'];
$personas        = (int) $_POST['pers'];
echo $personas;
echo $titulo;
echo $ingredientes;
echo $elaboracion;
echo $categorias;
echo $valoracion = NULL;
echo $imagenPrincipal;
echo $imagenes;
$fecha = date("Y-m-d H:i:s");
$contenido       = "".$ingredientes.$elaboracion;

//rename("imagenes/".$imagen, "imagenes/".$imagenFinal);
$query = "INSERT INTO recetas (titulo, contenido, fecha, idusuario, valoracion, imagenprincipal, imagenes, comensales) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

if ($stmt = $connection->prepare($query)) {
    $stmt->bind_param("ssssssss", $titulo, $contenido, $fecha, $idusuario, $valoracion, $imagenPrincipal, $imagenes, $personas);
    if ($stmt->execute()) {
        echo "Inserted";
        //header('Location: mostrar.php?usu='.$_GET['usu']."&pass=".$_GET['pass']);
    } else {
        // Do some logging
        error_log($stmt->error);
        echo "Not inserted";
    }
} else {
    // Do some logging
    error_log($connection->error);
    echo "Not inserted";
}


$connection->close();

表SQL

CREATE TABLE IF NOT EXISTS `recetas` (
  `idreceta` int(11) NOT NULL AUTO_INCREMENT,
  `titulo` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
  `contenido` mediumtext COLLATE utf8_spanish2_ci NOT NULL,
  `fecha` date NOT NULL,
  `idusuario` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
  `imagenprincipal` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
  `valoracion` float DEFAULT NULL,
  `imagenes` text COLLATE utf8_spanish2_ci,
  `comensales` int(11) NOT NULL,
  PRIMARY KEY (`idreceta`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci;

INSERT INTO `recetas` (`idreceta`, `titulo`, `contenido`, `fecha`, `idusuario`, `imagenprincipal`, `valoracion`, `imagenes`, `comensales`) VALUES
(1, 'Paella mixta', '<ul class=\"list-group\">\r\n  <li class=\"list-group-item\">Cras justo odio</li>\r\n  <li class=\"list-group-item\">Dapibus ac facilisis in</li>\r\n  <li class=\"list-group-item\">Morbi leo risus</li>\r\n  <li class=\"list-group-item\">Porta ac consectetur ac</li>\r\n  <li class=\"list-group-item\">Vestibulum at eros</li>\r\n</ul>', '2019-01-15', '1', 'paella.jpg', NULL, NULL, 0),
(2, 'Boquerones', '<ul class=\"list-group\">\r\n  <li class=\"list-group-item\">Cras justo odio</li>\r\n  <li class=\"list-group-item\">Dapibus ac facilisis in</li>\r\n  <li class=\"list-group-item\">Morbi leo risus</li>\r\n  <li class=\"list-group-item\">Porta ac consectetur ac</li>\r\n  <li class=\"list-group-item\">Vestibulum at eros</li>\r\n</ul>', '2019-01-24', '1', 'boquerones.jpg', NULL, NULL, 0),
(3, 'Salchichas', '<ul class=\"list-group\">\r\n  <li class=\"list-group-item\">Cras justo odio</li>\r\n  <li class=\"list-group-item\">Dapibus ac facilisis in</li>\r\n  <li class=\"list-group-item\">Morbi leo risus</li>\r\n  <li class=\"list-group-item\">Porta ac consectetur ac</li>\r\n  <li class=\"list-group-item\">Vestibulum at eros</li>\r\n</ul>', '2019-01-24', '1', 'salchi.jpg', NULL, NULL, 0);
COMMIT;

1 个答案:

答案 0 :(得分:0)

在准备好的语句中有两个变量,您没有从get请求中分配值。

$ contenido和$ idusuario

,由于您使用的是javascript以JSON格式返回结果,因此您没有看到实际的错误,请尝试直接从本地主机访问文件,以便打印错误并查看发生了什么。 但是现在,当我为上面提到的变量分配一个值时,该行已成功插入