Query to select value from drop down menu for given range












-3















I am making metrimonial website where I want to make a search query based on criteria having different age groups in drop down menu



<select class="form-input" id="age" name="age"  >
<option value="Age"> <div class="dot"></div> Age</option>
<option value="1"> <div class="dot"></div> 0-20/option>
<option value="2"> <div class="dot"> </div>21-30</option>
<option value="3"> <div class="dot"></div> 31-40</option>
<option value="4"> <div class="dot"></div> >40 </option>
</select>


I want to display all the users satisfying any of above age criteria
I tried using switch case for above in sql but I didnt get any result .



$age = mysqli_real_escape_string($db, $_POST['age']);
echo $age;

switch ($age) {
case 1: $age = ' AND age BETWEEN 100 AND 130 '; break;
case 2: $age = ' AND age BETWEEN 131 AND 150 '; break;
case 3: $age = ' AND age BETWEEN 151 AND 200 '; break;
}

$result = mysqli_query($db,
"SELECT *
FROM approved_user
WHERE
first_name= '$fn'
OR age=$age "
) or die(mysqli_error($db));

while ($row3 = mysqli_fetch_array($result)) {
$id = $row3['id'];
$fname = $row3['first_name'];
$lname = $row3['surname'];
$dob = $row3['dob'];
$gender = $row3['gender'];
$age = $row3['age'];
}


So please Help.










share|improve this question




















  • 1





    If you'd check for mysqli errors, you'll notice you're getting a syntax error, because WHERE (first_name = 'bob') OR ( AND age between 100 and 130) is not valid syntax. Drop the AND

    – aynber
    Dec 31 '18 at 19:07











  • Still not working

    – M K
    Dec 31 '18 at 19:15






  • 2





    Check for mysqli errors after your query to find out why it's failing. Since you're not using prepared statements, you can try echoing out your query and running it directly in the database to see if it works there.

    – aynber
    Dec 31 '18 at 19:16
















-3















I am making metrimonial website where I want to make a search query based on criteria having different age groups in drop down menu



<select class="form-input" id="age" name="age"  >
<option value="Age"> <div class="dot"></div> Age</option>
<option value="1"> <div class="dot"></div> 0-20/option>
<option value="2"> <div class="dot"> </div>21-30</option>
<option value="3"> <div class="dot"></div> 31-40</option>
<option value="4"> <div class="dot"></div> >40 </option>
</select>


I want to display all the users satisfying any of above age criteria
I tried using switch case for above in sql but I didnt get any result .



$age = mysqli_real_escape_string($db, $_POST['age']);
echo $age;

switch ($age) {
case 1: $age = ' AND age BETWEEN 100 AND 130 '; break;
case 2: $age = ' AND age BETWEEN 131 AND 150 '; break;
case 3: $age = ' AND age BETWEEN 151 AND 200 '; break;
}

$result = mysqli_query($db,
"SELECT *
FROM approved_user
WHERE
first_name= '$fn'
OR age=$age "
) or die(mysqli_error($db));

while ($row3 = mysqli_fetch_array($result)) {
$id = $row3['id'];
$fname = $row3['first_name'];
$lname = $row3['surname'];
$dob = $row3['dob'];
$gender = $row3['gender'];
$age = $row3['age'];
}


So please Help.










share|improve this question




















  • 1





    If you'd check for mysqli errors, you'll notice you're getting a syntax error, because WHERE (first_name = 'bob') OR ( AND age between 100 and 130) is not valid syntax. Drop the AND

    – aynber
    Dec 31 '18 at 19:07











  • Still not working

    – M K
    Dec 31 '18 at 19:15






  • 2





    Check for mysqli errors after your query to find out why it's failing. Since you're not using prepared statements, you can try echoing out your query and running it directly in the database to see if it works there.

    – aynber
    Dec 31 '18 at 19:16














-3












-3








-3








I am making metrimonial website where I want to make a search query based on criteria having different age groups in drop down menu



<select class="form-input" id="age" name="age"  >
<option value="Age"> <div class="dot"></div> Age</option>
<option value="1"> <div class="dot"></div> 0-20/option>
<option value="2"> <div class="dot"> </div>21-30</option>
<option value="3"> <div class="dot"></div> 31-40</option>
<option value="4"> <div class="dot"></div> >40 </option>
</select>


I want to display all the users satisfying any of above age criteria
I tried using switch case for above in sql but I didnt get any result .



$age = mysqli_real_escape_string($db, $_POST['age']);
echo $age;

switch ($age) {
case 1: $age = ' AND age BETWEEN 100 AND 130 '; break;
case 2: $age = ' AND age BETWEEN 131 AND 150 '; break;
case 3: $age = ' AND age BETWEEN 151 AND 200 '; break;
}

$result = mysqli_query($db,
"SELECT *
FROM approved_user
WHERE
first_name= '$fn'
OR age=$age "
) or die(mysqli_error($db));

while ($row3 = mysqli_fetch_array($result)) {
$id = $row3['id'];
$fname = $row3['first_name'];
$lname = $row3['surname'];
$dob = $row3['dob'];
$gender = $row3['gender'];
$age = $row3['age'];
}


So please Help.










share|improve this question
















I am making metrimonial website where I want to make a search query based on criteria having different age groups in drop down menu



<select class="form-input" id="age" name="age"  >
<option value="Age"> <div class="dot"></div> Age</option>
<option value="1"> <div class="dot"></div> 0-20/option>
<option value="2"> <div class="dot"> </div>21-30</option>
<option value="3"> <div class="dot"></div> 31-40</option>
<option value="4"> <div class="dot"></div> >40 </option>
</select>


I want to display all the users satisfying any of above age criteria
I tried using switch case for above in sql but I didnt get any result .



$age = mysqli_real_escape_string($db, $_POST['age']);
echo $age;

switch ($age) {
case 1: $age = ' AND age BETWEEN 100 AND 130 '; break;
case 2: $age = ' AND age BETWEEN 131 AND 150 '; break;
case 3: $age = ' AND age BETWEEN 151 AND 200 '; break;
}

$result = mysqli_query($db,
"SELECT *
FROM approved_user
WHERE
first_name= '$fn'
OR age=$age "
) or die(mysqli_error($db));

while ($row3 = mysqli_fetch_array($result)) {
$id = $row3['id'];
$fname = $row3['first_name'];
$lname = $row3['surname'];
$dob = $row3['dob'];
$gender = $row3['gender'];
$age = $row3['age'];
}


So please Help.







php html sql input






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 1:16









Dharman

5,19662553




5,19662553










asked Dec 31 '18 at 19:00









M KM K

136




136








  • 1





    If you'd check for mysqli errors, you'll notice you're getting a syntax error, because WHERE (first_name = 'bob') OR ( AND age between 100 and 130) is not valid syntax. Drop the AND

    – aynber
    Dec 31 '18 at 19:07











  • Still not working

    – M K
    Dec 31 '18 at 19:15






  • 2





    Check for mysqli errors after your query to find out why it's failing. Since you're not using prepared statements, you can try echoing out your query and running it directly in the database to see if it works there.

    – aynber
    Dec 31 '18 at 19:16














  • 1





    If you'd check for mysqli errors, you'll notice you're getting a syntax error, because WHERE (first_name = 'bob') OR ( AND age between 100 and 130) is not valid syntax. Drop the AND

    – aynber
    Dec 31 '18 at 19:07











  • Still not working

    – M K
    Dec 31 '18 at 19:15






  • 2





    Check for mysqli errors after your query to find out why it's failing. Since you're not using prepared statements, you can try echoing out your query and running it directly in the database to see if it works there.

    – aynber
    Dec 31 '18 at 19:16








1




1





If you'd check for mysqli errors, you'll notice you're getting a syntax error, because WHERE (first_name = 'bob') OR ( AND age between 100 and 130) is not valid syntax. Drop the AND

– aynber
Dec 31 '18 at 19:07





If you'd check for mysqli errors, you'll notice you're getting a syntax error, because WHERE (first_name = 'bob') OR ( AND age between 100 and 130) is not valid syntax. Drop the AND

– aynber
Dec 31 '18 at 19:07













Still not working

– M K
Dec 31 '18 at 19:15





Still not working

– M K
Dec 31 '18 at 19:15




2




2





Check for mysqli errors after your query to find out why it's failing. Since you're not using prepared statements, you can try echoing out your query and running it directly in the database to see if it works there.

– aynber
Dec 31 '18 at 19:16





Check for mysqli errors after your query to find out why it's failing. Since you're not using prepared statements, you can try echoing out your query and running it directly in the database to see if it works there.

– aynber
Dec 31 '18 at 19:16












1 Answer
1






active

oldest

votes


















0














The query that you generate does not make sense.



Given the following value for example :



$age = "  AND age BETWEEN 100 AND 130 ";


This expression :



"SELECT * FROM approved_user WHERE (first_name= '$fn') OR  (age=$age)"


Will actually generate :



"SELECT * FROM approved_user WHERE (first_name= '$fn') OR  (age= AND age BETWEEN 100 AND 130)"


This is not valid SQL. You probably want :



switch ($age) {
case 1: $filter = "100 AND 130"; break;
case 2: $filter = "131 AND 150"; break;
case 3: $filter = "151 AND 200"; break;
}

$result = mysqli_query($db,
"SELECT *
FROM approved_user
WHERE
first_name= '$fn'
OR age BETWEEN $filter"
) or die(mysqli_error($db));





share|improve this answer


























  • Its not working when i execute above query my page keep loading

    – M K
    Jan 1 at 2:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53990617%2fquery-to-select-value-from-drop-down-menu-for-given-range%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The query that you generate does not make sense.



Given the following value for example :



$age = "  AND age BETWEEN 100 AND 130 ";


This expression :



"SELECT * FROM approved_user WHERE (first_name= '$fn') OR  (age=$age)"


Will actually generate :



"SELECT * FROM approved_user WHERE (first_name= '$fn') OR  (age= AND age BETWEEN 100 AND 130)"


This is not valid SQL. You probably want :



switch ($age) {
case 1: $filter = "100 AND 130"; break;
case 2: $filter = "131 AND 150"; break;
case 3: $filter = "151 AND 200"; break;
}

$result = mysqli_query($db,
"SELECT *
FROM approved_user
WHERE
first_name= '$fn'
OR age BETWEEN $filter"
) or die(mysqli_error($db));





share|improve this answer


























  • Its not working when i execute above query my page keep loading

    – M K
    Jan 1 at 2:44
















0














The query that you generate does not make sense.



Given the following value for example :



$age = "  AND age BETWEEN 100 AND 130 ";


This expression :



"SELECT * FROM approved_user WHERE (first_name= '$fn') OR  (age=$age)"


Will actually generate :



"SELECT * FROM approved_user WHERE (first_name= '$fn') OR  (age= AND age BETWEEN 100 AND 130)"


This is not valid SQL. You probably want :



switch ($age) {
case 1: $filter = "100 AND 130"; break;
case 2: $filter = "131 AND 150"; break;
case 3: $filter = "151 AND 200"; break;
}

$result = mysqli_query($db,
"SELECT *
FROM approved_user
WHERE
first_name= '$fn'
OR age BETWEEN $filter"
) or die(mysqli_error($db));





share|improve this answer


























  • Its not working when i execute above query my page keep loading

    – M K
    Jan 1 at 2:44














0












0








0







The query that you generate does not make sense.



Given the following value for example :



$age = "  AND age BETWEEN 100 AND 130 ";


This expression :



"SELECT * FROM approved_user WHERE (first_name= '$fn') OR  (age=$age)"


Will actually generate :



"SELECT * FROM approved_user WHERE (first_name= '$fn') OR  (age= AND age BETWEEN 100 AND 130)"


This is not valid SQL. You probably want :



switch ($age) {
case 1: $filter = "100 AND 130"; break;
case 2: $filter = "131 AND 150"; break;
case 3: $filter = "151 AND 200"; break;
}

$result = mysqli_query($db,
"SELECT *
FROM approved_user
WHERE
first_name= '$fn'
OR age BETWEEN $filter"
) or die(mysqli_error($db));





share|improve this answer















The query that you generate does not make sense.



Given the following value for example :



$age = "  AND age BETWEEN 100 AND 130 ";


This expression :



"SELECT * FROM approved_user WHERE (first_name= '$fn') OR  (age=$age)"


Will actually generate :



"SELECT * FROM approved_user WHERE (first_name= '$fn') OR  (age= AND age BETWEEN 100 AND 130)"


This is not valid SQL. You probably want :



switch ($age) {
case 1: $filter = "100 AND 130"; break;
case 2: $filter = "131 AND 150"; break;
case 3: $filter = "151 AND 200"; break;
}

$result = mysqli_query($db,
"SELECT *
FROM approved_user
WHERE
first_name= '$fn'
OR age BETWEEN $filter"
) or die(mysqli_error($db));






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 1:28

























answered Dec 31 '18 at 19:58









GMBGMB

13.4k2824




13.4k2824













  • Its not working when i execute above query my page keep loading

    – M K
    Jan 1 at 2:44



















  • Its not working when i execute above query my page keep loading

    – M K
    Jan 1 at 2:44

















Its not working when i execute above query my page keep loading

– M K
Jan 1 at 2:44





Its not working when i execute above query my page keep loading

– M K
Jan 1 at 2:44




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53990617%2fquery-to-select-value-from-drop-down-menu-for-given-range%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas