multiple-to-one relationship mysql, submissions

Posted by Yulia on Stack Overflow See other posts from Stack Overflow or by Yulia
Published on 2011-02-12T21:09:49Z Indexed on 2011/02/13 15:25 UTC
Read the original article Hit count: 182

Filed under:
|
|

Hello,

I have the following problem. Basically I have a form with an option to submit up to 3 images. Right now, after each submission it creates 3 records for album table and 3 records for images. I need it to be one record for album and 3 for images, plus to link images to the album. I hope it all makes sense...

Here is my structure.

TABLE `albums` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(50) NOT NULL,
  `fullname` varchar(40) NOT NULL,
  `email` varchar(100) NOT NULL,
  `created_at` datetime NOT NULL,
  `theme_id` int(11) NOT NULL,
  `description` int(11) NOT NULL,
  `vote_cache` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;

TABLE `images` (
  `id` int(11) NOT NULL auto_increment,
  `album_id` int(11) NOT NULL,
  `name` varchar(30) NOT NULL,

and my code

function create_album($params)
    {
       db_connect();

         $query = sprintf("INSERT INTO albums set
                                         albums.title = '%s',
                                                                     albums.email = '%s',
                                                                     albums.discuss_url = '%s',
                                                                     albums.theme_id = '%s',
                                                                     albums.fullname = '%s',
                                                                     albums.description = '%s',
                                                                     created_at = NOW()",
                                                                     mysql_real_escape_string($params['title']),
                                                                     mysql_real_escape_string($params['email']),
                                                                                                                 mysql_real_escape_string($params['theme_id']),
                                                                     mysql_real_escape_string($params['fullname']),
                                                                     mysql_real_escape_string($params['description'])
                                                                     );

         $result = mysql_query($query);
         if(!$result)
         {
              return false;
         }

         $album_id = mysql_insert_id();

         return $album_id;
    }

    if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i])) 
{ 
$warning = 'No file uploaded'; 
} 
elseif is_valid_file_size($_FILES['userfile']['size'][$i])) { 
$_POST['album']['theme_id'] = $theme['id']; 
create_album($_POST['album']); mysql_query("INSERT INTO images(name) VALUES('$newName')"); 
copy($_FILES['userfile']['tmp_name'][$i], './photos/'.$original_dir.'/' .$newName.'.jpg');

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql