Skip to content
Advertisement

Storing an array of unknown length

I’m letting my users add number inputs in a HTML form and filling them with values. The form is serialized on submit and sent to a PHP file that inserts the data into a MySQL database with PDO. The database table has a bunch of columns for storing other values from the form, but the columns for storing the user added inputs are set up like this:

These columns allows NULL since I don’t know if the user will add any number inputs at all.

Is there a better way of storing these numbers?

Below is the part of my JS for getting form data and sending to LandOwners.php which inserts the values to my database. Code is without the number inputs (I haven’t added them yet since I’m not sure how I should store their data).

Below is the part of LandOwners.php which is inserting to mydatabase without the number inputs (I haven’t added them yet since I’m not sure I how/if should).

Below is the part of LandOwners.php that selects data from my database. I want to (still be able to) get the result as JSON.

Advertisement

Answer

If you find yourself in a situation where you start having column1, column2 on the table – chances are that your design is getting flawed, and you should instead create a separate table – where each columnX gets a row of its own. It’s (nearly) always better to have a separate table if you find yourself having multiple repeating columns on the same table.

That way, you avoid storing comma-separated values in columns, you avoid breaking your code/queries if you suddenly have to introduce another value column{X+1} – and instead can have as many or as few input-values as you need to.

For you, that would be something like a new table called land_owner_input, where you have the value (that you would put in columnX), and a reference to the row in land_owner that the value belongs to.

Typical design pattern would be something like this.

Keep in mind that your land_owner_id in the new table should be of the exact same type and size as the ID its referencing.

You can also create a foreign key constraint between the land_owner_id and the ID of the land_owner table to ensure data integrity.

Once you have your new table, you can query them together by using a LEFT JOIN (or a normal JOIN if you only want to return rows if it has input-values).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement