Frage

I would like to add a WHERE to my query when a gender is selected in a form.

I'm using bindParam to use one dropdownmenu to get the Year but I would like a second bindParam that inserts a 'AND Gender =' to the query and a '$_GET['dropdowngender'] to fill that.

I want to use an if statement to look if the dropdownmenu is being used and if a gender is being selected that the information is being added to the query. This is what I have so far but it doesn't seem to work.

<?php

@include ('conn.php');

$query = "SELECT Name, MAX(Count) AS MaxCount, ZIPcode, Year 
          FROM Name_Data 
          WHERE Year = :year ";
if( isset( $_GET['dropdowngender'] ) {
    $query = $query . " AND Gender = :gender ";
}

$query = $query . " AND Name_Data.Count = 
         ( SELECT MAX( Count ) FROM Name_Data AS f 
           WHERE f.id = Name_Data.id 
         ) GROUP BY ZIPcode";

$query->bindParam(':year', $_GET['dropdownyear'], PDO::PARAM_STR);
if( isset( $_GET['dropdowngender'] ) {
    $query->bindParam(':gender', $_GET['dropdowngender'], PDO::PARAM_STR);
}

$query_send = $db->prepare($query);

$query->execute();

$result = $query->fetchAll (PDO::FETCH_ASSOC);

$jsonResult = json_encode($result);

echo $jsonResult;

?>

my index page

<form method="post" action="#" id="form">
        <select name="dropdownyear" id="menuyear">
            <option selected>Choose a year</option>
        </select>
        <select name="dropdowngender" id="menugender">
            <option value="M">Male</option>
            <option value="F">Female</option>
        </select>
</form>

my jquery script

$( "#form" )
.change(function(){
$(".marker").empty();
var SelectYear= $("#menuyear option:selected").text();
var SelectGender= $("#menugender option:selected").text();

    $.getJSON( "sql.php",  { dropdownyear: SelectYear, dropdowngender: SelectGender }, function(jsonData) {    
        console.log( jsonData );

        $.each( jsonData, function(key,row) {
            var newDiv=$('<div>');
            var Content = row ['Name'] + ' ';
            newDiv.text(Content);
            newDiv.appendTo($("#result"));
    }); // each

    }); //get json
}); // change function
War es hilfreich?

Lösung

Pleas add the error message you get, do you have error reporting turned on, there are some syntax errors that would stop the program immediately. Next to that you are calling bindParam on a string, that won't work either ;).

(Zag dit toevallig op fb, gtz, d)

Edit:

This is untested, but something like this is how it should work:

// I assume you have an instance of the pdo object? I will call it $db for convenience.
require_once 'conn.php';

$q = "SELECT Name, MAX(Count) AS MaxCount, ZIPcode, Year 
      FROM Name_Data 
      WHERE Year = :year";

if( isset( $_GET['dropdowngender'] ) && !empty( $_GET['dropdowngender']) ) {
    $q .= " AND Gender = :gender";
}

$q .= " AND Name_Data.Count = 
         ( SELECT MAX( Count ) FROM Name_Data AS f 
           WHERE f.id = Name_Data.id 
         ) GROUP BY ZIPcode";

$stmt = $db->prepare($q);
$stmt->bindParam(':year', $_GET['dropdownyear'], PDO::PARAM_STR);
if( isset( $_GET['dropdowngender'] ) && !empty( $_GET['dropdowngender']) ) {
    $stmt->bindParam(':gender', $_GET['dropdowngender'], PDO::PARAM_STR);
}
$stmt->execute();

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

$jsonResult = json_encode($result);

echo $jsonResult;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top