Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:48:10+00:00 2026-05-27T19:48:10+00:00

Should I be using the __construct option to create a new record? Below is

  • 0

Should I be using the __construct option to create a new record? Below is the class I have called ‘Course’. I will only actually be “creating” a course 25% of the time, the rest of the time I’ll want to lookup courses and what not.

class Course {
    private $db;

    function __construct($db, $data) {
        global $error, $mysqli;

        $this->db = $db;

        requireOrError($data['course_type_id'], "Course Type Required");
        requireOrError($data['instructor_id'], "Instructor Required");
        requireOrError($data['dz_name'], "DZ Name Required");
        requireOrError($data['dz_address'], "DZ Address Required");
        requireOrError($data['dz_city'], "DZ City Required");
        requireOrError($data['dz_state'], "DZ State Required");
        requireOrError($data['dz_zip'], "DZ Zip Required");
        requireOrError($data['dz_email'], "DZ Email Required");     
        requireOrError($data['start_date'], "Course Start Date Required");
        requireOrError($data['end_date'], "Course End Date Required");
        requireOrError($data['student_slots'], "Number Of Student Slots Required");

        if(! is_numeric($data['student_slots'])) {
            $error[] = "Invalid Student Slots - Must be a number";
        }

        setError($error);

        if(empty($error)) {
            $add = $mysqli->query("INSERT INTO " . $this->db['courses'] . " (course_type_id, instructor_id, dz_name, dz_address, dz_city, dz_state, dz_zip, dz_email, start_date, end_date, student_slots, notes) VALUES ('$data[course_type_id]', '$data[instructor_id]', '$data[dz_name]', '$data[dz_address]', '$data[dz_city]', '$data[dz_state]', '$data[dz_zip]', '$data[dz_email]', '$data[start_date]', '$data[end_date]', '$data[student_slots]', '$data[notes]')");
            redirectTo("instructors.php");
        }
    }
}

I was going to create a function called “getCourseInfo” where I could pass an ID and it’ll return the course object. Is this the best way to do it, or, should I change the __construct behavior to create. Also, could you give me an example of how I would create/lookup?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:48:11+00:00Added an answer on May 27, 2026 at 7:48 pm

    No. You should have a separate method to create a record that you either pass the parameters as function arguments, or as an array. For example:

    <?php
    class Course {
    
        protected $db;    
    
        public function __construct($db) {
            $this->db = $db;
        }
    
        public function fetchById($id) {
            // perform database query; look-up on ID
        }
    
        public function create($data) {
            // validate your $data
            // create your record
            // return either boolean true or the ID of the newly-created record
            // and errors, either return boolean false or throw an exception
        }
    }
    

    You can then use your class as follows:

    <?php
    
    // create PDO instance in $pdo variable
    
    $course = new Course($pdo);
    
    $data = array(
        'course_type_id' => $_POST['course_type_id'],
        'instructor_id'  => $_POST['instructor_id'],
        // and so on...
    );
    
    if ($course->create($data)) {
        echo 'Course created.';
    }
    else {
        echo 'Error creating course.';
    }
    

    Make sure to escape and sanitize any posted data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an immutable type called Gene , that only has 2 fields:
I have a Class called MyMailSender that binds to the asp.net controller called EmailController.
Is there a specific reason why I should be using the Html.CheckBox , Html.TextBox
I'm trying to find out whether I should be using business critical logic in
I'd like to know situations in which I should consider using a framework other
Right, perhaps I should be using the normal Python lists for this, but here
Are there any blogs, guides, checklists, or controls we should be using to ensure
Should developers avoid using continue in C# or its equivalent in other languages to
Should I be using this method of throwing errors: if (isset($this->dbfields[$var])) { return $this->dbfields[$var];
Or should I be using a totally different server?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.