0

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.

3
  • What does your query() function do.... Commented Jul 28, 2014 at 8:59
  • how are you posting the data to save it? is it on form submit? Commented Jul 28, 2014 at 9:02
  • try dumping the data if it is present or not. Commented Jul 28, 2014 at 9:09

1 Answer 1

1

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>");

    }

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

4 Comments

I am getting an undefined index error if the event name has a space in it. How can I handle that? Also, how would I modify this so that it inserts into logbook at $_SESSION["id"]? I have tried to modify as follows: $str = "INSERT INTO logbook (id, ".$event.") VALUES (?,?)", $_SESSION["id"], $_POST[$event]; but it isnt working.
If it has space you can filter it with function trim - php.net/manual/ru/function.trim.php In my example you will need to change this string: $event = $row['event']; to $event = trim($row['event']); I edit my answer
Do you have a column Test Event in your database logbook? And do you have a key Test Event in your $_POST? Call print_r($_POST) before foreach
Edit to my question. Still having a similar error with the SPACE handling even with the updated form.

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.