Posted By:
9 Oct, 2013 7:05 pm PDT

Creating a form with file attachments and submit it using php

Hi, I want to create a form that has one button to submit and the following fields

File attachment:

How can i create and submit the details of each text fields once the button is clicked?

I want to save the data and display them in a textarea. I also want to enter the data to a database. The database details are:

  1. DB Name: db
  2. User name: test
  3. List of columns:first_name, email, add, path

I also want the attachment to be moved to a folder 'files'. I have some other things to do. but these are the basic ones.

Thanks in advance.


Please download the attached zip file and then unzip it. You will get two file upload.php and brian.sql. Please have a look at these two files. In case of more clarification required please reply.


if (isset($_POST['save'])) {
       $name = trim(strip_tags($_POST['name']));
       $email = trim(strip_tags($_POST['email']));
       $address = trim(strip_tags($_POST['address']));
       $file_name = trim(strip_tags($_FILES['file']['name']));
       $errors = array();
       if (empty($name))
             $errors['name'] = "Empty";
       if (empty($email))
             $errors['email'] = "Empty";
       if (empty($address))
             $errors['address'] = "Empty";
       if (empty($file_name))
             $errors['file'] = "Empty";
       else {
             $file_path = $_FILES['file']['tmp_name'];
             $file_content = addslashes(file_get_contents($file_path));
       if (empty($errors)) {
             mysql_connect("localhost", "root", "root") or die("Can not connect

to DB");

             mysql_select_db("brian") or die("Can not select DB");
             $q = "insert into test values(null, '$name', '$email', '$address',
'$file_name', '$file_content')";
             mysql_query($q) or die($q . mysql_error());
             $success = "
   <table border=1 align=center width=50%>
    <tr><th colspan=2>Following values have been saved:</th></tr>



} }


<form action=upload.php method=post enctype='multipart/form-data'>
   <table border=1 align=center width=50%>
    <tr><th colspan=2>File Upload Form</th></tr>
      <input type=text name=name value="<?php echo $name; ?>">
      <font color=red><?php echo $errors['name']; ?></font>

</tr> <tr>

      <input type=text name=email value="<?php echo $email; ?>">
      <font color=red><?php echo $errors['email']; ?></font>


      <input type=text name=address value="<?php echo $address; ?>">
      <font color=red><?php echo $errors['address']; ?></font>

</tr> <tr>

      <input type=file name=file>
      <font color=red><?php echo $errors['file']; ?></font>

</tr> <tr>

     <th colspan=2>
      <input type=submit name=save value=Save>

</th> </tr>



echo $success;


Was it useful?  

Please login to reply to this problem.