Solved

Upload form, but when I don't upload anything file there still goes textfile named untitled.txt? How i get that away?

Posted on 2014-03-03
1
392 Views
Last Modified: 2014-03-03
I have little upload form, but when I don't upload anything there still goes to mysql database textfile named untitled.txt? How i get that away? Thank you so much in advance...
Here is mys form:

<form action="lisaa_tiedosto_uutinen_action2.php" method="post" enctype="multipart/form-data">

      Title: <br><input name="title" size="40" maxlength="255">
      <br><br>
      Text1:<br> <textarea name="text1"  rows="7" cols="30"></textarea>
      <br>
      <br>
      <input type="file" name="uploaded_file">
      <br>
      <br>
            <input type="submit" value="Lisää uutinen">
</form>

Open in new window


Here is that upload page code:

<?php
// NEVER SUPPRES NOTICE MESSAGES - INSTEAD CORRECT THE UNDERLYING CONDITIONS THAT CAUSE THE NOTICE
error_reporting(E_ALL);

$dbLink = new mysqli('localhost', 'user', 'pass', 'db');
if(!$dbLink) 
{
    trigger_error("MySQL connection failed: ". mysqli_connect_error(), E_USER_ERROR);
}

// IF THERE WAS NO FILE UPLOADED
if ($_FILES['uploaded_file']['error'] == 4) 
{
    /* RUN THE QUERY WITHOUT THE FILE DATA 
     */
	 
	 // ESCAPE THE EXTERNAL DATA THAT WILL BE NEEDED
$title = $dbLink->real_escape_string($_POST['title']);
$text1 = $dbLink->real_escape_string($_POST['text1']);
	 
	             $query1 = "
                INSERT INTO `uutiset_tiedostot` (
                   `title`, `dtime`, `text1`
                )
                VALUES (
                    '$title',NOW(),'$text1'
                )";
				// Execute the query
            $result = $dbLink->query($query1);
			if($result) {
                echo 'Success 1! Your file was successfully added!';
            }
}
// IF THERE WAS A CLEAN UPLOAD
elseif ($_FILES['uploaded_file']['error'] == 0)
{

// Gather all required data
// ESCAPE THE EXTERNAL DATA THAT WILL BE NEEDED
$title = $dbLink->real_escape_string($_POST['title']);
$text1 = $dbLink->real_escape_string($_POST['text1']);
            $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
            $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
            $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
            $size = intval($_FILES['uploaded_file']['size']);
    /* USE Move_UpLoaded_File() 
     * ESCAPE NAME, TYPE, ETC
     * RUN THE QUERY WITH THE FILE DATA
     */
	 
	             $query2 = "
                INSERT INTO `uutiset_tiedostot` (
                   `title`, `dtime`, `text1`, `name`, `mime`, `size`, `data`, `created`
                )
                VALUES (
                    '$title',NOW(),'$text1','{$name}', '{$mime}', {$size}, '{$data}', NOW()
                )";
				// Execute the query
            $result = $dbLink->query($query2);
			if($result) {
                echo 'Success 2!  Your file was successfully added!';
            }
	 
}

?>

Open in new window

0
Comment
Question by:Enska77
1 Comment
 
LVL 14

Accepted Solution

by:
shobinsun earned 500 total points
ID: 39900055
Hi,

Check your database table for the default values to the fields or please give the tablke structure in sql file.

For reference you cna follow the below link:

http://php.about.com/od/phpwithmysql/ss/Upload_file_sql.htm

Regards,
Shobin markose.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now