0

I have a custom form wich have ~6 custom intuts and 1-20 can be random how can i get random inputs with $_GET and put them in col atributes:

    <form method="GET" action="products.php" enctype="multipart/form-data">
                                            <li>
                            <label>Name</label>
                            <input required  class="scurtI" type="text" name="prod_name" value="<?= $_GET['prod_name'] ?>" placeholder="">
                        </li>
                                                <li>
                            <label>Description</label>
                            <input required  class="scurtI" type="text" name="prod_descr" value="<?= $_GET['prod_descr'] ?>" placeholder="">
                        </li>
                                                <li>
                            <label>Meta - Title</label>
                            <input required  class="scurtI" type="text" name="prod_meta_title" value="<?= $_GET['prod_meta_title'] ?>" placeholder="">
                        </li>
                                                <li>
                            <label>Meta - Description</label>
                            <input required  class="scurtI" type="text" name="prod_meta_desc" value="<?= $_GET['prod_meta_desc'] ?>" placeholder="">
                        </li>
                                                                        <li>
                            <label>Meta - Keywords</label>
                            <input required  class="scurtI" type="text" name="prod_meta_keys" value="<?= $_GET['prod_meta_keys'] ?>" placeholder="">
                        </li>
                                                                        <li>
                            <label>Price</label>
                            <input required  class="scurtI" type="text" name="prod_price" value="<?= $_GET['prod_price'] ?>" placeholder="">
                        </li>
                                                                                                <li>
                            <label>Currency</label>
                            <select name="prod_curency" class="focusSelect">
                            <option value="RON">RON</option>
                            <option value="USD">USD</option>
                            <option value="EUR">EUR</option>
                          </select>
                        </li>
                                                                                                                        <li>
                            <label>Units</label>
                            <input required  class="scurtI" type="text" name="prod_units" value="<?= $_GET['prod_units'] ?>" placeholder="">
                        </li>
                        <li>
                            <label>Category</label>
                            <select name="prod_ctgy" class="focusSelect">

                            <?php

                            $sql = "SELECT * FROM `categorii` WHERE `cat_id`";
                            $connect = mysqli_query($db_connect, $sql);
                            while (($item = mysqli_fetch_array($connect)))
                                {
                                    ?>
                                     <option
                                    value="<?= $item['cat_id'] ?>"><?= str_repeat('&nbsp;', 6 * $item['section_level']) ?><?= $item['section_name'] ?></option>
                                <?php

                            }

                            ?>


                          </select>
                        </li>
                        <li>
                            <label>Manufacturer</label>
                            <select name="prod_manu" class="focusSelect">

                            <?php

                            $sql = "SELECT * FROM `manufactures` WHERE `id`";
                            $connect = mysqli_query($db_connect, $sql);
                            while (($item = mysqli_fetch_array($connect)))
                                {
                                    ?>
                                     <option
                                    value="<?= $item['manu_code'] ?>"><?= $item['manu_name'] ?></option>
                                <?php

                            }

                            ?>


                          </select>
                        </li>

                        <li>
                         <label>Atributes</label>
                         <select name="prod_atr_id" class="focusSelect">

                            <?php
                            if (isset($_GET['prod_atr_id'])){
                                echo "<option selected value=".$_GET['prod_atr_id'].">Selected</option>";
                            }

                            $sql = "SELECT * FROM `products_atributes` WHERE `prod_atr_id`";
                            $connect = mysqli_query($db_connect, $sql);
                            while (($item = mysqli_fetch_array($connect)))
                                {
                                    ?>
                                     <option
                                    value="<?= $item['prod_atr_id'] ?>"><?= $item['prod_atr_name'] ?></option>
                                <?php

                            }

                            ?>
                          </select>
                           <button class="btn btn-sm btn-primary" type="submit" name="next_add_product">Next</button> 
                        </li>
                                <?php 
            if(isset($_GET['prod_atr_id']))

    {
        list_atributes($_GET['prod_atr_id']);   

    ?> 

                            <li>
                                <label>Main Image </label>
                                <input type="file" name="prod_image" style="">
                            </li>
                            <li>
                                <input class="btn btn-sm btn-primary" type="submit" name="prod_add_new" value="Add Product">
    </li>                           
                        </form>

    <?php           
    }
        if(isset($_GET['prod_add_new']))

    {   
    //$prod_name = $_GET['prod_name']


    foreach($_GET as $key => $value){
      echo $key . " : " . $value . "<br />\r\n";

    }





    //print_r($all_gets);

    }
    ?>   



    function list_atributes($id)
{
    global $db_connect;
    $atribute_detect = $id;
            if ($atribute_detect > 0){

                $sql = "SELECT * FROM `products_atributes` WHERE `prod_atr_parent` = '".$atribute_detect."'";
                $connect = mysqli_query($db_connect, $sql);
                $count = mysqli_num_rows($connect);
                if ($count == 0)
                    {
                        $sql = "SELECT * FROM `products_atributes` WHERE `prod_atr_id` = '".$atribute_detect."'";
                    }
                if ($count > 0)
                    {
                        $sql = "SELECT * FROM `products_atributes` WHERE `prod_atr_parent` = '".$atribute_detect."'";
                    }

                $connect = mysqli_query($db_connect, $sql); 
                while (($item = mysqli_fetch_array($connect)))
                {
                    echo "
                        <li>
                            <label>".$item['prod_atr_name']."</label>
                            <input required  class='scurtI' type='text' name='".$item['prod_atr_code']."' value='".$_GET[$item['prod_atr_code']]."' >
                        </li>
                    ";

                }


            }



}

enter image description here

And now i get this restults

  • prod_name : sadfasd
  • prod_descr : asdfasd
  • prod_meta_title : ghdf
  • prod_meta_desc : gs
  • prod_meta_keys : asdf
  • prod_price : 235243
  • prod_curency : RON
  • prod_units : 1243123
  • prod_ctgy : 16
  • prod_manu : 70123825
  • prod_atr_id : 1
  • 26126933 : 1243ghz
  • 40320861 : 2000mb
  • 47789694 : 64bit
  • prod_image : log_masacru.png
  • prod_add_new : Add Product

How can i get this values, for all with prefix prod_ i have special cols, and for that with numbers i must put them all in a single col and after i must use them to extract as id=40320861 value=2000mb, etc. How can i build the code to add in db random number of $_GET values in a single column enter image description here

Malasuerte94
  • 1,454
  • 3
  • 14
  • 18
  • Check this query `$sql = "SELECT * FROM manufactures WHERE id=????";` – Saty Sep 09 '15 at 13:10
  • I don't have problems with the actual code, because is returnig right `prod_manu : 70123825` i just need to extrat that random values and insert them. – Malasuerte94 Sep 09 '15 at 13:19

1 Answers1

1

My understanding of the question is that you need to save the following GET variables :

26126933 : 1243ghz
40320861 : 2000mb
47789694 : 64bit

in a single db column where the keys are randomly generated ( unknown).

ANSWER:
Save the attributes in a multidimensional array, for this set their name values in the html to something like prod_attr_value[][$RANDOM_NUMBER], this will make the attributes values in the array $_GET['prod_attr_value'] .

Then to save in these values in a single db column, you can simply json_encode($arr).

hth

UPDATE:

<input required  class='scurtI' type='text' name='".$item['prod_atr_code']."' value='".$_GET[$item['prod_atr_code']]."' >

The above creates a $_GET var with a key having the name (not value) of the value of $item['prod_atr_code'].

Changing the name to something like prod_attr_values_arr[][$item['prod_atr_code']], will on sending the request create a array $GET['prod_attr_values_arr']

gyaani_guy
  • 3,191
  • 8
  • 43
  • 51
  • The 26126933 , 40320861 , 47789694 are from a database (they are ID CODES) but i can have over 20 i must detect them in php and put all them in a single array variable. My raw question is how can i detect all extra inputs if i can exclude predefinided and get the rest of them and put in a variable is ok or something like this – Malasuerte94 Sep 09 '15 at 14:32
  • Basically you have to modify the HTML, so that the $_GET['prod_attrr_var'] becomes an array . Please see this: http://stackoverflow.com/questions/20184670/html-php-form-input-as-array – gyaani_guy Sep 09 '15 at 14:40
  • I have added the function wich generate me the `random` inputs check top, thanks! – Malasuerte94 Sep 09 '15 at 14:44