<?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
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'] = [ ..