PHP retrieving tables from drop down list - php

Is there anyway of retrieving whole tables based on a drop down list? Like when i select one and press submit it will bring that particular table? 'gameResults' is the database which features tables. My code is as follows:
if(isset($_POST["submit"])) {
$tablesnames = $_POSt["tablenames"]; // name of selection list
if($_POST["tablenames"] == '1') { // if option 1 is selected
// display table
} }
<?php
$conn = mysql_connect("localhost", "xxxxxx", "xxxxxx");
mysql_select_db("gameResults", $conn)
or die ('Database not found ' . mysql_error() );
"SELECT TABLE FROM information_schema.tables WHERE table_schema = 'gameResults'";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
?>
<html>
<body>
<form name ="tables" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>Choose form to display :
<select name="tablesnames" id="tablesnames">
<option value="nothing"> </option>
<option value="fixtures"> Fixtures </option>
<option value="results"> Results </option>
<option value="teams"> Teams </option>
<option value="seasons"> Seasons </option>
<option value="administrators"> Administrators </option>
<option value="users"> Users </option>
</select></p>
<input type="submit" name="submit" > <input type="reset"
<html>
</body>

First off, there is no column TABLE. What you are looking for is TABLE_NAME. Consider this example: (And please stop using mysql_ functions, use PDO or mysqli_ instead)
<?php
$tables = array();
// host, db user, db password, datababse
$con = mysqli_connect("localhost","test","test","test");
$query = mysqli_query($con, "SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = 'test'");
$tables = mysqli_fetch_all($query, MYSQLI_ASSOC);
?>
<!-- loop thru the values -->
<select name="whatever">
<?php foreach($tables as $value): ?>
<option value="<?php echo $value['TABLE_NAME']; ?>"><?php echo $value['TABLE_NAME']; ?></option>
<?php endforeach; ?>
</select>

Related

Dropdown menu failed to Load Data from Posttgres

At this time the dropdown menu unable to call data from database Postgres.
<div class="form-group">
<?php
<label>Payee</label>
<form action="pg-t-payment-update.php" method="post">
<select class="form-control select2" style="width: 100%;">
<?php
$db = pg_connect("host=10.0.32.x port=5432 dbname=postgres user=postgres password=123");
$sql ="select distinct tenant_name FROM payment_ref_tenancy order by tenant_name asc";
$result = pg_query($db, $sql);
$rows = pg_num_rows($result);
while ($row = pg_fetch_assoc($result)) {
echo '<option value="'.htmlspecialchars($row['tenant_name']).'"></option>';
}
pg_close($db);
?>
</select>
</div>
I think the issue is here:
echo '<option value="'.htmlspecialchars($row['tenant_name']).'"></option>';
change it to:
echo '<option value="'.htmlspecialchars($row['tenant_name']).'">'.htmlspecialchars($row['tenant_name']).'</option>';
The correct syntax of option is:
<option value="1">One</option>
here value is the text we get when use it in code, where One is the text which is shown to user.

I am accessing MySQL table in PHP but it is not executing if condition and directly display the table

The code below is accessing the database table directly, but I want it to display the table content on giving conditions in drop down menu like when I select islamabad in one drop down menu and lahore in other as given in code and press search button, then it display the table flights, but it is displaying it directly
<p class="h2">Quick Search</p>
<div class="sb2_opts">
<p>
</p>
<form method="post" action="haseeb.php">
<p>Enter your source and destination.</p>
<p>
From:</p>
<select name="from">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<p>
To:</p>
<select name="To">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<input type="submit" value="search" />
</form>
</form> </table>
<?php
$from = isset($_POST['from'])?$_POST['from']:'';
$to = isset($_POST['to'])?$_POST['to']:'';
if( $from =='Islamabad'){
if($to == 'Lahore'){
$db_host = 'localhost';
$db_user = 'root';
$database = 'homedb';
$table = 'flights';
if (!mysql_connect($db_host, $db_user))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
}
}
mysqli_close($con);
?>
Fix the mysql_ and mysqli_.
Do not repeat the SQL Query to test $result. One is ok.
Fix the case of index - to or To.
Table displayed directly: Hope you are not refreshing the form with the pre-submitted values of your cities. Freshly open the page link, and confirm that $from, $to are really empty. If you are pressing F5 in browser, it may be wrong.

Mysql search all column together with WHERE and LIKE clause

I have a database where in the table there is four column id,title,address,phone.When searched with a selected table the query search the term in that particular row and result is fetched,but what I want to know is if no table is selected that is i.e-
<option value="">Select table</option>
I want the query to search the term in every table,I could do it with WHERE row1 LIKE %$keyword% OR row2 LIKE %$keyword%,but I want it to search this thing in one query as if it select all row data and search it with the term,like first it search the row 1 then row2 the row3 one after another on its own.Is there any way to do it
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$keyword= $_POST['keyword'];
$table= $_POST['table'];
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
$data = mysql_query("SELECT * FROM information WHERE $table LIKE '%".$keyword."%'");
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['id'];
echo $result['title'];
}
}
?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='POST'>
<input type='text' size='90' name='keyword'>
<select name='table'>
<option value="">Select table</option>
<option value="title">title</option>
<option value="address">address</option>
<option value="phone">phone</option>
</select>
<input type='submit' name='submit' value='submit' >
</form>
</body>
</html>
It looks like what you have should work, but you also need to concatenate the where clause. Try this one.
$data = mysql_query("SELECT * FROM information WHERE `{$table}` LIKE '%".$keyword."%'");

php mysql search using boxlist

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."'";

PHP- Fetch from database and store in drop down menu html

I can't seem to get the following code to make a dropdown menu that contains data from a mysql database. The "include('connect.php');" connects to the mysql database and I know it works on separate pages. Any suggestions?
Below is the entire code.
listCustomer
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($r = mysql_fetch_array($result))
{
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
}
echo "</select>";
?>
<BR>
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
From the looks of things, you're missing an opening option tag, so it's just outputting "Dropdown" as a line of text.
Edit
Just to be completely transparent, because I did not have connect.php, I had to add my own DB connections. My whole page looked thusly:
<?
//Adding to display errors.
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR />
<?php
// BEGIN ADDED CONNECTION HACKY GARBAGE
$con=mysql_connect("localhost","root","root");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selected = mysql_select_db("sample",$con)
or die("Could not select examples");
// END ADDED CONNECTION HACKY GARBAGE
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
<BR />
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
First off, you are missing an option opening tag, as correctly mentioned by stslavik. But this is not causing the issue here as it seems (it's auto-corrected by the browser - in my tests atleast).
Secondly, this wont work (problem causer):
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
You should use
echo "<option value=".$r["Cnum"].">".$r["CName"]."</option>";
or, as I always prefer single quotes to enclose echo or print output strings:
echo '<option value='.$r['Cnum'].'>'.$r['CName'].'</option>';
Third alternative (complex syntax: What does ${ } mean in PHP syntax?)
echo "<option value={$r["Cnum"]}>{$r["CName"]}</option>";
assuming you get data from the database try this
echo "<option value={$r['Cnum']}>{$r['CName']}</option>";
try,
echo "<option value=' . $r['Cnum'] . '>' . $r['CName'] . '</option>";
instead of
echo "<option value=$r[Cnum]>$r[CName]</option>";

Categories