基本上我有一个表单,当提交时它将数据发送到MySQL表,该表单输入的内容是文件上传,因此我必须检查它是否确实是图像。
因此,如果我在此处使用此代码将其发送到数据库:
$sql="INSERT INTO anuncio (anuncio_imagen, anuncio_titulo, anuncio_descripcion, anuncio_categoria, anuncio_precio, anuncio_nombre, anuncio_telefono, anuncio_email, anuncio_facebook)
VALUES ('$imagen', '$titulo', '$descripcion', '$categoria', '$precio', '$nombre', '$telefono', '$email', '$facebook')";
它可以正常工作,但是我想检查上传的文件是否是图像,因此我将上面的代码分成两个插入片段,并放置在图像检查代码中的图像插入片段中,其余输入字段保持完整。
这里是:
$imagen = ($_FILES['photo']['name']);
$titulo = mysqli_real_escape_string($con, $_POST['titulo']);
$descripcion = mysqli_real_escape_string($con, $_POST['descripcion']);
$categoria = mysqli_real_escape_string($con, $_POST['categoria']);
$precio = mysqli_real_escape_string($con, $_POST['precio']);
$nombre = mysqli_real_escape_string($con, $_POST['nombre']);
$telefono = mysqli_real_escape_string($con, $_POST['telefono']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$facebook = mysqli_real_escape_string($con, $_POST['facebook']);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["photo"]["name"]);
$extension = end($temp);
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/jpg")
|| ($_FILES["photo"]["type"] == "image/pjpeg")
|| ($_FILES["photo"]["type"] == "image/x-png")
|| ($_FILES["photo"]["type"] == "image/png"))
&& ($_FILES["photo"]["size"] < 10000000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["photo"]["error"] > 0) {
echo "Error: " . $_FILES["photo"]["error"] . "<br>";
} else {
if (file_exists("images/gallery/" . $_FILES["photo"]["name"])) {
echo $_FILES["photo"]["name"] . " Ya existe en el servidor. ";
} else {
move_uploaded_file($_FILES["photo"]["tmp_name"],
"images/gallery/" . $_FILES["photo"]["name"]);
$sql="INSERT INTO anuncio (anuncio_imagen)
VALUES ('$imagen')";
}
}
} else {
echo "Archivo invalido ";
}
$sql="INSERT INTO anuncio (anuncio_titulo, anuncio_descripcion, anuncio_categoria, anuncio_precio, anuncio_nombre, anuncio_telefono, anuncio_email, anuncio_facebook)
VALUES ('$titulo', '$descripcion', '$categoria', '$precio', '$nombre', '$telefono', '$email', '$facebook')";
但是,当我这样做时,它是行不通的,图像数据没有上载到我的数据库,其余的数据却没有上载。我究竟做错了什么?
最佳答案
仅使用一个INSERT
并使用mysqli_query
。检查move_uploaded_file
返回是否为true。尝试:
<?php
$imagen = ($_FILES['photo']['name']);
$titulo = mysqli_real_escape_string($con, $_POST['titulo']);
$descripcion = mysqli_real_escape_string($con, $_POST['descripcion']);
$categoria = mysqli_real_escape_string($con, $_POST['categoria']);
$precio = mysqli_real_escape_string($con, $_POST['precio']);
$nombre = mysqli_real_escape_string($con, $_POST['nombre']);
$telefono = mysqli_real_escape_string($con, $_POST['telefono']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$facebook = mysqli_real_escape_string($con, $_POST['facebook']);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["photo"]["name"]);
$extension = end($temp);
$bol_image = false;
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/jpg")
|| ($_FILES["photo"]["type"] == "image/pjpeg")
|| ($_FILES["photo"]["type"] == "image/x-png")
|| ($_FILES["photo"]["type"] == "image/png"))
&& ($_FILES["photo"]["size"] < 10000000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["photo"]["error"] > 0) {
echo "Error: " . $_FILES["photo"]["error"] . "<br>";
} else {
if (file_exists("images/gallery/" . $_FILES["photo"]["name"])) {
echo $_FILES["photo"]["name"] . " Ya existe en el servidor. ";
} else {
if (move_uploaded_file($_FILES["photo"]["tmp_name"],
"images/gallery/" . $_FILES["photo"]["name"])){
$bol_image = true;
} else{
echo "Problem in upload the image";
}
}
}
} else {
echo "Archivo invalido ";
}
if($bol_image){
$sql="INSERT INTO anuncio (anuncio_titulo, anuncio_descripcion, anuncio_categoria, anuncio_precio, anuncio_nombre, anuncio_telefono, anuncio_email, anuncio_facebook, anuncio_imagen)
VALUES ('$titulo', '$descripcion', '$categoria', '$precio', '$nombre', '$telefono', '$email', '$facebook', '$imagen')";
} else{
$sql="INSERT INTO anuncio (anuncio_titulo, anuncio_descripcion, anuncio_categoria, anuncio_precio, anuncio_nombre, anuncio_telefono, anuncio_email, anuncio_facebook)
VALUES ('$titulo', '$descripcion', '$categoria', '$precio', '$nombre', '$telefono', '$email', '$facebook')";
}
mysqli_query($con, $sql);
?>
关于php - 表单数据未插入MySQL数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23835654/