3

Im trying to find a solution to add multiple values in my database using the same name of textboxes but I cant. I dont know exactly if there is and solution Please see the below code.

<form action="" method="post">
                        <?php
                        if(isset($_POST['pats'])){
                            $pats = $_POST['pats'];
                            mysql_query("INSERT INTO trainings(pats) VALUES ('$pats')") or die(mysql_error());
                        }
                        ?>
                        <input type="text" name="pats" placeholder="Pats">
                        <input type="text" name="pats" placeholder="Pats">
                        <input type="text" name="pats" placeholder="Pats">
                        <input type="text" name="pats" placeholder="Pats">
                        <input type="text" name="pats" placeholder="Pats">
                        <input type="submit" name="insert" value="Add">
                </form>
Key K
  • 169
  • 13

4 Answers4

0

Try it like this

<form action="" method="post">
     <?php
       if(isset($_POST['pats'])){
         foreach($_POST['pats'] as $pat) {
           mysql_query("INSERT INTO trainings(pats) VALUES ('$pat')") or die(mysql_error());
         }

       }
     ?>
                    <input type="text" name="pats[]" placeholder="Pats">
                    <input type="text" name="pats[]" placeholder="Pats">
                    <input type="text" name="pats[]" placeholder="Pats">
                    <input type="text" name="pats[]" placeholder="Pats">
                    <input type="text" name="pats[]" placeholder="Pats">
                    <input type="submit" name="insert" value="Add">
            </form>

It will save each value from the input as a separate row in the db.

Mubashar Abbas
  • 5,536
  • 4
  • 38
  • 49
  • Because you are assuming he wants the data as comma separated string. Add the solution for distinct inserts as alternative and the answer will be fine. – Philipp Jul 26 '16 at 06:16
  • I think it's a fair assumption, to save multiple values in a single field in the db. How else is he supposed to achieve that? – Mubashar Abbas Jul 26 '16 at 06:18
0

Add the [] to the name attribute of your input elements, they will then get send as an array to the server.

If you want to add the values in different rows:

In your backend you can then iterate over the array you received, check if there are actually values on the corresponding key and insert it into your database.

I added mysql_real_escape_string(), but you should not use this code, as it is still vulnerable to sql injections. See this answer: https://stackoverflow.com/a/60496/3595565

<form action="" method="post">
        <?php
        if(isset($_POST['pats'])){
            foreach ($_POST['pats'] as $pats) {
                if(!empty($pats)){
                    mysql_query("INSERT INTO trainings(pats) VALUES ('" . mysql_real_escape_string($pats) . "')") or die(mysql_error());
                }
            }
        }
        ?>
        <input type="text" name="pats[]" placeholder="Pats">
        <input type="text" name="pats[]" placeholder="Pats">
        <input type="text" name="pats[]" placeholder="Pats">
        <input type="text" name="pats[]" placeholder="Pats">
        <input type="text" name="pats[]" placeholder="Pats">
        <input type="submit" name="insert" value="Add">
</form>
Community
  • 1
  • 1
Philipp
  • 2,787
  • 2
  • 25
  • 27
-1

Use your input element name as like

<form action="" method="post">
<input type="text" name="pats[]" placeholder="Pats">
<input type="text" name="pats[]" placeholder="Pats">
<input type="text" name="pats[]" placeholder="Pats">
<input type="text" name="pats[]" placeholder="Pats">
<input type="text" name="pats[]" placeholder="Pats">
<input type="submit" name="insert" value="Add"> 
</form>
dod29
  • 105
  • 7
-1

In your html you can pass in an array for the name..

<input type="text" name="pats[]" placeholder="Pats">
<input type="text" name="pats[]" placeholder="Pats">
<input type="text" name="pats[]" placeholder="Pats">
<input type="text" name="pats[]" placeholder="Pats">

Note the [] in the name, these are called HTML input arrays..This way php will receive an array of pats..

<?php
    if (isset($_POST['pats'])) {
        implode(',',$pats = $_POST['pats']);
        mysql_query("INSERT INTO trainings(pats) VALUES ('$pats')") or die(mysql_error());
    }
?>

You could use the implode() function to convert an array into a single string with a delimiter

Asfandyar Khan
  • 1,677
  • 15
  • 34