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 8439451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:00:38+00:00 2026-06-10T08:00:38+00:00

I have a user a object that looks like this. $user = new User();

  • 0

I have a user a object that looks like this.

$user = new User();
$user->name = 'Name';
$user->age = 34;

In my database there are five fields,

id, name, age, location, reputation

I have a method that (for all my tables) collects all the fields dynamically from a table with the following SQL. This way I can dynamically get the number of place-holders to put in an INSERT statement.

SHOW FIELDS FROM users
SHOW FIELDS FROM questions
SHOW FIELDS FROM topics

I then have written a create method that counts the number of fields in the table and prepares a string full of placeholders and automatically insert it into my PDO query making this:

INSERT INTO users VALUES (?, ?, ?, ?, ?)

I then take my $user object and make an array out of all the properties that it has and give that array to my PDO prepared statement in the execute parameter. This works fine and the function inserts data into the database.

My problem is that sometimes I dont yet have all the properties for my objects filled in and I therefore populate the rest of the array that feeds the execute function with empty strings. When it reaches a field that takes an integer, it causes a fatal error and doesn’t work. What can I do to be able to insert empty fields into my database dynamically without writing the correct data-type for each field in my code? Because this is all done dynamically I cannot know ahead of time to put an integer or an empty string there. Is there a way to leave out some placeholders altogether? Any other solutions?

  • 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-06-10T08:00:39+00:00Added an answer on June 10, 2026 at 8:00 am

    Set your optional mysql fields to accept NULL.

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

Sidebar

Related Questions

I have an object that I save like this in my controller: @user =
I have some JSON data that looks like this: { data: [{ name:John Smith,
I'm writing an app that monitors the user's location. I have a CLLocationManager object
I have a table in SQL Server which looks like this: ID Code Name
I have a file that looks like this: function UserController(){}; UserController.prototype = { theData:
I'm using C# and .NET and I have a Regex that looks like this
I have an object called users that I use to store user objects that
I have the following code that loads a user's Active Directory DirectoryEntry object from
I have a form that is bind to an object, and when the user
Following scenario: I have user object: ... @Column(name = TITLE) private String title; @NotNull

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.