Query to select value from drop down menu for given range
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
add a comment |
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
1
If you'd check for mysqli errors, you'll notice you're getting a syntax error, becauseWHERE (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
add a comment |
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
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
php html sql input
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, becauseWHERE (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
add a comment |
1
If you'd check for mysqli errors, you'll notice you're getting a syntax error, becauseWHERE (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
add a comment |
1 Answer
1
active
oldest
votes
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));
Its not working when i execute above query my page keep loading
– M K
Jan 1 at 2:44
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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));
Its not working when i execute above query my page keep loading
– M K
Jan 1 at 2:44
add a comment |
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));
Its not working when i execute above query my page keep loading
– M K
Jan 1 at 2:44
add a comment |
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));
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));
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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