How to define name='' value dynamic php?

Asked by At

I have a dynamic form where input name='' value is from database, but how can I define it in function? Or you have any better suggestions how to write this code.

<?php

            $query = "SELECT * FROM product_types";
            $input_product_attribute = mysqli_query($connection,$query);

            while($row = mysqli_fetch_array($input_product_attribute)) {
                $product_type_ID = $row['Product_type_ID'];
                $label_name = $row['Product_type_label_name'];
                $product_type_attribute = $row['Product_type_attribute'];
                $label_comment = $row['Product_type_label_comment'];

        ?>

        <div id='div_<?php echo $product_type_ID ?>' class="divParameter" style="display: none;">
        <div class="form-group">
            <label for='<?php echo $label_name ?>'><?php echo $label_name ?></label>
            <input id='<?php echo $label_name ?>' type="text" name='<?php echo $product_type_attribute ?>' class="form-control">
            <label><?php echo $label_comment ?></label>
        </div>
        </div>


        <?php } ?>
<?php
function createRows(){

if (isset($_POST['submit'])) {   
global $connection;
file_put_contents('debug.txt', json_encode($_POST)."\n", FILE_APPEND );
$productType = $_POST['select_box'];
$productAttribute = $_POST['?']; //PROBLEM!!

$productType = mysqli_real_escape_string($connection, $productType );
$productAttribute = mysqli_real_escape_string($connection, $productAttribute );

  $query = "INSERT INTO products(Product_type,Product_size) ";
  $query .= "VALUES ('$productType', '$productAttribute') ";

}
?>

2 Answers

1
anuj arora On Best Solutions

You can make your form and php code is below way

HTML

<label for='<?php echo $label_name ?>'><?php echo $label_name ?></label>
//solution to your problem, see name attribute
<input name='dynamic_values[<?php echo $product_type_attribute ?>]' id='<?php echo $label_name ?>' type="text" class="form-control">

PHP

<?php
function createRows(){

     if (isset($_POST['submit'])) {   
         global $connection;
         file_put_contents('debug.txt', json_encode($_POST)."\n", FILE_APPEND );
         $productType = $_POST['select_box'];
         //$productAttribute = $_POST['?']; //PROBLEM!! //problem solved below

         $productType = mysqli_real_escape_string($connection, $productType );
         //$productAttribute = mysqli_real_escape_string($connection, $productAttribute );

         //solution to your problem
         if( !empty($_POST['dynamic_values']) ) {
             foreach( $_POST['dynamic_values'] as $val ) {
                 $query = "INSERT INTO products(Product_type,Product_size) ";
                 $val = mysqli_real_escape_string($connection, $val );
                 $query .= "VALUES ('$productType', '$val') ";
             }
         }
     }
 }
 ?>

If you face any problem, let me know in comments.

0
Mantykora 7 On

I do it this way:

function update_products($products), function insert_products($products), unction find_products_by_id($id, $options=[])....

EXAMPLE

 function insert_products($products) {
            global $db;

            $errors = validate_products($products);
            if(!empty($errors)) {
              return $errors;
            }

            $sql = "INSERT INTO products ";
            $sql .= "(cat_id, name, code, content) ";
            $sql .= "VALUES (";
            $sql .= "'" . db_escape($db, $products['cat_id']) . "',";
            $sql .= "'" . db_escape($db, $products['name']) . "',";
            $sql .= "'" . db_escape($db, $products['code']) . "',";
            $sql .= "'" . db_escape($db, $products['content']) . "'";
            //echo $sql;
            $sql .= ")"; 
            $result = mysqli_query($db, $sql);
            // For INSERT statements, $result is true/false
            if($result) {
            return true;
            } else {
            // INSERT failed
            echo mysqli_error($db);
            db_disconnect($db);
            exit;
         }
       }