0

Hi, so im trying to insert data but i get:

Notice: Undefined index: events

html part:

<form method="post" action="seating.php>
    <div class="drop">
        <label>Event : <label/>
        <select name="events" id="dd-events" onchange="getguests(this.value)">
            <option value="" disabled selected >Event</option>
                <?php foreach($retrieve as $r): ?>
                <option value="<?=$r['event_id']?>"><?=$r['event_name']?></option>
                <?php endforeach ?>
        </select>
    </div>

    <input type="hidden" name="table_name[]" value='+tablename+'><span class="tableName">'+tablename+'</span>
    <input type="text" name="guestname[]" class="add-name" size="40">
</form>

php:

<?php
if(isset($_SESSION['sess_user_id']))
{
    if(isset($_POST['save-seating']))
    {
        require "connection.php";
        $name = $_POST['guestname'];
        $table = $_POST['table_name'];
        $event = $_POST['events'];//<- THIS LINE
        $sess = $_SESSION['sess_user_id'];

        $seats = $dbh->prepare("INSERT INTO tables(tuser_id,table_name,event_id,guest_name)
        VALUES(?,?,?,?)");

        for($i = 0; $i < count($_POST['guestname']); $i++)
        {
            if(trim($_POST['guestname'][$i]) != '')
            {
                $seats->bindParam(1, $sess);
                $seats->bindParam(2, $table[$i]);
                $seats->bindParam(3, $event);   
                $seats->bindParam(4, $name[$i]);
                if(!$seats->execute())
                {
                    echo "fail!";
                }
            }
        }
    }
}
?>

Im still a baby at php and all programming so I can't see where I made a mistake. could someone point it out to me please. thank you.

2
Contributors
2
Replies
25
Views
3 Years
Discussion Span
Last Post by nadiam
0

If no event has been selected from the dropdown, you are in trouble since $_POST['events'] possibly does not exist and you get a notice (the script obviously gets executed anyway). What you have to do is to check for existence and act appropriately:

if(isset($_POST['events'])) {
    $event = $_POST['events'];
} else {
    // do something (get user to provide the data or provide a default value yourself...)
}

You might use this technique for all fields that might be empty but are mandatory. or even better do a client side check first using javascript.

Edited by broj1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.