0

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 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@edd, I have made some edits, please use them and I hope no errors will come then.
Still not inserting into database, I don't really know how to fix this
I think it's something wrong with form, because it just wont insert. Is there can be mistake that form is built from other table but have to insert into other table. Need somehow connect table together or something?
can you please paste more code, so that it get more clear what is actually wrong/missing?
0

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;
         }
       }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.