I have a table in MySQL with 5 data fields: id, name, total marks, percentage, and rank. I already fetch and displayed all data, but I want search and display by 3 fields name: total marks, and rank. These 3 will be entered in text boxes.
Please mention the particular query for this 3 fields search.
As you've had to ask this question, I'd like to first of all point you towards the MySQL manual and the PHP manual. However, I'll also give you some pointers.
First of all, you'll need to post these search values to your PHP code. This can be done using a form.
<form method="POST" action="script.php">
<input name="name" type="text" />
<input name="total_marks" type="text" />
<input name="rank" type="text" />
<input type="submit" value="Search" />
</form>
Then, you'll need to access these values in your PHP script as such.
// script.php
$name = mysql_real_escape_string($_POST['name']);
$total_marks = mysql_real_escape_string($_POST['total_marks']);
$rank = mysql_real_escape_string($_POST['rank']);
// I'll leave SQL injection protection up to you
Finally, you'll need to pass these queries to an SQL query to retrieve the items from your database. As you haven't posted your exact scheme, you'll have to modify this to suit your needs. Also, I've left the actual database loading/access to you.
// script.php
$sql = "SELECT * FROM `table` WHERE (
`name` = '{$name}' AND
`total_marks` = '{$total_marks}' AND
`rank` = '{$rank}'
)";
Rather than passing the variables directly to the SQL query and using mysql_real_escape_string or similar functions, I'd look in to using PDO for security and for some database abstraction.
If I understand you correctly
Select *
FROM Keys
WHERE name = 'stringName' AND total_marks = numberTotalMarks AND rank = procent
Related
i am trying to filter multiple products from database using php. i have check box in html page with 4 category,the values will be stored in a array on submit values post to php page , where data is filtered. see below example.
bellow is my html
<input class="le-checkbox" type="checkbox" name="filter1[]" value="<?=$bkey?>" /> <label><?=$bkey?></label>
<input class="le-checkbox" type="checkbox" name="filter2[]" value="<?=$bkey?>" /> <label><?=$bkey?></label>
<input class="le-checkbox" type="checkbox" name="filter3[]" value="<?=$bkey?>" /> <label><?=$bkey?></label>
<input class="le-checkbox" type="checkbox" name="filter4[]" value="<?=$bkey?>" /> <label><?=$bkey?></label>
values are stored in filter1[],filter2[],filter3[],filter4[]
in php
$filter1=$_POST['filter1'];
$filter2=$_POST['filter2'];
$filter3=$_POST['filter3'];
and also implode
i
f(isset($filter1)){
$pbrand= implode(",",$filter1);
}
if(!empty($filter2)){
$pcolor= implode(",",$filter2);
}
if(!empty($filter3)){
$pfab= implode(",", $filter3);
}
now check with mysql
$result = $dbh->prepare("SELECT * FROM products_list WHERE status='Available' AND brand IN (?) OR color IN (?) OR fabric IN (?)");
//$result->bindValue(1, $status);
$result->bindValue(1, $pbrand);
$result->bindValue(2, $pcolor);
$result->bindValue(3, $pfab);
its working fine when i select single checkbox from each category. when i check multiple checkbox from single catagory, its not working and also my condition status='Available' not working i don't understand why its not working
$result = $dbh->prepare("SELECT * FROM products_list WHERE status='Available' AND (brand IN (?) OR color IN (?) OR fabric IN (?))"
brackets is need after AND
I am new to web development. I am trying to implement a database-driven web server using Apache+PHP+SQLite. Using the post method, I try to pass the input values on the html page to the web server as an SQL query to retrieve data inside the database.
Problem description:
Let's say there is a table (named my_table) in my database, having 3 columns, a1, a2 and a3. In the php file, there are 3 input values specified by the end-user, say $v1, $v2 and $v3. The values of $v1, $v2 and $v3 will be matched with patterns in a1, a2 and a3 so that the matched data rows will be retrieved. $v1, $v2 or $v3 can be an empty string, depending on the queries by the user. Formally speaking, if $v1='', $v2='' and $v3='', all of the data in my_table will be retrieved; if $v1='v1_value', $v2='' and $v3='', the data where column a1 has pattern v1_value will be retrieved.
My HTML form tag
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
<tr><td>Item1: </td><td><input type="text" name="v1"></td></tr>
<tr><td>Item2: </td><td><input type="text" name="v2"></td></tr>
<tr><td>Item3: </td><td><input type="text" name="v3"></td>
</table>
<input type="submit", name="submit" value="Submit">
</form>
My PHP code
$v1 = $_POST['v1'];
$v2 = $_POST['v2'];
$v3 = $_POST['v3'];
And I am stuck at here...
$query = "SELECT * FROM my_table WHERE <A_PROPER_CONDITION_STATEMENT>"
That is, how to have a query to retrieve data as I want.
Hope I describe clearly. Thanks for help.
Try this code :-
$query = "SELECT * FROM my_table WHERE 1=1 ";
if(isset($_POST['v1']))
$query .=" and a1=".$_POST['v1'];
if(isset($_POST['v2']))
$query .=" and a2=".$_POST['v2'];
if(isset($_POST['v3']))
$query .=" and a3=".$_POST['v3'];
I researched a lot and found some solution ,probably not a convincing solution for me.Hence i am posting this question,Please help me
I Have A checkbox with same name and different values like
1.cate.php
<form action="mobile-phones-category.php" method="post">
<input type="checkbox" value="samsung" name="mobile[]"> sams
<input type="checkbox" value="nokia" name="mobile[]"> nolkia
<input type="submit" name="submit" value="SUBMIT" class="btn btn-primary pull-right">
</form>
2.) mobile-phones-category.php
I retrieve the values of check box on submit[array format] and want to search from db..I am using normal mysql_query(not pdos)
$search=$_POST["mobile"];
$search_string = implode(', ', $search);
echo $search_string;
Here i Get something like Nokia,Sams
Next I write a single sql query
include('connection.php');
$query = mysql_query("SELECT * FROM tablename where titles like '%$search_string%' ") or die(mysql_error());
What is happening is that only one value in the array is searched and not all the values in array..What changes should i Make so that all the array element should get searched
Thanks and regards
Use IN keyword in your query instead of LIKE
$query = mysql_query("SELECT * FROM tablename where titles IN ($search_string)" ) or die(mysql_error());
Usage Example:
$query = mysql_query("SELECT * FROM tablename where titles IN ('Nokia','Sams')" ) or die(mysql_error());
This will give you records with title Nokia & Sams from the table.
Like User016 said, I would also recommend using the IN Statement. It searchs for several searchterms, splitted by a ,.
You can find the Doc there:
http://dev.mysql.com/doc/refman/5.1/en/comparison-operators.html#function_in
I have a Newsletter in which I have the following checkboxes:
<input type="checkbox" name="assign[]" value="1" >designation_code_1
<input type="checkbox" name="assign[]" value="3" >designation_code_2
<input type="checkbox" name="assign[]" value="4" >designation_code_3
<input type="checkbox" name="assign[]" value="5" >designation_code_4
I have 2 tables namely tbl_designations and tbl_profile and below mentioned is the query which i am using to retrieve values from the respective tables.
SELECT
*
FROM
tbl_designations AS A
JOIN (
SELECT
*
FROM
tbl_profile
) AS B
ON A.is_placement = B.is_placement
AND A.designation_code = B.hierarchy
AND A.designation_id =5
This query gives me 100 records each which are there corresponding to designation_id = "X"
where X =(1,2,3,4)
The value of X is determined by select the checkboxes above.
If I choose only one checkbox(say first checkbox) then I will get the designation_id = 1
and the query will fetch records as per the choice/selection of checkbox and gives the desired result.
My doubt is what query should I write so that when I select more than on checkbox at a time then it pulls out the records from the table as per the choice(s) made by in the checkbox(means corresponding to the values of the checkboxes).
I am using foreach loop to get the values stored in the assign[] array.
Please help
Good morning inquisitive,
you could write something like:
... and A.designation_id in (1, 2, 3)
The values inside the brackets should be replaces by the values returned from the selected checkboxes.
That's silly. Use IN instead, joining each element of $_POST['assign'] with a comma.
This doesn't directly answer your question, but when I was editing your post for formatting changes, I couldn't help but see the SQL.
I have rewritten it for you below (You should never use a select * as a join - just use the whole table and your database will be much happier with you) and you should find better results.
SELECT
*
FROM
tbl_designations AS A
JOIN tbl_profile B
ON A.is_placement = B.is_placement
AND A.designation_code = B.hierarchy
AND A.designation_id =5
try this:
where X in (1,2,3,4)
You can build a comma separated string and use it in the query with a FIND_IN_SET function, e.g. -
...
WHERE FIND_IN_SET(X, '1,2,3,4')
FIND_IN_SET(str,strlist) function.
for select the random number of columns for each refresh we use this:
$sql = mysql_query("SELECT * FROM $Tablename where column = '$bookmark' order by rand() limit 1");
:)
I am trying to use a dynamic select form to send information to a MySQL database. The user will be able to choose their school, and then select their major from within that school's list (all retrieved from a MySQL table). I then want to send that information to a different table in the database to be stored.
This is what I have for the code thus far:
<select name="school">
<php
$sql = "SELECT school_name, school_id FROM school_table ORDER BY school_name";
$query = mysql_query($sql,$conn);
while($row = mysql_fetch_array($states))
{
echo ("<option value=$row[school_id]>$row[school_name]</option>");
}
?>
</select>
I don't know how to make the second select, which would ideally recognize the school_id from the first table and match it with the corresponding school_id on the second table, which also lists the majors at that school. Also, I don't know how to send the form when it is finally done to a MySQL table.
You could either use a simple form to submit the value from the combobox to the server (as HTTP POST or HTTP GET) and use the value as a variable in you SQL statement or you could use a simple AJAX request to send the necessary information to your php script. Anyway, your serverside code should look like this:
//process.php
$myRetrievedValue = $_POST["school"];
$mySqlStm = "SELECT * FROM foo WHERE bar = '".mysql_escape_string($myRetrivedValue)."'";
On the client side you code could look like this (using a simple form and no AJAX stuff):
<form action="process.php" method="post">
<select name="school">
<php $sql = "SELECT school_name, school_id FROM school_table ORDER BY school_name";
$query = mysql_query($sql,$conn); while($row = mysql_fetch_array($states)) {
echo ("<option value=$row[school_id]>$row[school_name]</option>"); } ?>
</select>
<input name="" type="submit" />
</form>
Please remember: Whenever you use a user input in you query use prepared statements (or at least escape methods as above) to avoid SQL injections.
answer is to select from both tables in one SELECT using joins:
http://dev.mysql.com/doc/refman/5.0/en/join.html
INNER JOIN
SELECT `school_table`.`school_name`,
`school_table`.`school_id`,
`2ndTable`.`school_id`,
`2ndTable`.`major`,
FROM school_table,2ndTable
WHERE `school_table`.`school_id`=`2ndtable`.`school_id`
ORDER BY school_name
or a
LEFT JOIN (returning all columns in the left)
SELECT `school_table`.`school_name`,
`school_table`.`school_id`,
`2ndTable`.`major`,
`2ndTable`.`school_id`
FROM school_table
LEFT JOIN on `school_table`.`school_id`=`2ndtable`.`school_id`
ORDER BY school_name