Storing Image Path in mySQL field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • New User
    New Member
    • Oct 2011
    • 4

    #1

    Storing Image Path in mySQL field

    I made a PHP form to add data to the mysql database and now I wan't to add photos (not to mysql db because I heard that's not a good ideea, so...) and list it with rest of the info from db I`v inserted with.

    I made a upload form, but I don't know how to rename the file`s I upload, and make a new folder for each time I upload data + photo, and than list photo from folder with the rest of the data I`v inserted.


    In other words...

    New data added from form to db
    Name, Age, Email, Description, Photo1, Photo2, Photo3.
    Upload to db and list it when necessary.

    Please help...
    Best regards
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    To rename files you can use rename() function: http://php.net/manual/en/function.rename.php

    Once you have your files referenced in your database, retrieve rows of data using MySql SELECT statement http://dev.mysql.com/doc/refman/5.0/en/select.html

    Comment

    • New User
      New Member
      • Oct 2011
      • 4

      #3
      I did rename the files, but how I make referenced in my database?
      thank you

      Comment

      • zorgi
        Recognized Expert Contributor
        • Mar 2008
        • 431

        #4
        Same way as you would add anything into the database. I usually insert only unique name of the file. Of course you can store path to the file if you prefer that way.

        Comment

        • New User
          New Member
          • Oct 2011
          • 4

          #5
          How can I upload 3-4 photos without copy-paste the entire code and how do I make references of new photos uploaded to insert in the db with the new data enter from 'inputImobil' and 'inputDescriere '?
          Please help

          This is the code that I`v combined for insert data to db and upload 1photo.


          <?php

          include 'connection.php ';

          $imobil = $_POST[inputImobil];
          $desc = $_POST[inputDescriere];



          define ("MAX_SIZE","10 00");
          function getExtension($s tr) {
          $i = strrpos($str,". ");
          if (!$i) { return ""; }
          $l = strlen($str) - $i;
          $ext = substr($str,$i+ 1,$l);
          return $ext;
          }
          $errors=0;


          if(isset($_POST['submit'])) {
          $image=$_FILES['image']['name'];
          if ($image) {
          $filename = stripslashes($_ FILES['image']['name']);
          $extension = getExtension($f ilename);
          $extension = strtolower($ext ension);
          if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
          {
          $newrecord1 = "<h1>Unknow n extension!</h1>";
          $errors=1;
          }
          else
          {
          $size=filesize( $_FILES['image']['tmp_name']);
          $image_name=tim e().'.'.$extens ion;
          $newname="uploa d/".$image_na me;
          $copied = copy($_FILES['image']['tmp_name'], $newname);

          }}}
          if(isset($_POST['submit']) && !$errors)
          {
          $newrecord2 = "<h1>File Uploaded Successfully! Try again!</h1>";
          }

          if(!$_POST['submit']) {
          $newrecord3 = "Completeaz a formularul";

          }else{
          mysql_query("IN SERT INTO vanzare (`ID`,`Imobil`, `Descriere`)
          VALUES(NULL,'$i mobil','$desc') ") or die(mysql_error ());
          $newrecord4 = "Adaugare reusita!";
          header('Locatio n: new5.php');
          }

          ?>

          Comment

          • New User
            New Member
            • Oct 2011
            • 4

            #6
            I`v insert the patch to db like this...

            mysql_query("IN SERT INTO vanzare (`ID`,`Imobil`, `Descriere`,`Te lefon`)
            VALUES(NULL,'$i mobil','$desc', '$newname')") or die(mysql_error ());



            How can I add 3-4 photos without double the code...
            And how do I select the table where to add the data.

            If select value="1" add data to table1 and if select value="2" add data to table2... All data from all fields.... (inputImobil, inputDescriere, Photo1, Photo2, etc...)

            Comment

            Working...