1

I am trying to insert an array into a table within my database. I am dynamically adding inout fields so I can add team names. What I need to then do is insert those inputs into separate rows into the table but i either just get 'array' in the column or loads of empty fields.

Here is where I am at:

HTML for the form:

<div id="exercises">
    <div class="team">
        <input type="text" name="teamName[]" />
        <button class="remove">x</button>
    </div>
</div>

JQuery that handles the creating new inputs:

<script type="text/javascript">
$('#add_exercise').on('click', function() { 
    $('#exercises').append('<div class="team"><input type="text" name="teamName[]"><button class="remove">x</button></div>');
    return false; //prevent form submission
});

$('#exercises').on('click', '.remove', function() {
    $(this).parent().remove();
    return false; //prevent form submission
}); 
</script>

Then I am wanting to submit that to the database so here is where I am at with that (no security in place and am aware its open to SQL injection).

<?php 

// Config
include '../config.php';

// Get post data
$teamName = $_POST['teamName'];
$dashboardId = $_POST['dashboardId'];

// Check if the daashboard exists already
$sql = "INSERT INTO teams (team_name, dashboard_id) VALUES ('$teamName', '$dashboardId')";

if ($conn->query($sql) === TRUE) {
    echo 'good';
}

?>

If i do a var_dump n test submits I get the following:

array(2) { [0]=> string(6) "team 1" [1]=> string(6) "team 2" }

So I would like to insert a row with the name 'team 1' and another with the name 'team 2'

Any help would be greatly appreciated as I am a little stuck...

2
  • When having a name like: name="teamName[], then $_POST['teamName'] will be an array, so you need to loop it and insert them one by one (or in a batch-insert-query). Commented Jan 13, 2017 at 13:42
  • How would I go about that sorry? I am a little confused, is there a little demo you could show me? Commented Jan 13, 2017 at 13:42

1 Answer 1

1

When having a name like name="teamName[], then $_POST['teamName'] will be an array, so you need to loop it and insert them one by one (or in a batch-insert-query).

Example:

$dashboardId = $_POST['dashboardId'];

foreach($_POST['teamName'] as $teamName) { 
    if (empty($teamName)) {
        // Empty value, continue to next iteration instead
        continue;
    }

    //your db code 
}

You said that you are aware of the SQL Injection risk in your code so I won't go on about that. But I just want to note that if any of the names have an ' character or ends with a \, your code will break, since it isn't escaped.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Magnus, that works perfectly. And I will do that now and make sure everything is set correctly.
@PhpDude Feel free to mark the answer if it works for you.
I just need to wait 3 mins :)

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.