0

I have created a PHP-site which basically is a site with some forms for MySQL-input.

This is how it looks right now. Pretty simple, fill it in and click submit to enter the data into the database.

https://i.sstatic.net/74uDQ.jpg

This is how I want it to look and work. I wan't it to be able to send multiple querys to the database with only one submit-button.

https://i.sstatic.net/DXZkR.jpg

So the question is.. how do I solve this problem in the most simple and efficient way?

2 Answers 2

2

In the name of each field, use e.g. name="opb1[]", this way it will turn into an array (opb = array).

Then you could do like:

foreach($opt1 as $k => $_opt1)
    if(isset($opb2[$k]) && isset($opb3[$k]) .....etc) {
        // use each row e.g. $opb2[$k]...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your HTML should look like:

<input name="name[]" />
<input name="obp1[]" />
<input name="obp2[]" />
   <br />
<input name="name[]" />
<input name="obp1[]" />
<input name="obp2[]" />

and your PHP:

for($i = 0; $i < count($_POST['name']); $i++) {
  // verify if all fields are completed
  if(empty($_POST['name'][$i]) || empty($_POST['obp1'][$i])) { // all fields here
    // add an error
  } else {
    $mysqli->query("INSERT INTO my_table VALUES ('" . $mysqli->real_escape_string($_POST['name'][$i]) . "', '" . $mysqli->real_escape_string($_POST['obp1'][$i]) . "')");
  }
}

1 Comment

But what if I only want to fill in half of the available forms half of the times then?

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.