Search in Select List - php

Currently creating this project and having problems on how to create a search in select list. I would like to search from option values. how can i create this kind of search on select list?
<?php
error_reporting(0);
//connect to database
$conn = mysqli_connect("localhost", "root", "", "waterdis_stbwd");
if(mysqli_connect_errno($conn)) {
echo "Unable to connect to database server";
}
//query database for items to populate
$sql = "SELECT id FROM usrs";
$query = mysqli_query($conn, $sql);
echo '<select id="slct">';
echo '<option value="">Select ID</option>';
while($id = mysqli_fetch_assoc($query)){
echo "<option>{$id['id']}</option>";
}
echo '</select>';
?>

You will want to use a plugin, like select2
https://select2.github.io/examples.html
https://select2.github.io/

<?php
error_reporting(0);
//connect to database
$conn = mysqli_connect("localhost", "root", "", "waterdis_stbwd");
if(mysqli_connect_errno($conn)) {
echo "Unable to connect to database server";
}
//query database for items to populate
$sql = "SELECT id FROM usrs";
$query = mysqli_query($conn, $sql);
echo '<select id="slct">';
echo '<option value="">Select ID</option>';
while($id = mysqli_fetch_assoc($query)){
echo "<option value="{$id['id']}">{$id['id']}</option>";
}
echo '</select>';
?>

Related

<select> accessing values with $_POST

I am little confused about this because everywhere I look for it, they have it same as me, but I have problem that I am listing various of options by <select> and <option> and I need to get back the value of what I have clicked, by it doesn't seem to be working, so if there is someone who knows?
$connect = mysqli_connect("localhost", "root", "", "uklidy");
$sql = "SELECT * FROM budovy ORDER BY id ASC";
$result = mysqli_query($connect, $sql);
echo '<select name="budova_name">
';
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row["id"].'">'.$row["jmeno"].'</option>';
}
echo '</select>';
$budova = $_POST["budova_name"];
echo $budova;
You need to submit it with a form.
<?php
$connect = mysqli_connect("localhost", "root", "", "uklidy");
$sql = "SELECT * FROM budovy ORDER BY id ASC";
$result = mysqli_query($connect, $sql);
echo '<form method="post" action="mypage.php">';
echo '<select name="budova_name">';
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row["id"].'">'.$row["jmeno"].'</option>';
}
echo '</select>';
echo '<input type="submit">';
echo '</form>';
and if you're talking about getting the selected value on the same page you're gonna have to use javascript for that. PHP is a server side language.

How to retrieve all table names from database?

I'm trying to retrieve all table names from a MySQL database and link them to a dropdown list. Then I want to store those table names in a table and preview them.
here's my code...
<?php
$sql = "SHOW TABLES FROM $ip";
$result = mysql_query($sql);
?>
<form id="form1" name="form1" method="post" action="newloay.php">
<select name="select" id="select" required>
<?php
while($row = mysql_fetch_array($result)){
?>
<option> <?php print $row[0] ?> </option>
<?php
} ?>
}
</select>
MySQL :
SELECT
table_name
FROM
my_schema.tables
Where my_schema is your schema name , $ip, I guess.
Follow the following code to get the Name of the Tables in your Database :
<?php
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
mysql_free_result($result);
?>
Hope this helps, for more details on this, please check out THIS PAGE

How to display database value in form list

I've created drop down list with value name from the database. When I select the value from the drop down list, other data will appear in other textfield based on the database. The submit process was doing fine except when I check on the list. The drop down list value didn't appear in the list but other data did.
This is my adding form:
<tr><td width="116">Medicine name</td><td width="221">
<center>:
<select name="name" id="name" >
<option>--- Choose Medicine ---</option>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("arie");
$sql = mysql_query("SELECT * FROM tabelmedicine ORDER BY name ASC ");
if(mysql_num_rows($sql) != 0){
while($row = mysql_fetch_assoc($sql)){
$option_value = $row['priceperunit'] . ',' . $row['stock'];
echo '<option value="'.$option_value.'">'.$row['name'].'</option>';
}
}
?>
</select ></center>
This is a script to display other database value in other textfield when the drop down list is selected:
<script>
var select = document.getElementById('name');
var priceperunit = document.getElementById('priceperunit');
var stock = document.getElementById('stock');
select.onchange = function()
{
var priceperunit_stock = select.value.split(',');
priceperunit.value = priceperunit_stock[0];
stock.value = priceperunit_stock[1];
}
</script>
This is my inserted data into database process:
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "arie";
$connect = mysql_connect($host, $user, $pass) or die ('Failed to connect! ');
mysql_select_db($db);
$name=$_POST['name'];
if ($name === "")
{
echo "Please fill all the data";
}
else
{
$query="INSERT INTO `tabelout`(`name`)
VALUES ('$name');";
$result = mysql_query($query) OR die (mysql_error());
echo "You have successfully added new medicine to the database.";
}
?>
This is my list page, where the name didn't show up:
<?php
$con=mysqli_connect("localhost","root","","arie");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tabelout");
echo "<table border='1'>
<th>name</th>";
while($row = mysqli_fetch_array($result))
{
echo "<td><center>" . $row['name'] . "</center></td>";
}
echo "</table>";
mysqli_close($con);
?>
Make sure your database table has records, If it has records, then change the table structure, Add tr tags where required.
echo "<table border='1'>
<tr><th>name</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td><center>" . $row['name'] . "</center></td></tr>";
}
echo "</table>";

Unable to populate list from databae

I am trying to retreive data from a database and populate a list from it, there doesnt seem to be errors in my code and still the list is not populating, my code
<?php
$hostname = "localhost";
$username = "username";
$password = "password";
$dbase = "db";
$link = # mysql_connect($hostname, $username, $password);
$db_selected = # mysql_select_db($dbase);
?>
<?php
include("scripts/dbconnect.php");
$query="select class from school";
$result=mysql_query($query);
$numrows=mysql_num_rows($result);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<option value="'.$row['class'].'">'.$row['class'].'</option>';
}
?>
any help much appreciated, thanks
Options must be wrapped with select tag:
echo "<select name='class'>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<option value="'.$row['class'].'">'.$row['class'].'</option>';
}
echo "</select>";
Additionally do:
if (!$link) {
die('Could not connect: ' . mysql_error());
break;
}
To see if there are any errors of mysql connection.
You need to echo a select element.
<?php
include("scripts/dbconnect.php");
$query="select class from school";
$result=mysql_query($query);
$numrows=mysql_num_rows($result);
echo "<select>"
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<option value="'.$row['class'].'">'.$row['class'].'</option>';
}
echo "</select>"
?>

how to write text in textbox from database in php

I have a form where I am getting values from previous page in $GET. I want to fetch around 3 different values from database and display them in textbox. How will I do it? Following is my code for getting data from database.
<?php
require_once('config.php');
$id = $_GET['id'];
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$query = "select question,price,sequence from questions where status = 1 and qid =".$id;
//echo $query;
$result=mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$num_rows = mysql_num_rows($result);
if($num_rows==0) {
echo '<center><font color="red"><b>No record found!!</b></font></center>';
}
else {
$row = mysql_fetch_array($result);
echo $row['question'];
echo $row['sequence'];
echo $row['price'];
}
?>
Thanks
Pankaj
<textarea><?php echo htmlentites($row['question']);?></textarea>
or
<input type="text" value="<?php echo htmlentites($row['question']);?>" />
Depending on your fancy.

Categories