php mysql search using boxlist - php

I am looking for a way to search for data from the database using input type box list, I tried make the code but it doesn't display anything:
html code:
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="1">user1</option>
<option value="2">user2</option>
<option value="3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
php code:
if (isset($_POST['users'])) {
$key = trim ($_POST['users']);
$s = "SELECT * FROM users where user_name LIKE '%$key %'";
$res = mysql_query($s) or die('query did not work');
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
when I try the code I didn't get any result and when I remove the while loop and put this instead of it :
<?php echo $key; ?>
it gives me the numbers of the selected value, for example if I select user2 the result will be 2. and I want the result to be user id and user name.

you need to fetch all the user name in your drop down select box
<select name="users">
<option selected="selected" value="">-- select --</option>
<?php $s2 = "SELECT * FROM users";
$q2=mysql_query($s2) or die($s2);
while($rw=mysql_fetch_array($q2))
{
echo '<option value="'.$rw['userid'].'">'.$rw['username'].'</option>';
}</select>
?>
<?php if (isset($_POST['search'])) { // submit button name here
$key = $_POST['users'];
$s = "SELECT * FROM users where user_id='".$key."'";
$res = mysql_query($s) or die($s);
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>

edit your html to this,you will get the in $_POST which will be in value='something'
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="user1">user1</option>
<option value="user2">user2</option>
<option value="user3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
Or if value is the id of user then change query to this
$s = "SELECT * FROM users where user_id='".$key."'";

Related

how to concatenate two columns from mysql in PHP

I'm trying to get all the names from MySQL into a dropdown list using PHP.
I connected to MySQL using PDO. currently I can get only the first name, But I want the names to be first name + last name in the drop down list but I couldn't concatenate them.
I tried to concatenate them like this:
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["firstname"+"lastname"]; ?></option>
<?php } ?>
</select>
but that didn't work out for me.
$query="select * from user_details";
$exec = $conn->prepare($query);
$exec->execute();
$rc = $exec->rowCount();
$result=$exec->fetchAll();
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["firstname"]; ?></option>
<?php } ?>
</select>
it worked only with one column which is the firstname but I want it to be both firstname + lastname
Update this view code:
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["firstname"] .' '.$output["lastname"]; ?></option>
<?php } ?>
</select>
With Mysql you can try this concat()
$query="select concat(firstname,' ',lastname) as fullname from user_details";
$exec = $conn->prepare($query);
$exec->execute();
$rc = $exec->rowCount();
$result=$exec->fetchAll();
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["fullname"]; ?></option>
<?php } ?>
</select>

How to echo the selected value from an array fetched from the database

I want to echo the selected value from the database to update it then store it
for example I have an asset with category printers from table category which contains other categories and when I want to edit this asset on the edit page I should get a dropdown list contains all the categories and selected on printers then if I want to change it I will if not leave unchanged
The array is drop-down from table category inner joined with user_asset table in the database by asset_category as a foreign key
this is what I have done so far
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required>
<?php while( $row = mysqli_fetch_array($result)) {?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['cate_name'];?>
</option>
<?php }?>
</select>
</div>
You can add if check if ($row['cate_name'] == 'computer') { ?> and then add selected to this option:
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required >
<?php while( $row = mysqli_fetch_array($result)) {
if ($row['cate_name'] == 'computer') { ?>
<option value="<?php echo $row['category_id'];?>" selected><?php echo $row['cate_name'];?></option>
<?php } else { ?>
<option value="<?php echo $row['category_id'];?>"><?php echo $row['cate_name'];?></option>
<?php }
}?>
</select>
Notice: If you have multiple elements with that category it will select the last one.
the answer is very simple.. let's put this code
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required>
<?php while( $row = mysqli_fetch_array($result)) {
if($row['cate_name']== printers) { ?>
<option value="<?php echo $row['category_id'];?>" selected="selected">
<?php echo $row['cate_name'];?> </option>
<?php } else { ?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['cate_name'];?> </option>
<?php }?>
</select>
</div>
The logic is that using while loop, checking the condition using if class, and when it satisfies make it as selected. Then it will be echo as selected Value.

Text box show value from Ajax

Using PHP, Ajax and Jquery, I'm able to create a cascading dependent drop down menu. But I want to have the third drop down to be a text box instead.
How can I do this?
Ajax Code
PHP code...
<?php
include('conn.php');
$query = $con->query("SELECT * FROM job_category group by category_name ");
$rowCount = $query->num_rows;
?>
Category:
<select name="category" id="category">
<option value="">Select Category</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['category_num'].'">'.$row['category_name'].'</option>';
}
}else{
echo '<option value="">Country not available</option>';
}
?>
</select>
<br>
Sub-Category:
<select name="sub_type" id="sub_type">
</select>
<br>
Priority:
<select name="priority" id="priority_level" >
</select>
?>
Please help, I'm having a hard time on this.

Setting selected option in drop-down box

My SELECT looks like the following:
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>">
<?php echo $line['Descriptor']; ?>
</option>
<?php
}
mysql_close();
?>
</select>
Querying the DB and setting up the drop-down works. The problem is that the value listed first isn't automatically selected. If a user wants to use it, for further navigation, they must first select a different one and then select the first once again.
I couldn't alter the values in the DB. If I insert selected='selected' it returns the last value of the result set, but always without being selected.
You maybe want this? First selected option when the form is loaded is blank.
<select name="country" onchange="getState(this.value)">
<option value=""></option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
or select the selected data from the database? selected column with selected value.
<select name="country" onchange="getState(this.value)">
<?php
$first = true;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php echo ($line['selected']=='selected') ? 'selected="selected"' : '' ; ?>>
you can test with respect to $line['Sbj_ID'] if this is = to the value you want by default
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php if($line['Sbj_ID']==value_you_want_selected){?>selected<?php } ?>>
<?php echo $line['Descriptor']; ?>
</option>
<?php
$i++; }
mysql_close();
?>

How to populate a particular column list in Combo Box with PHP?

I am using the given code below to populate some values from a column of a table. It's just getting filled blank..
Can you please find the problem with it ?
<select name="category">
<option value="" selected>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("muskilaasaan");
$category = "SELECT cat FROM category";
$query_result = mysql_query($category);
while($result = mysql_fetch_array($query_result))
{
?>
<option value = "<?php echo $result['cat']?>"/>
<?php
}
?>
</select>
<select name="category">
<option value="" selected>Select a category</option>
<?php
mysql_connect("localhost","root","");
mysql_select_db("muskilaasaan");
$category = "SELECT cat FROM category";
$query_result = mysql_query($category);
while($result = mysql_fetch_assoc($query_result))
{
?>
<option value = "<?php echo $result['cat']?>"><?php echo $result['cat']?></option>
<?php
}
?>
</select>
Changed to mysql_fetch_assoc and also you didn't put anything in the option tags, that would cause it to appear blank.
it does not help if you specify the connection string as a second argument in mysql_select_db() ? see here for an example: http://php.net/manual/de/function.mysql-select-db.php

Categories