1

I am hoping to post two selected items from an html form list through a variable in a php postgresql query. I have tried php's extract() function, but got this response:

Warning: extract() [function.extract]: First argument should be an array. As a note, my form options are also the result of a separate query.

My form:

<form method="post" id="report" action="customphp.php">
    <div class="formitem" style="max-width: 200px;">
        <p style="font-color: white" style="font-weight: 800px">Select Media</p>
        <!-- The attribute name is used as the key for the $_POST array. Without a name the form control will not be submitted   -->
        <p><select multiple name="userMedia" class="form-control" id="userMedia">
        <?php
            $conn = pg_connect("LOGIN INFO REDACTED");
            if (!$conn) {
                echo "Did not connect.\n";
                exit;
            }
            $sql = "SELECT medias.name FROM public.medias where medias.startdate >  '2015-01-01'";
            $rs = pg_query($conn, $sql);
            if (pg_num_rows($rs) > 0) {
                // output data of each row
                while($row = pg_fetch_assoc($rs)) {
                    $menu .= "<option value=".$row['name'].">" . $row['name']. "</option>";
                }
            }
            echo $menu;

            pg_close($conn); 
        ?> 
        </select></p>
    </div>                      
    <div class="formitem" style="max-width: 200px" style="margin-bottom: 20px">
        <!-- The attribute name is used as the key for the $_POST array. Without a name the form control will not be submitted   -->
        <p>FROM DATE</p> <input type="DATE" class="textarea" id="userDatea" name="userDatea" style="height: 30px; border-radius: 5px;"><br><br>
        <!-- The attribute name is used as the key for the $_POST array. Without a name the form control will not be submitted   -->
        <p>TO DATE</p> <input type="DATE" class="textarea" id="userDatez" name="userDatez"  style="height: 30px; border-radius: 5px;"><br><br>
    </div>

    <div style="padding-bottom: 120px">
        <input type="submit" class="btn btn-small greyBtn light submit" value="Submit" style="max-width: 200px; max-height: 125px">
    </div>
</form>

Here is customphp.php ...

$datea= $_POST["userDatea"];
$media= extract($_POST['userMedia']);
$datez= $_POST["userDatez"];

if( !empty($_SERVER['REQUEST_METHOD']) && 
(strcasecmp($_SERVER['REQUEST_METHOD'], 'post')===0)  ) {
    // Create connection
    $conn = pg_connect("login info redacted");

    // Check connection
    if (!$conn) {
        echo "Did not connect.\n";
        exit;
    }

    $result = pg_query($conn,
        "SELECT
            date (b.starttime),
            Count(b.starttime) as Plays,
            Count(distinct(b.playerid)) as Stores
         FROM
             public.billing b,
             public.medias m,
             public.players p
         WHERE
             b.mediaitemid = m.id and
             p.id = b.playerid and
             m.name LIKE any (array['$media%']) and
             b.starttime >= date('$datea') and 
             b.starttime < date('$datez') and
             b.channelname = 'GameStop TV'
         GROUP BY
             date (b.starttime)
         ORDER BY
             date (b.starttime);");
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
KevMoe
  • 443
  • 4
  • 21
  • it's not about postgres - `$media= extract($_POST['userMedia']);` so `$_POST['userMedia'])` is not an array. add `var_dump($_POST['userMedia']));` to make sure – Vao Tsun May 17 '17 at 19:55
  • Thanks, I understand that it is not an array, I'm just trying to figure out how to make it one. – KevMoe May 17 '17 at 20:23
  • PHP's `extract()` function creates a variable using the supplied array's key and set's the value of declared variable to the associated value in the array. This is not the function you are looking for. – mickmackusa May 17 '17 at 22:28

0 Answers0