# Dynamic create Php Variables for insert or update reading mysql table structure

**URL:** <https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618>\
**Category:** General PHP Help\
**Created:** [November 18, 2018, 6:03pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618 "2018-11-18T18:03:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhandyx](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rhandyx](https://www.phphelp.com/u/rhandyx)\
**Post date:** [November 18, 2018, 6:03pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/1 "2018-11-18T18:03:51Z")

</div>

Hi.

I have one Db with 40 tables and more 25 fields for each table.

Is lots of variables to assign.

I have make all forms.

But know is the time to put all variables working.

Like:  
$table\_1\_field\_name=$mysqli\_real\_escape\_string ($link, $\_POST[‘name’])

But do this stuff by hand for more than 1200 variables is a crazy thing.

Any way to do it dynamic???

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [November 18, 2018, 6:16pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/2 "2018-11-18T18:16:18Z")

</div>

You would use array(s). Arrays are for sets of data.

If you are still using the xxx\_escape\_string() function, the php mysqli extension, and discrete logic to handle errors, switch to use prepared queries, the much simpler php PDO extension, and use exceptions to handle errors. Doing this will result in the simplest php code and the simplest sql syntax.

You can also dynamically validate the input data as a set and dynamically build any INSERT or UPDATE query by having an array defining the expected/permitted fields, with validation attributes and query attributes, then loop over this defining array to perform the validation using general purpose code and to build the sql statements with the appropriate fields.

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [November 18, 2018, 7:10pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/3 "2018-11-18T19:10:02Z")

</div>

Another comment on this subject.

The $\_POST is itself a variable array. Therefore, you can loop thru it and pull out data without the need  
to assign each name manually. You can loop thru it using something loosely like this:

> foreach ($\_POST as $variable\_name=\>$variable\_data) { }

If you use prepared statements and set up your database field names the same as you use in your form,  
you can use a simple loop like this to build the query for updating data. You can validate each field inside  
the loop and if no errors use the loop to build the query for you. No need to code 1200 items that way.  
Just a few lines to indicate which fields should be text, numeric or Boolean… Saves a lot of time!

---

<div class="post-metadata">

**Author:** ![rhandyx](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rhandyx](https://www.phphelp.com/u/rhandyx)\
**Post date:** [November 18, 2018, 9:29pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/4 "2018-11-18T21:29:20Z")

</div>

Never do it before, let me try it! Im a beginner.

I have old program database for my business complete obsolete in paradox tables,  
I migrate all tables with success to mysql and Im trying to built my own web app.

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [November 18, 2018, 9:42pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/5 "2018-11-18T21:42:33Z")

</div>

Well, let’s discuss this further. Before you get a database set up with so many tables, it might be better to start by redesigning the database. Why would you have 40 tables with 25 fields? My guess is that you never designed it logically. If your 40 tables have the exact same 25 fields, you should just create one and only one table. Just add a master field that would indicate which table it is linked to.

Can you give us any more information about the current 40 tables? Do they use the same fields for all?  
If so, create one table for all with the 25 fields in place. Add one filed and call it “table\_name” and then  
insert all your data from the 40 tables using the table name as a master index. Then, it would be easier to use in the rest of your project. I am not a database designer, but, it seems incorrect to have so many tables.

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [November 18, 2018, 9:43pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/6 "2018-11-18T21:43:05Z")

</div>

> [@rhandyx](#):
>
> I have one Db with 40 tables and more 25 fields for each table.

This smells of a bad DB design. Post an SQL dump of your DB schema for us to review. There is no point writing code against a bad DB design if that is the case.

> [@ErnieAlex](#):
>
> I am not a database designer, but, it seems incorrect to have so many tables.

I _am_ a Database Architect and I concur with @ErnieAlex.

---

<div class="post-metadata">

**Author:** ![rhandyx](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rhandyx](https://www.phphelp.com/u/rhandyx)\
**Post date:** [November 18, 2018, 10:00pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/7 "2018-11-18T22:00:54Z")

</div>

The database is portuguese. I could put a dump. But is a database for insurance broker.  
The common fields is id (number of cliente), number of insurance company, number of product of Company ( like allianz health number 50), number of the policy.

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [November 18, 2018, 10:04pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/8 "2018-11-18T22:04:22Z")

</div>

Well, the question is why so many tables? Take just the first two tables. What is different between them. Are the fields all the same? Just different table names? If so, just combine them into one larger table using the table name as a field. Then, pull out data for any one table name as needed.

This way you are just dealing with 25 fields, not 1200… Does this make sense? If not, @benanamen can help you design it more correctly…

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [November 18, 2018, 10:08pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/9 "2018-11-18T22:08:31Z")

</div>

We don’t necessarily need the data so there is no privacy issues, just the table SQL dump. You can also PM it to us.

---

<div class="post-metadata">

**Author:** ![rhandyx](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rhandyx](https://www.phphelp.com/u/rhandyx)\
**Post date:** [November 18, 2018, 10:20pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/10 "2018-11-18T22:20:03Z")

</div>

how to send it in PM?

It gives error, I cannot send message to user»

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [November 18, 2018, 10:25pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/11 "2018-11-18T22:25:42Z")

</div>

I am leaving for a few hours. Click on benanamen’s ICON and you will see the MESSAGE button.  
Click on that and send it to him. He is better at databases than me, so he will help you out!

---

<div class="post-metadata">

**Author:** ![rhandyx](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rhandyx](https://www.phphelp.com/u/rhandyx)\
**Post date:** [November 18, 2018, 10:28pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/12 "2018-11-18T22:28:12Z")

</div>

No message button

> **[Screenshot](https://prnt.sc/ljzpuj)**
>
> Captured with Lightshot

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [November 18, 2018, 10:30pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/13 "2018-11-18T22:30:45Z")

</div>

Odd! Your image did not show. I can’t view it. Leaving for 3 or four hours… Will check back in them…

---

<div class="post-metadata">

**Author:** ![rhandyx](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rhandyx](https://www.phphelp.com/u/rhandyx)\
**Post date:** [November 18, 2018, 10:31pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/14 "2018-11-18T22:31:58Z")

</div>

No problem . I will try again later.

You could sent to PM and I reply it .

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [November 18, 2018, 10:34pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/15 "2018-11-18T22:34:46Z")

</div>

I just bumped your level. See if you see the message button now.

---

<div class="post-metadata">

**Author:** ![rhandyx](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rhandyx](https://www.phphelp.com/u/rhandyx)\
**Post date:** [November 18, 2018, 10:44pm UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/16 "2018-11-18T22:44:38Z")

</div>

Thank You. I have sent the db dump by Pm.

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [November 19, 2018, 3:48am UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/17 "2018-11-19T03:48:44Z")

</div>

Let us all know if you find a solution to this…

---

<div class="post-metadata">

**Author:** ![rhandyx](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rhandyx](https://www.phphelp.com/u/rhandyx)\
**Post date:** [November 19, 2018, 9:26am UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/18 "2018-11-19T09:26:04Z")

</div>

I´m trying to found. I start today the first test.

---

<div class="post-metadata">

**Author:** ![rhandyx](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rhandyx](https://www.phphelp.com/u/rhandyx)\
**Post date:** [November 19, 2018, 10:47am UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/19 "2018-11-19T10:47:08Z")

</div>

I´m close to build the query but is insecure.

And I have a problem with last variable it writes the comma after it.

Im trying to check if is last of the array and not write the comma.

```
<?

	echo "\$sql= INSERT INTO apolices(";
	foreach ($_POST as $variable_name=>$variable_data) {
		if($variable_name=="submit"){
			$variable_name="";
		}else{						
		echo($variable_name).',';
		}
		
	}
	echo ")" . "Values (";
   	foreach ($_POST as $variable_name=>$variable_data) {
	    if($variable_data==""){
		  $variable_data="NULL";
		} 
		echo($variable_data).',';		
	}echo ")";
	
?>
```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [November 19, 2018, 11:22am UTC](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618/20 "2018-11-19T11:22:39Z")

</div>

Please don’t **unconditionally** loop over all the submitted $\_POST data and blindly use it in your code. Hackers love this, because they submit 100s of their own fields and data values in the hope that you are using variable variables or extract (without using a prefix) so they can override your program variables or if you are putting the external data directly into emails or sql query statements so that they can produce their own emails and sql queries.

Your server-side code must be in **control** of what data it operates on and how it uses it, which is why someone wrote doing this by -

> [@phdr](#):
>
> having an array defining the expected/permitted fields, with validation attributes and query attributes, then loop over this defining array to perform the validation using general purpose code and to build the sql statements with the appropriate fields.

If I have time, I will post an example for producing INSERT/UPDATE queries using the php PDO extension (the ridiculous mysqli code needed to dynamically do this is overly complicated and slow and not worth wasting any time trying.)

[Next page](https://www.phphelp.com/t/dynamic-create-php-variables-for-insert-or-update-reading-mysql-table-structure/27618.md?page=2)
