andy
andy

Reputation: 391

insert multiple form data in foreach loop

I am really unsure why I am unable to insert data into my table based on a foreach loop. The form that is Posting the data is created using this code:

<?php

        $eventList = query("SELECT event FROM PrimaryEvents");

        foreach ($eventList as $row)
        {
        //build the form
                echo("<div class='form-group'>");
                        echo("<label for='{$row["event"]}' class='col-lg-2 control-label'>'{$row["event"]}'</label>");
                        echo("<div class='col-lg-10'>");
                        echo("<input type='number' class='form-control' id='{$row["event"]}' name='{$row["event"]}' placeholder='????'>");
                echo("</div>");
                        echo("</div>");

        }

?>

The $eventList array contains a list of event names.

When it comes to updating the database I have tried this code which isnt working:

        $eventList = query("SELECT event FROM PrimaryEvents");
        logConsole(' eventlist is: ', $eventList, true);

        foreach ($eventList as $row)
        {
        query("INSERT INTO logbook ('{$row["event"]}') VALUES (?)", $_POST['{$row["event"]}']);
        }

I have searched and searched but havent found a solution. I have tried a few variations on the $row["event], '$row["event"] etc but have tried so many now I am starting to get confused.

I always treated '' to mean php would take the text from between the '' and not the variable value itself.

Thanks for any help;

Andy

EDIT1:

Code updated to the suggestion below from fiction but I am still getting the following error :

Notice: Undefined index: Test Event in /var/www/NST/public/input.php on line 53

Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Event) VALUES (?)' at line 1 in /var/www/NST/includes/functions.php on line 164

The event name is 'Test Event'. If I use the code on a event with name 'testevent' then it works just fine.

Code based on fiction answer looks like:

        $eventList = query("SELECT event FROM PrimaryEvents");

        foreach ($eventList as $row)
        {    
        $event = trim($row['event']);
        $str = "INSERT INTO logbook (".$event.") VALUES (?)";
        query($str, $_POST[$event]);
        }

Also, I would like all of the event data to be added to the same entry of the database (currently each time through the loop writes a new line and increments the primary id).

Thanks for the help so far!

EDIT2: Form updated now as per fictions suggestion. Code now reads:

<?php

        $eventList = query("SELECT event FROM PrimaryEvents");

        foreach ($eventList as $row)
        {
        //build the form
                echo("<div class='form-group'>");
                        echo("<label for='".$row["event"]."' class='col-lg-2 control-label'>'".$row["event"]."'</label>");
                        echo("<div class='col-lg-10'>");
                        echo("<input type='number' class='form-control' id='".$row["event"]."' name='".$row["event"]."' placeholder='????'>");
                    echo("</div>");
                echo("</div>");

        }

?>

Chrome inspect shows the element is named correctly:

<input type="number" class="form-control" id="Test Event" name="Test Event" placeholder="????">

I am still getting an error when I try to write to the table. print_r($_POST) shows that the INSERT INTO is still trying to write to TEST_EVENT.

Upvotes: 0

Views: 1192

Answers (1)

fiction
fiction

Reputation: 586

You can replace this code inside foreach:

query("INSERT INTO logbook ('{$row["event"]}') VALUES (?)", $_POST['{$row["event"]}']);

with this code:

$event = trim($row['event']);
$str = "INSERT INTO logbook (".$event.") VALUES (?)";
query($str, $_POST[$event]);

This should fix your problem

Also change fix your form-paste code to:

<?php

    $eventList = query("SELECT event FROM PrimaryEvents");

    foreach ($eventList as $row)
    {
    //build the form
            echo("<div class='form-group'>");
                    echo("<label for='".$row["event"]."' class='col-lg-2 control-label'>'".$row["event"]."'</label>");
                    echo("<div class='col-lg-10'>");
                    echo("<input type='number' class='form-control' id='".$row["event"]."' name='".$row["event"]."' placeholder='????'>");
            echo("</div>");
                    echo("</div>");

    }

?>

Upvotes: 1

Related Questions