-1

So, I am creating a very simply uploader where I take in a file/name/description and to store to store the file to the server and name/desc/filepath. This is what my database looks like:

Basically, i take in a file, name, and description and want the file to be stored in the server. While the path/name/desc to be stored into mySQL. I also want an incrementing ID. I have a db named "test". And want to post to "test_table". The database is already created but I need to check if the table exists, if not, create it. I think I have the basics of mySQL correct below, but I need to know how to check for table/create if needed and how to set the path variable. Thanks in advance!

Also, how do I increment the ID variable in mysql with each entry? does that happen automatically or .. ?

solved

Brian J
  • 7
  • 7

2 Answers2

0

To check, if a table exists, see this question: check if MySQL table exists or not Essentially, this is the code to check:

if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) 
    echo "Table exists";
else echo "Table does not exist";

To autoinc the ID just use the autoincrement attribute to the ID field, see the documentation here for more info: https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

Code:

ALTER TABLE test_tabel MODIFY COLUMN ID INT auto_increment

Hope this helps...

Community
  • 1
  • 1
Paladin
  • 1,637
  • 13
  • 28
  • 1
    OP is using `PDO` and you're using a deprecated Library (`mysql_*`), please keep your answer relevant and up to date to prevent any future issues the OP could face from using this library :-) – Darren Nov 17 '15 at 06:50
  • @Darren These are just examples on how to do the job, taken from prev answers here on SO ... i think, one can easily extract the sql from the code and port it onto his needs – Paladin Nov 17 '15 at 06:51
0

you need to upload your file to the server before inserting in the database:

if ($_FILES['fileinput']['name']!="") {  

                    if (is_uploaded_file($_FILES['fileinput']["tmp_name"])) {                   
                        $name = date("Y-m-d : H:i:s")." ".$_FILES['fileinput']['name']; 
                        $filevalue = $nameoffile;
                            $path =$file_save_path.$nameoffile;

                            if (move_uploaded_file($_FILES["file".$i]["tmp_name"], $uploaddir.$nameoffile)) {       

                                 $sql = "INSERT INTO test_table (name, desc, path)  VALUES ($name, $desc, $path)";
                                mysqli_query($conn, $sql)
                            } else { 

                                echo "File Upload Error. Please Try again";                             
                            }
                        }

             }
Simone
  • 636
  • 2
  • 8
  • 25