troubleshooting Question

How do I create a query and binding function that dynamically add values that are available before inserting to data base

Avatar of B O
B O asked on
PHP
1 Comment1 Solution13 ViewsLast Modified:
I am trying to dynamically get values that are available

e.g. : I want to load 3 images while the database contains 5 columns for images
$data = [ 
title => ??,
user_id => ??,
body => ??,
img_0 => ??,
img_1 => ??,
img_2 => ??
];


This is my code:
public function addPost($data){


                $this->db->query('INSERT INTO posts (title, user_id, body, img_0, img_1, img_2, img_3, img_4) VALUES(:title, :user_id, :body, :img_0, :img_1, :img_2, :img_4)');
                
                //  Bind Values
                $this->db->bind(':title', $data['title']);
                $this->db->bind(':user_id', $data['user_id']);
                $this->db->bind(':body', $data['body']);


                // bind values of images
                $this->db->bind(':img_0', $data[0]['img_0']);
                $this->db->bind(':img_1', $data[1]['img_1']);
                $this->db->bind(':img_2', $data[2]['img_2']);
                $this->db->bind(':img_3', $data[3]['img_3']);
                $this->db->bind(':img_4', $data[4]['img_4']);
                


    
                // Execute
                if($this->db->execute()){
                    return true;
                } else {
                    return false;
                }
        }



ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros