Arrays in MySQL, PHP and select tags

Viewed 40

I'm not familiar with php and MySQL. However, I got a calendar project to change a form for creating new event. Previously, the form took only one username in a select tag. But now It should be able to take more than one usernames and its options come from database. First I made a select tag which has multiple="multiple" as attribute and used chosen library. Then I tried to make a column in in MySQL to save those usernames in database as string. so after getting usernames in api as array I converted them to string with {implode(", ",$names)}. and tried to create new event with these username. However, it save only the first selected user in database.

front:

<select id='users[]' name='users' class="form-select input-sm chosen-select" size="20"  data-placeholder="Begin typing a name to filter..." multiple="multiple">
            <option>--select user--</option>
                <?php
                $sql = "SELECT id, name FROM tbl_users";
                $result = dbQuery($sql);
                while($row = dbFetchAssoc($result)) {
                    extract($row);
                ?>
                <option  id="o3"  value="<?php echo $id; ?>"><?php echo $name; ?></option>
                <?php 
                }
                ?>
        </select>

api:

function bookCalendar() {
    $name       = $_POST['name'];
    $names      = (array)$_POST['users'];
    $userId     = (int)$_POST['userId'];
    $users_id   = (array)$_POST['usersId'];
    $address    = $_POST['address'];
    $phone      = $_POST['phone'];
    $email      = $_POST['email'];
    $rdate      = $_POST['rdate'];
    $rtime      = $_POST['rtime'];
    $bkdate     = $rdate. ' '. $rtime;
    $ucount     = $_POST['ucount'];
    
    //TODO first check if that date has a holiday
    $hsql   = "SELECT * FROM tbl_holidays WHERE date = '$rdate'";
    $hresult = dbQuery($hsql);
    if (dbNumRows($hresult) > 0) {
        $errorMessage = 'You can not book any event on Holiday. Please try another day.';
        header('Location: ../views/?v=DB&err=' . urlencode($errorMessage));
        exit();
    }
    
    

    // converting to string
    $names_together = implode(", ", $names);
    $users_together = implode(", ", $users_id);
    
    
    $sql = "INSERT INTO tbl_reservations (uid, ucount, rdate, status, comments, users_id, bdate) 
            VALUES ($userId, 1, '$bkdate', 'PENDING', '', '$names_together', NOW())";
    dbQuery($sql);

DB:

CREATE TABLE `tbl_reservations` (
  `id` int(10) NOT NULL,
  `uid` int(10) NOT NULL,
  `ucount` int(10) NOT NULL,
  `rdate` varchar(100) NOT NULL,
  `status` varchar(10) NOT NULL,
  `comments` varchar(250) NOT NULL,
  `bdate` varchar(100) NOT NULL,
  `users_id` varchar(1000)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

1 Answers

I've changed it in api:

$names      =  filter_input(
                    INPUT_POST,
                    'users',
                    FILTER_SANITIZE_STRING,
                    FILTER_REQUIRE_ARRAY
                    );
 

Related