Current location: Hot Scripts Forums » Programming Languages » PHP » Upload image & text to MYSQL --> AT THE SAME TIME?


Upload image & text to MYSQL --> AT THE SAME TIME?

Reply
  #1 (permalink)  
Old 02-20-04, 01:46 PM
charliedhq charliedhq is offline
New Member
 
Join Date: Feb 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question Upload image & text to MYSQL --> AT THE SAME TIME?

See: http://jordache.com/forums/upload-data-and-image.gif

I'm tring to create a form that does the following on submit:

1. Validates the form (checks for errors and file-size limits)
If invalid: Tags invalid fields with a red astrik (or gif)

2. If valid: Uploads the data in the fields to a MYSQL db
and uploads the image(s) to a specific folder on the server

MY PROBLEM:
I can't figure out how to upload the image and post the text data to the database AT THE SAME TIME.

Help!
Thanks,
Chuck
Reply With Quote
  #2 (permalink)  
Old 02-20-04, 02:57 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Quote:
Originally Posted by charliedhq
See: http://jordache.com/forums/upload-data-and-image.gif

I'm tring to create a form that does the following on submit:

1. Validates the form (checks for errors and file-size limits)
If invalid: Tags invalid fields with a red astrik (or gif)

2. If valid: Uploads the data in the fields to a MYSQL db
and uploads the image(s) to a specific folder on the server

MY PROBLEM:
I can't figure out how to upload the image and post the text data to the database AT THE SAME TIME.

Help!
Thanks,
Chuck
Try this...basic error checking, upload, and mysql insert...change the commented areas and error checking as needed...name your gif "asterisk.gif".

PHP Code:

 
     
// checks file type allowed...add more if needed
     
$file_types = array(   
     
'image/pjpeg' => 'jpg'
     
'image/jpeg' => 'jpg'
    ); 
 
     
// Info from your form...change to what info you are entering
     
$name=$_POST['name']; 
     
$title=$_POST['title'];
     
$desc=$_POST['desc'];
    
    
     
$max_size 2000000;
     
$filesize $_FILES['upfile']['size'];
     
$filetype $_FILES['upfile']['type'];
 
    
$star=asterisk.gif;
 
     
// Change next line to the directory you are putting the pics into...
     
$upload_dir $_SERVER['DOCUMENT_ROOT'] . "/pics/"
       if((!
$fname) || (!$title) || (!$desc)){
 echo 
'You did not submit the following required information. Use your browsers "back" button to complete your form. <br />';
 if(!
$fname){
  echo 
"$star Name is a required field. <br />";
 }
 if(!
$title){
  echo 
"$star Photo title is a  required field. <br />";
 
 }
              if(!
$desc){
  echo 
"$star Photo description is a  required field. <br />";
 
 }
               }else{
       
        if (
$filesize $max_size){
           echo 
"This file is too large.";
           exit;
        }
         if (!
array_key_exists($filetype$file_types)) { 
             echo 
"This file is of the wrong type.";
            exit;
        } 
        else
        {   
                
$new_file substr(sha1(rand(10time())), 08) . '.' $file_types[$filetype]; 
               if (
move_uploaded_file($_FILES['upfile']['tmp_name'], $upload_dir $new_file)) { 
                 
              
$filename=$new_file;
               
                
// Add database connection
$conn mysql_connect(localhost,"user","pass") or die ("Could not connect to MySQL"); 
mysql_select_db("database") or die ("Could not open database");
 
// Change table_name 
$query "INSERT INTO table_name VALUE('','$name','$title','$desc','$filename')"
$result=mysql_query($query); 
      }
   }
mysql_close(); 
echo 
"File uploaded!";

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to change text dynamically with image? rabbit51 JavaScript 1 02-23-04 08:35 AM
How to change text dynamically with image? rabbit51 Script Requests 0 02-07-04 12:14 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM
Basic image upload and data to mysql mdhall Script Requests 3 11-05-03 05:00 PM
File upload and path to mysql dalio PHP 0 08-12-03 08:24 AM


All times are GMT -5. The time now is 09:04 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.