I have two database tables namely, photos
, albums
. I am able to upload photos and insert form data into both tables at once which is okay.
The problem I have is that,
when I upload e.g, 5 photos, 5 rows are created into each table-- each row for each photo.
What I want is that,
5 rows should be created in the photos
table but only one row should go into albums
table. The photos
table has foreign key
to albums
table.
Here is my code below:
The index.php
which extends the UploadHandler
handler has the following code:
<?php
$options = array(
'delete_type' => 'POST',
'db_host' => 'localhost',
'db_user' => 'username',
'db_pass' => 'password',
'db_name' => 'test',
'db_table' => 'photos'
);
error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
class CustomUploadHandler extends UploadHandler {
protected function initialize() {
$this->db = new mysqli(
$this->options['db_host'],
$this->options['db_user'],
$this->options['db_pass'],
$this->options['db_name']
);
parent::initialize();
$this->db->close();
}
protected function handle_form_data($file, $index) {
$file->title = @$_REQUEST['title'][$index];
$file->description = @$_REQUEST['description'][$index];
}
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
$index = null, $content_range = null) {
$file = parent::handle_file_upload(
$uploaded_file, $name, $size, $type, $error, $index, $content_range
);
if (empty($file->error)) {
$sql = 'INSERT INTO `'.$this->options['db_table']
.'` (`name`, `size`, `type`, `title`, `description`)'
.' VALUES (?,?, ?, ?, ?)';
$query = $this->db->prepare($sql);
$query->bind_param(
'sisss',
$file->name,
$file->size,
$file->type,
$file->title,
$file->description,
);
$query->execute();
$file->id = $this->db->insert_id;
//LABEL: PROBLEM BLOCK BEGINS
/*Here, I am attempting to insert only row in the albums table
for each batch of photos I upload. So even if I upload 5 photos
into the photos table, only one row should be created in the albums table*/
$sql2 = 'INSERT INTO `albums` (`album_title`, `album_description`)'
.' VALUES (?,?)';
$query2 = $this->db->prepare($sql2);
$query2->bind_param(
'ss',
$file->title,
$file->description,
);
$query2->execute();
$file->id = $this->db->insert_id;
//LABEL: PROBLEM BLOCK ENDS
}
return $file;
}
}
$upload_handler = new CustomUploadHandler($options);
?>
In the code above, I have commented block of code as
//LABEL: PROBLEM BLOCK BEGINS
...
//LABEL: PROBLEM BLOCK ENDS.
The code above works to insert same number of rows in both photos
table and albums
table. I need help with the PROBLEM BLOCK
in order to create only one row in albums
table for each batch of photos
I upload in photos table.
I figured out the solution myself. I added a unique id
column to the album
table and I added the value of the unique id
as hidden
field on the upload form so it became like this
the album table
CREATE TABLE album (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
album_title varchar(255) DEFAULT NULL,
album_description text,
special_album_id varchar(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE (special_album_id)
)
In the upload form, I added
<input type = "hidden" name = "special_album_id[]" value= "<?php echo $variable-data; ?>">
You will need to create your own function to get the variable $variable-data
which will be different for each new photo upload.
With this, the CustomUploadHanler.php
will send queries to insert multiple rows in the album
table but the table will accept only one row for each batch of photos uploaded. Perhaps there is a better solution out there but this worked for me for now.
Also do the following changes in the code in the OP.
change the:
protected function handle_form_data($file, $index) {
$file->title = @$_REQUEST['title'][$index];
$file->description = @$_REQUEST['description'][$index];
}
to
protected function handle_form_data($file, $index) {
$file->title = @$_REQUEST['title'][$index];
$file->description = @$_REQUEST['description'][$index];
$file->special_album_id = @$_REQUEST['special_album_id'][$index];
}
Then in the PROBLEM BLOCK, change:
$sql2 = 'INSERT INTO `albums` (`album_title`, `album_description`)'
.' VALUES (?,?)';
$query2 = $this->db->prepare($sql2);
$query2->bind_param(
'ss',
$file->title,
$file->description,
);
to
$sql2 = 'INSERT INTO `albums` (`album_title`, `album_description`, `special_album_id`)'
.' VALUES (?,?,?)';
$query2 = $this->db->prepare($sql2);
$query2->bind_param(
'sss',
$file->title,
$file->description,
$file->special_album_id
);
That's it; we are done. Hope this helps someone