0

Im having trouble with this php mysql issue. I have 5 input fields with the same name, and foreach input field entered i was that to be saved in the database. But at the moment, if i input 2 fields, its inserting them plus 3 blank ones. How do i just insert the ones which are not empty

here is the html

<input type="text" name="tags[]" placeholder="Add Tags" />
<input type="text" name="tags[]" placeholder="Add Tags" />
<input type="text" name="tags[]" placeholder="Add Tags" />
<input type="text" name="tags[]" placeholder="Add Tags" />
<input type="text" name="tags[]" placeholder="Add Tags" />

here is the php

 $tags = $_POST['tags'];

    for($i = 0; $i < count($tags); $i++){
                 $tag = $_POST['tags'][$i];

                 $addtags = $gifs->addtags($tag, $id);
              }
Daniel Lematy
  • 157
  • 1
  • 3
  • 13
  • Duplicate http://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array – Haywire Feb 17 '14 at 15:58

4 Answers4

0

Try this, You can use foreach

$tags = $_POST['tags'];
foreach($tags as $tag) {
  $tag = trim($tag);
  if (! empty($tag)) {
    $addtags = $gifs->addtags($tag, $id);
  }
}
Edgar Ortega
  • 1,672
  • 18
  • 22
Krish R
  • 22,583
  • 7
  • 50
  • 59
  • this won't do what OP is asking, if tag[4] is empty it will be sent to addtags anyway – sathia Feb 17 '14 at 15:59
  • 1
    @sathia, Please check OP requirment, he want to avoid empty insertion into table, this solves his problem! – Krish R Feb 17 '14 at 16:00
  • a) there's no sanitisation of input, b) if the tag is just a space it will be sent to addtags c) if the tag is empty it will be sent to addtags, your foreach is doing the same thing as OP's one – sathia Feb 17 '14 at 16:02
0

well, first thing is to sanitize your input, second thing is to trim each value and add this:

$tag = trim($tag);
if(!empty($tag)){
 ...code
sathia
  • 2,192
  • 2
  • 24
  • 42
  • What do you think that trim() is going to do? I totally get and agree with the !empty but nowhere does the OP say that it should not allow entries with a space – Anigel Feb 17 '14 at 16:03
  • it will trim the variable, removing spaces and other non visible chars in order not to create empty/duplicated tags. what is a tag made of vertical tab good for? – sathia Feb 17 '14 at 16:06
  • what if a user submits this tag: \t\n\n\t, do OP wants this tag? – sathia Feb 17 '14 at 16:06
  • 1
    That is a question for the OP, all I'm saying is that we do not know enough about the real world usage to make any assumptions about what are valid characters in the input. The only case the user mentions is empty values – Anigel Feb 17 '14 at 16:08
  • yeah, I understand your point, but simply reading the code it's obvious he doesn't want invisible characters. at least, I hope so – sathia Feb 17 '14 at 16:10
  • @sathia, Same point for you, Where is your sanitisation of input code? – Krish R Feb 17 '14 at 16:12
  • i said Sanitisation first, thought it was obvious :) ciao – sathia Feb 17 '14 at 16:22
0

Try This

    $tags = $_POST['tags'];

for($i = 0; $i < count($tags); $i++){
             $tag = trim($_POST['tags'][$i]);
    if (!empty($tag))
             $addtags = $gifs->addtags($tag, $id);
          }
Jignesh Patel
  • 1,028
  • 6
  • 10
0

Remove the empty with array_filter():

$tags = array_filter($_POST['tags']);

foreach($tags as $tag) {
    $addtags = $gifs->addtags($tag, $id);
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87