<?php ini_set('display_errors', 1); error_reporting(E_ALL); include ('engine.php'); $db = new Database; $ins = array( 'type_product' => (array( 'post' => $_POST['type_product'], 'name' => 'Π’ΠΈΠΏ ΠΏΡ€ΠΎΠ΄ΡƒΠΊΡ‚Π°', 'type' => 'text', )) , 'company_product' => (array( 'post' => $_POST['company_product'], 'name' => 'Компания', 'type' => 'text', )) , 'color_product' => (array( 'post' => $_POST['color_product'], 'name' => 'Π¦Π²Π΅Ρ‚ ΠΏΡ€ΠΎΠ΄ΡƒΠΊΡ‚Π°', 'type' => 'text', )) , 'line_product' => (array( 'post' => $_POST['line_product'], 'name' => 'Π›ΠΈΠ½Π΅ΠΉΠΊΠ°', 'type' => 'text', )) , 'bend_product' => (array( 'post' => $_POST['bend_product'], 'name' => 'Изгиб', 'type' => 'text', )) , 'thickness_product' => (array( 'post' => $_POST['thickness_product'], 'name' => 'Π’ΠΎΠ»Ρ‰ΠΈΠ½Π°', 'type' => 'text', )) , 'long_product' => (array( 'post' => $_POST['long_product'], 'name' => 'Π”Π»ΠΈΠ½Π°', 'type' => 'text', )) , 'unit_of_measure_product' => (array( 'post' => $_POST['unit_of_measure_product'], 'name' => 'Π•Π΄Π΅Π½ΠΈΡ†Π° измСрСния', 'type' => 'text', )) , 'price_product' => (array( 'post' => $_POST['price_product'], 'name' => 'Π¦Π΅Π½Π°', 'type' => 'text', )) , 'mix_product' => (array( 'post' => $_POST['mix_product'], 'name' => 'Набор', 'type' => 'text', )) , 'quantity_product' => (array( 'post' => $_POST['quantity_product'], 'name' => 'ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ', 'type' => 'text', )) , 'lines_product' => (array( 'post' => $_POST['lines_product'], 'name' => 'Π›ΠΈΠ½ΠΈΠΈ', 'type' => 'text', )) , 'size_product' => (array( 'post' => $_POST['size_product'], 'name' => 'Π Π°Π·ΠΌΠ΅Ρ€', 'type' => 'text', )) , 'composition_product' => (array( 'post' => $_POST['composition_product'], 'name' => 'Бостав', 'type' => 'text', )) , 'description_product' => (array( 'post' => $_POST['description_product'], 'name' => 'ОписаниС', 'type' => 'text', )) , 'image_product' => (array( 'post' => 'images/' . 'basename' . $_FILES['image_product']['name'], 'name' => 'Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅', 'type' => 'file', )) , ); if (isset($_POST['submit'])) { if (is_array($ins)) { $db->insert('product', $ins); move_uploaded_file($_FILES['image_product']['tmp_name'], $ins['image_product']['post']); } } ?> <form method="post" enctype="multipart/form-data"> <?php foreach($ins as $key => $value) { echo '<p><input type="' . $value['type'] . '" name="' . $key . '" placeholder="' . $value['name'] . '"></p>'; } ?> <input type="submit" name="submit" value="ΠžΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ"> </form> 
  • And what do you think is not oversimplified? - Manitikyl

2 answers 2

since no one writes, I note that the check in the code

 is_array($ins) 

will always return true , since actually you have an array defined above.

To increase readability, use the [] array() short entry instead of array() .

Field descriptions can be somewhat simplified by replacing the view code

 $ins = array( 'type_product' => (array( 'post' => $_POST['type_product'], 'name' => 'Π’ΠΈΠΏ ΠΏΡ€ΠΎΠ΄ΡƒΠΊΡ‚Π°', 'type' => 'text', )) , 'company_product' => (array( 'post' => $_POST['company_product'], 'name' => 'Компания', 'type' => 'text', )) , 

on an array of text field definitions

 $txtFields = [ 'type_product' => "Π’ΠΈΠΏ ΠΏΡ€ΠΎΠ΄ΡƒΠΊΡ‚Π°", 'company_product' => "Компания", .... ]; 

with which then get

 $data = array_map(function($k, $v) { return [ 'post' => $_POST[$k], 'name' => $v, 'type' => 'text', ]; }, array_keys($txtFields), $txtFields); 

well, or in any other way.

In any case, you should separate the description of the configuration of the fields, and getting their values ​​from $_POST . Both logically in code and structurally in different arrays. The above piece of code forms an array of your template, however, because, for example, it makes no sense to transfer the string Π’ΠΈΠΏ ΠΏΡ€ΠΎΠ΄ΡƒΠΊΡ‚Π° to the insert method in the database? The config must be stored in a watery place, given in another.

    Almost all fields are typical. Make the difference in a separate smaller array and then collect the full version for query to the database:

     $fields = [ 'type_product' => 'Π’ΠΈΠΏ ΠΏΡ€ΠΎΠ΄ΡƒΠΊΡ‚Π°', 'company_product' => 'Компания', 'color_product' => 'Π¦Π²Π΅Ρ‚ ΠΏΡ€ΠΎΠ΄ΡƒΠΊΡ‚Π°', .. 'composition_product' => 'Бостав', 'description_product' => 'ОписаниС', ]; $ins = []; foreach ($fields as $n => $v) { $ins[$n] = ['post' => $_POST[$v], 'name' => $n, 'type' => 'text']; } $ins['image_product'] = [ ..