Adding data to a table column in php

This chunk of code works successfully upon a purchase transaction in a web video site.
I’d like to add additional data from the ‘videos’ table, corresponding to the purchased video, into the ‘paid_videos’ table.
In the ‘videos’ table there is a column that populates an identifier ‘short_id’ for each uploaded video. How can I make the corresponding ‘short_id’ appear in the ‘paid_videos’ table? based on the code below.

$db->startTransaction();

$inserted_records = 0;

foreach ($id_array as $id){

$video_id = (int)PT_Secure($id);

// get video data
$video = $db->where('id', $id)->getOne(T_VIDEOS);

$time_start = microtime(true);

// add data to paid table

$insert_buy = $db->insert('paid_videos', [
'id_user' => $user_id,
'id_video' => $video_id,
'session_key' => $_SESSION['session_key'],
'video_title' => $video->title,
'user_id_uploaded' => $video->user_id,
'time' => $time_start
]);

Any guidance is appreciated.

Add the short ID to your insert() according to the documentation of your database abstraction layer.

… after making sure the paid_videos table has a suitable column to insert it into, of course.

I’m not sure it’s a good idea to duplicate the information though - if your paid_videos already contains a link to the table that stores the short_id (which it appears to through the id_video column) why would you want to store it in a second place? Sure, it’ll make queries a little easier, but it also adds an extra step if you ever change the value of the short_id in your videos table.

Thanks for the replies.
Regarding “Add the short ID to your `insert()”, I did this:

$insert_buy = $db->insert('paid_videos', [
'id_user' => $user_id,
'id_video' => $video_id,
'session_key' => $_SESSION['session_key'],
'video_title' => $video->title,
'user_id_uploaded' => $video->user_id,
'time' => $time_start
**'short_id' => $short_id**

what else does this code need, something like this?:

$video = $db->where('short_id', $short_id)->getOne(T_VIDEOS);

Additionally, regarding “the paid_videos table has a suitable column to insert it into”, I believe I have that correct (it’s a duplicate of the ‘videos’ table > ‘short_id’ column structure).

any additional guidance is welcomed.

Doesn’t this code that you already have

$video = $db->where('id', $id)->getOne(T_VIDEOS);

retrieve the video information, which would presumably include the short id if that’s a column in the videos table?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.