0

I know there are similar topics out there but haven't been able to find what I'm looking for. So what I need to do is target a specific input name and foreach loop only that input instead of the whole form.

HTML look something like below.

        <form action"<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" name="table1" method="post">
            <input name="something1" type="text" />
            <input name="something2" type="text" /> 
            <input name="something3" type="text" />
            <input name="something4" type="text" />
            <input name="something4" type="text" />
            <input name="something4" type="text" /> 
            <input name="button"  type="submit" value="Add" />    
        </form>

So I wanna loop every "something4" and just ignore the rest. Is this possible?

Just to explain what I want to do with the value is for every "something4" I'm gonna add a field to my DB and the input the respective input value into that field.

something like below...

        $i = 0;
        foreach ($_POST as $something4 => $something4_value) {
            $add = mysqli_query($connect, "ALTER TABLE 'table' ADD something4$i VARCHAR( 255 ) NOT NULL") or die (mysql_error());
            $sql_update = mysqli_query($con, "UPDATE 'table' SET something4$i='$something_value' WHERE id='$id'") or die (mysql_error());
            $i++;
        }

I hope this make sense! Thank you! :)

0

1 Answer 1

2

Create each of the name="something4" into an array like this:

<input name="something4[]" type="text" />

Then you can do a

foreach($_POST['something4'] as $something4) {
}
Sign up to request clarification or add additional context in comments.

5 Comments

So simple, so beautiful! Thanks alot!
Do you mind if I ask you another question around this topic with foreach loops?
Of course. Although it may be best to simply post a new question.
I will but you seemed to have a good grip around this foreach topic and i'm pretty new to it. I got it to work so it now count the number to put in database and it's values. But now I need to grab ta info on a new page. I tried something like this. foreach ($row['something[$i]'] as $values){ $value .= $values; $i++; } But that din't seem to do it. If I do $row['something0'] I get the first row tho.
It all depends on how you are building the $row array. If it's from the results of a mysqli_query, then your structure will depend on the query. This isn't a suitable place to discuss this, so you should start a new question.

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.