简体   繁体   中英

Form data is not getting inserted into MySQL database

Basically i have a form that when submitted it sends data to a MySQL table, the thing is of this inputs of the form is a file upload so i have to check that it really is an image.
So if i use this code right here to send it to the database:

$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')";  

it works flawlessly, but i want to check to see if the uploaded file is an image so i separate the code above into two inserts and placed to image insert inside the image check code and left the rest of the inputed fields intact.

Here it is:

$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')";  

But it doesn't work when i do this, the image data is not uploaded to my database, the rest of the data is uploaded but not the image data. What am i doing wrong?

Replace the second INSERT statement with an UPDATE statement. The first INSERT creates the row in the table, with all the column values that you supplied there. In the second statement, you want to UPDATE the already-created row with a new value.

Use only one INSERT and use mysqli_query . Check if the move_uploaded_file return is true. Try:

<?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);

?>

The INSERT INTO will only work if the record (row) doesn't already exist. If you want to do it in two steps you would use UPDATE on your second statement. For example:

Your code:

move_uploaded_file($_FILES["photo"]["tmp_name"],
"images/gallery/" . $_FILES["photo"]["name"]);
$sql="INSERT INTO anuncio (anuncio_imagen)
VALUES ('$imagen')";

Instead:

move_uploaded_file($_FILES["photo"]["tmp_name"],
"images/gallery/" . $_FILES["photo"]["name"]);
$sql="UPDATE anuncio
SET anuncio_imagen = ('$imagen')
WHERE anuncio_titulo = ('$titulo')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM