1

I have some problems when I upload a picture, it uploads to the directory but in SQL it doesn't put the name, only puts 1 or 0

https://i.sstatic.net/KxSaO.jpg

In SQL, it's all in varchar(255).

   if(isset($_POST["submit"])) {
    $date=date("d/m/Y");

$target_dir = "../img/logo/";

  if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_dir.$_FILES['fileToUpload']['name'])){

    $insert=mysqli_query($db, "UPDATE logo SET img = '".basename($_FILES["fileToUpload"]["name"])."' AND  data_insercao = '".$date."'") or die("Error".mysqli_error());
    echo "<script>alert('Success!');</script>";
    echo "<meta http-equiv=\"refresh\" content=\"0;URL='index.php?pg=3'\" /> ";
    echo "<meta http-equiv=\"refresh\" content=\"0;URL='index.php?pg=3'\" /> ";
  }
}

SOLVED

What I did to change

$insert=mysqli_query($db, "UPDATE logo SET img = '".basename($_FILES["fileToUpload"]["name"])."', data_insercao = '".$date."'") or die("Error".mysqli_error());

Just had a comma instead of AND

5
  • 1
    be sure to that the variable type in DB of column 'img' is VAR_CHAR and not INT Commented Mar 7, 2017 at 11:03
  • I already did @CamiloGo, It's in the post Commented Mar 7, 2017 at 11:08
  • 1
    Try switching AND for , in your query. You could also build your query first, then echo it for testing purposes, then execute it. Commented Mar 7, 2017 at 11:11
  • @justbaron Thank you! Never thought it would be so easy to do. Commented Mar 7, 2017 at 11:13
  • @BrunoMoutinho added answer, feel free to accept and up-vote ;) Commented Mar 7, 2017 at 11:17

4 Answers 4

1

Switch AND for , in your query.

$insert=mysqli_query($db, "UPDATE logo SET img = '".basename($_FILES["fileToUpload"]["name"])."',  data_insercao = '".$date."'") or die("Error".mysqli_error());

Presumably, you are using this table for one image, hence no need for a WHERE clause.

Sign up to request clarification or add additional context in comments.

Comments

1

Hope this might help:

$insert=mysqli_query($db, "UPDATE logo SET img = '".$_FILES["fileToUpload"]["name"]."', date = '$date'") or die("Error".mysqli_error());

p/s: You might want to put WHERE, just for reminder

Comments

0

To update multiple columns use , instead of AND

 $insert=mysqli_query($db, "UPDATE logo SET img = '".basename($_FILES["fileToUpload"]["name"])."' , data_insercao = '".$date."'") or die("Error".mysqli_error());

Comments

0
       if(isset($_POST["submit"])) {
        date_default_timezone_set('Asia/Calcutta'); 
    $date= date("Y-m-d H:i:s"); 

    $target_dir = "../img/logo/";

      if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_dir.$_FILES['fileToUpload']['name'])){

        $insert=mysqli_query($db, "UPDATE logo SET img = '".$_FILES["fileToUpload"]["name"]."', date = '$date'") or die("Error".mysqli_error());
        echo "<script>alert('Success!');</script>";
        echo "<meta http-equiv=\"refresh\" content=\"0;URL='index.php?pg=3'\" /> ";
        echo "<meta http-equiv=\"refresh\" content=\"0;URL='index.php?pg=3'\" /> ";
      }
    }

use date time with second because sum time file name is same and date also so file will overwrite in image folder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.