php - 表单数据未插入 MySQL 数据库

标签 php mysql

基本上我有一个表单,提交后它会将数据发送到 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/

相关文章:

MySQL 编号排序

Java SE 将桌面应用程序连接到在线数据库

javascript - JustGage Javascript 值中的 PHP 数据

mysql - 为什么在MySQL中添加外键而不绘制关系

php - Android MySQL-PHP 数据库字符串不通过问题

php - MySQL 多个 And 和 or 匹配没有结果

javascript - 使用 AJAX 更改 PHP db_query 参数

php - 作为报价收到的 JSON(带有 ""标记)

php - 通过 PDO exec() 在 MYSQL DB 中插入多行

javascript - Codeigniter:仅在第一个模式框上调用 Ajax