How to enter several data received from the same field?

Asked

Viewed 49 times

1

I have a form in html that has a field where the user goes inserting the data as it shows the code below, this form is dynamic and the user can insert as much information as he wants, in the following case I put only six systems.

My question is: How will I receive this amount of information in PHP by $_POST and insert it into the database? The query I can do, but I know I have to work with a repetition structure, but how?

HTML code:

<div id="container">
<ul class="tags">
<li class="addedTag">
sistema1
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema1" name="tags[]"/>
</li>
<li class="addedTag">
sistema2
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema2" name="tags[]"/>
</li>
<li class="addedTag">
sistema3
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema3" name="tags[]"/>
</li>
<li class="addedTag">
sistema4
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema4" name="tags[]"/>
</li>
<li class="addedTag">
sistema5
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema5" name="tags[]"/>
</li>
<li class="addedTag">
sistema6
<span class="tagRemove" onclick="$(this).parent().remove();">x</span>
<input type="hidden" value="sistema6" name="tags[]"/>
</li>
<li class="tagAdd taglist">
<input type="text" id="search-field"/>
</li>
</ul>
</div>

PHP:

<?php 

    $tags = $_POST['tags']; 
    $query = mysqli_query($dbc, ""INSERT INTO tb_conhecimentos .......
?>

1 answer

2


That one form will return something like this:

Code:

<?php

    var_dump($_POST['tags']);

Exit:

array(6) { [0]=> string(8) "sistema1" 
           [1]=> string(8) "sistema2" 
           [2]=> string(8) "sistema3" 
           [3]=> string(8) "sistema4" 
           [4]=> string(8) "sistema5" 
           [5]=> string(8) "sistema6" }

How do you record these items?

With a foreach:

<?php

    foreach($_POST['tags'] as $value):
        echo $value;
    endforeach;

the $value would be every item shipped within that array of tags.


If the version of PHP for >= 5.2.0 can use filter_input:

<?php

    $tags = filter_input (INPUT_POST, "tags", FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
    foreach($tags as $value):
        echo $value;
    endforeach;
  • The data is written but I noticed that if I use FOREACH I can’t insert the data directly in the database right... pq from php error "Catchable fatal error: Object of class mysqli_result could not be converted to string in" ... using "$query = mysqli_query($dbc, "INSERT INTO tb_tag (tag_id, tag_knowledge_id, tag_name) VALUES (NULL, '$query','$tags');");"

  • If you’re doing it wrong inside the foreach takes the $value variable and inserts in tag_nome! the way you’re wrong anyway.

  • the boy. and truth... I falter my! but still cotninua giving the message "Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\base\incluir_validacao.php on line 113" .... now the code is like this: $query_insert = mysqli_query($dbc, "INSERT INTO tb_tag (tag_id, tag_conhecimento_id, tag_nome) VALUES (NULL, '$query','$value');");

  • what’s inside the $query? can be like this if tag_id is auto_increment mysqli_query($dbc, "INSERT INTO tb_tag (tag_conhecimento_id, tag_nome) VALUES ('$query','$value');");

  • Boy... you killed the syrup! I took $query and used only $value and it inserted... but what $query brings is the last value that was inserted in another table!

  • so, it’s something on your record that’s missing !!!

  • 1

    Really... I’m going to poke around here! Thanks for the help!!!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.