Mysql search all column together with WHERE and LIKE clause - php

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

Related

Dropdown PHP with SQL with two different queries in one line inside dropdown > (Name, Number)

I want the dropdown to show the "client_code", "name" in one line. It almost works but not 100%. I am a beginner with php and SQL, can someone help me please?
Code that doesn't work
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$queryKlantCode = "SELECT klant_code FROM klant";
$resultKlant=mysqli_query($mysqli,$queryKlant);
$resultKlantCode=mysqli_query($mysqli,$queryKlantCode);
while($row=mysqli_fetch_array($resultKlant) &&
$row2=mysqli_fetch_array($resultKlantCode) )
{
?>
<option><?php echo $row[0]. ", ". $row2[0];?></option>
<?php
}
?>
</select>
</form>
Code that only works with retrieving name in dropdown from database
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$res=mysqli_query($mysqli,$queryKlant);
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row[0]; ?></option>
<?php
}
?>
</select>
</form>
You can select more than one column from a table in the same select, and as both these columns live in the same table it makes producing this result much simpler.
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$sql = "SELECT naam, klant_code FROM klant";
$result = mysqli_query($mysqli,$sql);
while($row=mysqli_fetch_array($result)){
?>
<option><?php echo $row[0]. ", ". $row[1];?></option>
<?php
}
?>
</select>
</form>
You probably want to do this with your <option> tag as well rather than put the name and code in the visible portion
<option value="<?php echo $row[1];?>"><?php echo $row[0];?> </option>
And if you use mysqli_fetch_assoc() you can use the columns names so you know what you are putting where
while($row=mysqli_fetch_assoc($result){
<option value="<?php echo $row['klant_code'];?>"><?php echo $row['naam'];?> </option>

Adding new columns to a table using php/sql

I'm trying to create a feature that allows me to add a new column to a table in my database using sql and php. I currently have a front end form, where the user can select what table they wish to alter and input boxes for adding a new column name. I also want to display the new version of the selected table (i.e. with the new column attached) My front and back end scripts look like this below.
sql10.html:
<form action="sql10.php" method="post">
<select id="category">
<option value="" disabled selected>Select your option</option>
<option id="customer" value="customer">Customer Table</option>
<option id="booking "value="booking">Booking Table</option>
<option id="bookingline" value="bookingline">Order Table</option>
<option id="package" value="package">Package Table</option>
</select>
<br><br>
Column Name: <input type="text" name="colname" placeholder="Insert New Column Name">
<br>
<input type="submit" value="Submit"> <input type="reset" value="Clear">
sql.php:
<?php
$conn = mysqli_connect("localhost", "######", "#####")
or die("Could not connect: " . mysqli_error($conn));
print "Successful Connection!";
mysqli_select_db($conn, '#####') or die ('db will not open' );
print " You have connected to the Assignment 3 database.<br>";
$category = $_POST["category"];
$newcolumn = $_POST["colname"];
$query1="ALTER TABLE $category ADD $newcolumn varchar(255)";
$query2 = "SELECT * FROM $category";
mysqli_query($conn, $query1) or die ("Invalid query");
echo "Success in database entry";
$numrows = mysqli_affected_rows($conn);
echo $numrows . "row updated<br>";
$result = mysqli_query($conn, $query) or die("Invalid query");
$num = mysqli_num_rows($result);
echo "<table border='1'><tr><th>Booking ID</th></tr>";
for($i=0; $i<$num; $i++) { //uses $num as loop end value
$row = mysqli_fetch_row($result);
echo "<tr><td>" . $row[0] . "</td></tr>";
}
echo "</table>";
mysqli_close($conn);
?>
As of using this code, I am not able to create a new column for tables. My database consists of multiple tables and I want the user to be able to select which table they wish to add a column to as well as enter the name of the new column. Can anybody see what the problem is?
I think your mistake is that your select tag doesn't have a name attribute. Remember that a form when is submitted, it takes the value of elements with the name attribute.
So, if you put in your HTML code <select id="category" name="category">, you will be able to catch its value in your sql.php query with $_POST["category"]; maybe the $_POST["category"] is arriving empty, and therefore you cannot execute the $query1 sentence.

select tag not passing value to PHP code

I have 2 PHP files.
File1: A form to select skills from an existing table (Skills table with column name: skill & skill_id)
<select name="skill1">
<?php
$self=$_SESSION['subuser_id'];
$sql = "SELECT * FROM skills";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$skill = $row["skill"];
$skill_id= $row["skill_id"];
?>
<option value='<?php $skill_id?>'><?php echo $skill;?></option>
<?php
}
?>
</select>
File2: I am recalling value through $skill1 =$_POST['skill1']; which is returning null.
I want to pass skill_id of the selected skill in code 1 to $skill1 variable on file2
Change
<option value='<?php $skill_id?>'><?php echo $skill;?></option>
to
<option value='<?php ECHO $skill_id;?>'><?php echo $skill;?></o

PHP retrieving tables from drop down list

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>

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