search by category with php mysql - php

hello im using this code to do search
<form action="arama.php" method="get">
<input type="text" name="lol">
<select name='kategori'>
<option value="tum">Tum kategoriler</option>
<?
while ($kat = mysql_fetch_array($kategori_isim)) {
echo "
<option value=".$kat[kategori_isim].">".$kat[kategori_isim]."</option>";
}
?>
</select>
<input type="submit" value="ara">
</form>
<?
$lol = mysql_real_escape_string($_GET['lol']);
$kategori = mysql_real_escape_string($_GET['kategori']);
if ($kategori == "tum") {
$ara = mysql_query("select * from dosyalar where baslik like '%$lol%'");
}
else {
$ara = mysql_query("select * from dosyalar where baslik like '%$lol%' order by kategori = '%$kategori%'");
}
?>
search by term works but not listing by kategori.. what can i do?

I'm not sure I understand the question (I can't really figure out what those fields mean), but I think that your second query should be more like:
$ara = mysql_query("SELECT * FROM dosyalar WHERE kategori LIKE '%$kategori%'");
ORDER BY only specifies how to sort the results, you can only use a column name, not a check as in your code.
Extending my answer: the ORDER BY kategori = '%$kategori%' isn't a syntax error but I don't think it does anything useful. The check kategori = '%$kategori%' will always be false (unless you have a value with percent signs both at start and at end) so the ORDER BY clause will be useless and you will just do the same select you're doing in the other branch of the if block.
Specifying ORDER BY kategori = '$kategori' will return 0 for all the records where kategori is not equal to $kategori, 1 for the ones where it matches. This will basically sort all the matching rows at the end of your query.

Maybe the query failed. In that case mysql_query() returns FALSE and mysql_error() returns a description of the error.
Try
<form action="arama.php" method="get">
<input type="text" name="lol" />
<select name='kategori'>
<option value="tum">Tum kategoriler</option>
<?php
while ( false!==($kat=mysql_fetch_array($kategori_isim)) ) {
$htmlIsim = htmlspecialchars($kat['kategori_isim']);
echo ' <option value="', $htmlIsim, '">', $htmlIsim, "</option>\n";
}
?>
</select>
<input type="submit" value="ara" />
</form>
<?php
$lol = isset($_GET['lol']) ? mysql_real_escape_string($_GET['lol']) : '';
$kategori = isset($_GET['kategori']) ? mysql_real_escape_string($_GET['kategori']) : '';
$query = "select * from dosyalar where baslik like '%$lol%'";
if ( 'tum'!==$kategori ) {
$query .= "order by kategori = '%$kategori%'";
}
$ara = mysql_query($query) or die( htmlspecialchars(mysql_error().': '.$query) );
echo '<pre>Debug: ', mysql_num_rows($ara) , ' records in the result set for ', htmlspecialchars($query), "</pre>\n";
?>

Related

UPDATE an imploded multiple selection into database

I have a problem while updating a to database, It happens to add the new value beside the old value into the database for example
Current Database Tags: tag1,tag2
Form GET Tags: [tag1][tag2]
Form Edited Tags: [tag1][tag2][tag3][tag4]
Updated Database Tags: tag1,tag2,tag1,tag2,tag3,tag4
GET
$query = "SELECT * FROM data WHERE id = $id";
$edit = mysqli_query($dbc, $query);
while($row = mysqli_fetch_assoc($edit)){$tags = $row['tags'];}
POST
$tags = implode(",",$_POST['tags'];
$query = "UPDATE data SET tags= '$tags' WHERE id = $id";
<form method="post" action="">
<select id="tags" name="tags[]" multiple="multiple">
<?php foreach ($tags as $tag) {echo "<option value'$tag' selected>$tag</option>";} ?>
<option>tag1</option>
<option>tag2</option>
<option>tag3</option>
<option>tag4</option>
</select>
<button type="submit" name="update">Submit</button>
</form>
There are few issues with your code, such as:
$tags is not an array. See the below statement in your while() loop,
$tags = $row['tags'];
So you can't use it in foreach loop like that. Use explode() function to split the string and get the tags in an array, like this:
$tags = explode(",",$row['tags']);
And then use this $tags array in your form, which is explained below.
Syntax error here,
$tags = implode(",",$_POST['tags'];
^ missing closing )
Even you get the tags as an array(as pointed above), you don't have to use that foreach loop either, it will unnecessarily append additional/redundant tags in your <select> element. better use in_array() function to check the tag value is present in $tags array or not and make it selected accordingly
value attribute is missing from <option> tags.
Place the SELECT operation below the UPDATE operation, otherwise you'll get old tag values from the SELECT operation even if you update the tags using the form.
So your code should be like this:
if(isset($_POST['update'])){
$tags = implode(",",$_POST['tags']);
$query = "UPDATE data SET tags= '$tags' WHERE id = $id";
mysqli_query($dbc, $query);
}
$query = "SELECT * FROM data WHERE id = $id";
$edit = mysqli_query($dbc, $query);
while($row = mysqli_fetch_assoc($edit)){
$tags = explode(",",$row['tags']);
}
<form method="post" action="index.php">
<select id="tags" name="tags[]" multiple="multiple">
<option value="tag1"<?php if(in_array('tag1', $tags)){ echo ' selected="selected"'; } ?>>tag1</option>
<option value="tag2"<?php if(in_array('tag2', $tags)){ echo ' selected="selected"'; } ?>>tag2</option>
<option value="tag3"<?php if(in_array('tag3', $tags)){ echo ' selected="selected"'; } ?>>tag3</option>
<option value="tag4"<?php if(in_array('tag4', $tags)){ echo ' selected="selected"'; } ?>>tag4</option>
</select>
<button type="submit" name="update">Submit</button>
</form>

How to filter based on two arguments from php, in a SQL database

I have a MySQL database, and the table I need to work with has 9 columns of information. My goal is to be able to filter, based on two arguments. For instance, the table is about students so it has data for first name, last name, id, course they are signed up for, status, occupation age and another 2 fields that are not that important. I need to be able to filter, based on the student's status and/or the course.
So far, I managed to get the php work done, with a form and a select tag, to filter based on status, but I have no idea how to add the second part. The done thing should be able to filter, based on status only, based on course only, or based on the selected status and course. The code looks like this:
if (isset($_POST['filter'])) {
$search_term = mysqli_real_escape_string($conn, $_POST['filter_status']);
$q .= " WHERE status = '$search_term'";
}
echo $q;
<form method="POST" action="index.php">
<select name="filter_status" >
<option value="confirmed">confirmed</option>
<option value="declined">declined</option>
<option value="rejected">rejected</option>
<option value="pending">pending</option>
<option value="unconfirmed">unconfirmed</option>
</select>
<input type="submit" name="filter">
</form>
This works correctly, I have it a second time for the second criteria, but they don't work together.
try to change,
$q .= " WHERE status = '$search_term'";
to
$q .= " WHERE CONCAT_WS(',',status,course) like %'$search_term'%";
you can add as many columns after course.
$filter_status = $_POST['filter_status'];
$course = $_POST['course'];
$where = 'WHERE 1';
$where .= $filter_status ? " AND status = {$filter_status}" : '';
$where .= $course ? " AND course = {$course}" : '';
Did you mean this? when user select course and filter_status use this two conditions, on the other hand use one of conditions which is being selected.
The WHERE 1 will always be TRUE, so it can be followed by AND statements
Use the term AND or OR in your query after WHERE
WHERE status = '$search_term' AND course = '$something'
Thank you all for your input. It helped nudge me in the right direction. The code that ended up doing what I needed is as follows. It's not very elegant, but it does the job well:
$q = "SELECT *
FROM students";
if (isset($_POST['filter'])) {
if ($_POST['filter_status'] == null) {
$search_term2 = mysqli_real_escape_string($conn, $_POST['filter_course']);
$q .= " WHERE course = '$search_term2'";
} elseif ($_POST['filter_course'] == null) {
$search_term = mysqli_real_escape_string($conn, $_POST['filter_status']);
$q .= " WHERE status = '$search_term'";
} else {
$search_term = mysqli_real_escape_string($conn, $_POST['filter_status']);
$search_term2 = mysqli_real_escape_string($conn, $_POST['filter_course']);
$q .= " WHERE status = '$search_term' AND course = '$search_term2'";
}
}
And the form:
<form method="POST" action="index.php">
<select name="filter_status" >
<option value= ""></option>
<option value="confirmed">confirmed</option>
<option value="declined">declined</option>
<option value="rejected">rejected</option>
<option value="pending">pending</option>
<option value="unconfirmed">unconfirmed</option>
</select>
<select name="filter_course">
<option value= ""></option>
<option value="php">php</option>
<option value="java">java</option>
</select>
<input type="submit" name="filter">
</form>

Can't get the values from multiple select with $_GET request

I'd like some help please.
I'm sending with a GET request from a multiple select field the selected values to my proccess page (archive.php)
<form id="form1" class="four columns" action="archive.php" method="get" name="form1">
<select id="select2" multiple="multiple" name="location[]">
<option value="103001000">value1</option>
<option value="103002000">value2</option>
<option value="103003000">value3</option>
<option value="103004000">value4</option>
</select>
I get the selected locations on my url like this
location[]=103002000&location[]=103003000
and in the archive.php I'm trying to fetch data from the database like this
if (( isset($_GET['location']) && !empty($_GET['location']) )) {
die(var_dump($_GET['location'])); // the var_dump doesn't return an array at all
$loc = implode(', ', $_GET['location']);
$sql="SELECT * FROM locations WHERE AreaID IN (". $loc.")";
}
but I'm getting the following error: Error: Unknown column 'Array' in 'where clause'.
How can I fix this?
try with as below it might be simple
if (( isset($_GET['location']) && !empty($_GET['location']) )) {
die(var_dump($_GET['location'])); // the var_dump doesn't return an array at all
//$loc = implode(', ', $_GET['location']);
$_GET['location'] = array('first'=>'loc1','second'=>'loc2','third'=>'loc3');
$loc = implode('","', $_GET['location']);
$loc ='"'.$loc.'"';
$sql='SELECT * FROM locations WHERE AreaID IN ('.$loc.')';
echo $sql;
}
result query
SELECT * FROM locations WHERE AreaID IN ("loc1","loc2","loc3")

Get value form same table

I have dropdown menu with 3 values.
and here is my table (table name is Sms)
What I want to do? Example : If I choose 2,49 and press submit, then I get sonum value.
This is my form
<div class="col_12" style="margin-top:100px;">
<div class="col_6">
<label for="asukoht">Vali Hind</label>
<form class="vertical" method="GET">
<select name="hind">
<option value="1">-- Vali --</option>
<?php
// Tegin dropdown menüü, kust saab valida komponendi, mille alla see pilt läheb
$andmed = mysql_query("SELECT * FROM Sms");
// Dropdown menüü
while($rida = mysql_fetch_array($andmed)){
echo '<option value="'.$rida['id'] . '">'.utf8_encode($rida['hind'] ). '</option>';
}
?>
<input type="submit" name="add" id="add">
</form>
I tried something like this
if(mysql_query("DESCRIBE `Sms`")) {
$sql = "SELECT sonum FROM `Sms`";
echo $sql;
}
I think it should be pretty easy, but I'm looking for a solution and I didnt found it.
Thank you for helping !
You need to work on SQL and Loop.
Based on your code:
if(mysql_query("DESCRIBE `Sms`")) {
$sql = "SELECT sonum FROM `Sms`";
echo $sql;
}
First we do change the query including $_GET parameter.
So this:
$sql = "SELECT sonum FROM `Sms`";
Will become:
$sql = "SELECT sonum FROM `Sms` WHERE id = ".$_GET['hind'];
It will be better if you check that the var exist and is setted with something like:
if(isset($_GET['hind']) && is_numeric(trim($_GET['hind']){//Code here}
But it is off-topic.
Now let's change echo $sql; with a loop, we need to loop and fetch the data.
while($result = mysql_fetch_array($sql)){
echo '<option value="'.$result ['id'] . '">'.utf8_encode($result ['hind'] ). '</option>';
}
I've only changed what i know, you know your system ^_^
You should do:
$sql = "SELECT sonum FROM Sms WHERE id = ".$_GET['hind'];
Then do :
echo mysql_query($sql);
$sql = "SELECT sonum FROM Sms WHERE id = ".$_GET['hind'];
while($rida = mysql_fetch_array($sql)){
echo '<option value="'.$rida['id'] . '">'.utf8_encode($rida['hind'] ). '</option>';
}
Do not use MYSQL queries...try MySQLi or PDO with prepared statement.

Making a dynamic drop down menu sticky in php

I have a dynamic drop down menu on a PHP form which is working fine in that it retrieves/inputs the right id and will not process the form if no option is collected.
However, I am not sure how to make it sticky. I can do it on a static drop down with no problems but obviously I am missing something, can anyone help?
Below is the drop down menu:
echo '<div align="left">
<select name="dealership_id">
<option value="NULL">Choose a Dealer:</option>';
$query = 'SELECT * FROM dealership ORDER BY users_dealer_name ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0] \" <?php if (isset($_POST['dealership_id']) && $_POST['dealership_id'] == '$row[0]') {echo 'selected=\"selected\"';} ?> >$row[3]</option>";
}
// Complete the dropdown
echo '</select>
</div>
';
Below is the validation code
if (isset($_POST['dealership_id'])) {
$dealer_id = (int) $_POST['dealership_id'];
} else {
$dealer_id = 0;
}
if ($dealer_id > 0) {
$query = "SELECT dealership_id FROM dealership WHERE dealership_id=$dealer_id";
$result = mysql_query ($query); }
else {
echo '<p><font color="red">Please select your Dealership</font></p>';
}
BTW, row 0 is the primary key, row 3 is the name.
I don't think there should be single quotes around $row[0] in the following:
$_POST['dealership_id'] == '$row[0]'
By using single quotes you are literally comparing the string $row[0] instead of the variable value
Here's your code with some changes that are at least valid syntax; I didn't test to see if it works, but it should. It would be helpful for you to research string concatenation in php, some useful info here: http://www.php.net/manual/en/language.operators.string.php
echo '<div align="left">
<select name="dealership_id">
<option value="NULL">Choose a Dealer:</option>';
$query = 'SELECT * FROM dealership ORDER BY users_dealer_name ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0]\"";
if (isset($_POST['dealership_id']) && $_POST['dealership_id'] == $row[0]){
echo ' selected=\"selected\"';
}
echo ">$row[3]</option>";
}
// Complete the dropdown
echo '</select>
</div>
';

Categories