How to display postgresql search results using PHP - php

The Problem
Hello, I am creating a search function that will allow users to search for a specific E-Number and see whether it is derived from animals or not, basically seeing if it's vegan. I have successflly connected to the database using PHP on my website.
The Code
At the top of the page:
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=***** port=*****
dbname=**** user=**** password=*****")
or die('Could not connect: ' . pg_last_error());
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
// Performing SQL query
$query = 'SELECT vegan FROM enumbers WHERE code = searchq';
}
?>
The search bar:
<div id="tablebox">
<!-- Search bar -->
<p>Is It Vegan?</p>
<form name="form1" method="post" action="searchEnumbers.php">
<input name="search" type="text" size="30" maxlength="5" />
<input name="submit" type="submit" value="Search" />
</form>
</div>
How will I now display the 'vegan' result that has been searched? I am unsure how to print the results.
Update
The column names in the enumbers table are: 'code', 'name', 'type', and 'vegan'.
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=db.dcs.aber.ac.uk port=5432
dbname=cs399030_16_17 user=sec17 password=Liverpool2112")
or die('Could not connect: ' . pg_last_error());
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
// Performing SQL query
$pg_query = 'SELECT vegan FROM enumbers WHERE code = '.$searchq;
$result = pg_query($query);
foreach($result as $r){ //If you have multiple records or $result
echo "<p> Your ".$r->params." or ".$r['params']." here </p>"; //for instance
}
}
?>

You should have something like:
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=**** port=****
dbname=**** user=**** password=****")
or die('Could not connect: ' . pg_last_error());
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
// Performing SQL query
$query = 'SELECT vegan FROM enumbers WHERE code = '.$searchq;
// make the query with: pg_query
// $result = pg_query($query);
foreach($result as $r){ //If you have multiple records or $result
echo "<p> Your ".$r->params." or ".$r['params']." here </p>"; //for instance
}
//you could use: var_dump(pg_fetch_all($result));
}
?>
<!-- If you want to show always the form. If not, put inside the else -->
<div id="tablebox">
<!-- Search bar -->
<p>Is It Vegan?</p>
<form name="form1" method="post" action="searchEnumbers.php">
<input name="search" type="text" size="30" maxlength="5" />
<input name="submit" type="submit" value="Search" />
</form>
</div>
Note this is not the best way to do it (you could use AJAX, and differents pages in order to now refresh the page), or others ways. But this could work as the way you want.
Check official documentation from PHP: http://php.net/manual/kr/function.pg-connect.php
Hope it helps!

Related

I want to make search page where I want to display my searched data from database in a "div"?

I want to make search page(php) where I want to display my searched data from database in a "div"?I made a connection with database and searched for data in one phppage and created a div tag in another phppage.how can i display the searched data of one php page to be displayed in another php page's "div"
Search.php:
<?php
include 'Searchdata.php';
include 'connect.php';
if(isset($_POST['submit'])){
$searchkey= $_POST['search'];
$searchkey=preg_replace("#[^0-9a-z]#i", "", $searchkey);
$query = mysqli_query($conn, "SELECT * FROM newentry WHERE Date LIKE '%$searchkey%'")or die("Could not search!");
$count = mysqli_num_rows($query);
if(!($count == 0)) {
while($row=mysqli_fetch_array($query)){
$Date=$row['Date'];
$Entry=$row['Entry'];
echo'<div>'.$Date.'<br>'.$Entry.'</div>';
}
} else {echo "There was no search result!";}
}?>
Searchdata.php:
<div>
<form action="Search.php" method="post">
<input type="text" name="search" placeholder="Search">
<input type="submit" value="Search" />
</form>
Just create a variable to store the results in this case its $data. Move your include searchdata.php to the bottom of the code so it can reconize $data.
Then echo it on your html page.
html page
<div>
<form action="Searchdata.php" method="post">
<input type="text" name="search" placeholder="Search">
<input type="submit" value="Search">
</form>
<div><?php echo $data ?></div>
</div>
The PHP code.
<?php
include 'connect.php';
$data = '';
if(isset($_POST['submit'])){
$searchkey= $_POST['search'];
$searchkey=preg_replace("#[^0-9a-z]#i", "", $searchkey);
$query = mysqli_query($conn, "SELECT * FROM newentry WHERE Date LIKE '%$searchkey%'")or die("Could not search!");
$count = mysqli_num_rows($query);
if(!($count == 0)) {
while($row=mysqli_fetch_array($query)){
$Date=$row['Date'];
$Entry=$row['Entry'];
$data = '<div>'.$Date.'<br>'.$Entry.'</div>';
}
} else {
$data = "There was no search result!";}}
include 'Search.php';
?>

How to take more than one input from users in HTML form and search in MySql database?

I am trying to make a web form that take multiple ids and search that ids in MySql database and then return that row in table.
PHP script
<form method="post" name="display" action="multi.php" />
Enter the name you like to display the data from MySQL:<br>
<input type="text" name="name" multiple />
<input type="submit"/>
</form>
</html>
<?php
mysql_connect("localhost", "root", "root123") or die("Connection Failed");
mysql_select_db("myDB")or die("Connection Failed");
$name = array();
foreach ($_POST as $name)
{
$query = "SELECT * FROM check_12 where name = '$name'";
}
$result = mysql_query($query);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td>'.$line['ID'].'</td>
<td>'.$line['Name'].'</td>
<td>'.$line['Email Id'].'</td>
<td>'.$line['Address'].'</td>
<td>'.$line['Phone number'].'</td>
<td>'.$line['Remark'].'</td>
</tr>';

Trying to Link a populated drop down list PHP

As the title suggests I am trying to link a populated drop down list to a form on another page.
My dropdown list is currently connected to my database which displays the addressID's of 6 people. So when the user selects for example AddressID 3 it will take them to the next page (customerdetails.php) which will then allow them to update the form which will update the database accordingly.
My current code is as follows
<?php
//adding the database connection
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the databse
$dbhandle = mysql_connect ($hostname, $username, $password)
or die ("Unable to Connect to MySQL");
echo "Connected to MySQL";
//selecting the database we want to work with
$selected = mysql_select_db("my_guitar_shop2", $dbhandle)
or die("Could not select my_guitar_shop2");
?>
<p>AddressID:</p> <br>
<?php
$sql = "SELECT addressID FROM addresses";
$result = mysql_query($sql);
echo "<select name='addressID' onchange = 'getAddressID(this)'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['addressID'] ."'>" . $row['addressID'] ."</option>";
}
echo "</select>";
?>
Now on the customerdetails.php page i have the code:
<?php
$adrresIDSelected = $_GET['addressID'];
?>
For the life of me I cannot seem to connect the 2 pages together.
Am i anywhere near the correct path? I would prefer not to use javascript as I have no prior knowledge of it.
Many thanks in advance
UPDATE
customerdetails.php page
<?php
//adding the database connection
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the databse
$dbhandle = mysql_connect ($hostname, $username, $password)
or die ("Unable to Connect to MySQL");
echo "Connected to MySQL";
//selecting the database we want to work with
$selected = mysql_select_db("my_guitar_shop2", $dbhandle)
or die("Could not select my_guitar_shop2");
?>
<?php
$addrresIDSelected = $_GET['addressID'];
?>
Contact Form
<form class="form">
<p class="first">
<label for="name">FirstLine</label>
<input type="text" name="firstline" id="first" />
</p>
<p class="second">
<label for="email">SecondLine</label>
<input type="text" name="secondline" id="second" />
</p>
<p class="city">
<label for="web">City</label>
<input type="text" name="city" id="web" />
</p>
<p class="state">
<label for="web">State</label>
<input type="text" name="state" id="web" />
</p>
<p class="zip">
<label for="web">Zip Code</label>
<input type="number" name="zip" id="web" />
</p>
<p class="update">
<input type="button" value="Update" />
</p>
<p class="remove">
<input type="button" value="Remove" />
</p>
</form>
First solution (no Javascript)
For a solution without Javascript, you will need to use the select within a form element and use a submit button too to send the information completed/selected in the form to the desired page:
...
<form action="customerdetails.php" method="get">
<select name="addressID">
<?php
$sql = "SELECT addressID FROM addresses";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['addressID'] ."'>" . $row['addressID'] ."</option>";
}
?>
</select>
<input type="submit" value="Take me to the other page">
</form>
...
UPDATE - Second solution (with Javascript)
For using the getAddressID Javascript function to send the ID instead of using a form, you will need to update the function a bit:
<script>
function getAddressID (option) {
var addressID = option.value;
// you do not need the <your_domain> prefix here, as probably both your php scripts are on the same server/domain and same folder
window.location.replace ("customerdetails.php?addressID =" + addressID);
//-----------------------------------------------------^
// Extra space must be removed!
}
</script>

how to do search in the same page?

1) this my search form
search.html :
<form action="process.php" method="POST">
<input type="text" name="query" />
<input type="hidden" name="searching">
<input type="submit" value="Search" />
</form>
2) All of my search process is handled and will be shown on process.php.
process.php :
<?php
$connection = mysql_connect("*****","*****","*****");
if (!connection) {
die ("Please reload page. Database connection failed: " . mysql_error());
}
// Select a databse to use
$db_select = mysql_select_db("*****",$connection);
if (!$db_select) {
die("Please reload page. Database selection failed: " . mysql_error());
}
if (isset($_POST["searching"])) {
/*call search process*/
dosearch();
}
function doSearch(){
$keyword = $_POST("query");
$search = "SELECT * FROM tbl_name WHERE name LIKE '%$keyword%'";
$result = mysql_query($search) or die('query did not work');
While($result_arr = mysql_fetch_array( $result ))
{
echo $result_arr['name'];
echo " ";
echo "<br>";
echo "<br>";
}
}
?>
3)How to show the search result at page "search.html" and how to combine it into one same page?
You will have to create search.php instead of search.html.
you may try using php sessions. they are very helpful.
you can save whatever variable you want to transfer from "process.php" to "search.php" in the $_SESSION array and then use them in search.php. for example :
in process.php
<?php session_start();
$_SESSION['a']=$result_arr['name'];
?>
then in search.php
<?php session_start();
echo $_SESSION['a'];
?>

populate a select box with php mysql

I'm having difficulty populating a select box within a form to display existing "forenames" of nurses from the "Nurses" table. Could anyone tell me what Im doing wrong? Thanks in advance!
Here is the form
<form method="post" action="insert.php">
<br>
<tr><td align="left"><strong>Nurse Information</strong></td>
</td>
<tr>
<td>nurse_name</td>
<td><select name="valuelist">
<option value="valuelist" name="nurse_name" value='<?php echo $nurse_name; ?>'></option>
</select></td>
<tr>
The QUERY which should populate the nurse_forename:
<html><head><title>Connect to Database</title></head><body>
<font size="4">Query gets Forename of nurse</font>
<br><br><font size="4">Choose a name</font><br><br>
<form action="insert.php" method="post">
<select name="valuelist">;
<?php
$value=$_POST ["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');
$fetch_nurse_name = mysql_query("SELECT DISTINCT $nurse_name FROM nurse");
$result = mysqli_query($con, $query) or die("Invalid query");
while($throw_nurse_name = mysqli_fetch_array($fetch_nurse_name)) {
echo '<option value=\"'.$nurse_name['nurse_name'].'">'.$throw_nurse_name['nurse_name'].'</option>';
}
echo "</select>";
mysqli_close($con);
?>
<input type="submit" value="Submit">
</form></body></html>
Try this:
<html><head><title>Connect to Database</title></head><body>
<font size="4">Query gets Forename of nurse</font>
<br><br><font size="4">Choose a name</font><br><br>
<form action="insert.php" method="post">
<select name="valuelist">;
<?php
$value=$_POST ["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');
$fetch_nurse_name = mysql_query("SELECT DISTINCT Forename FROM nurse");
while($throw_nurse_name = mysql_fetch_array($fetch_nurse_name)) {
echo '<option value=\"'.$throw_nurse_name[0].'">'.$throw_nurse_name[0].'</option>';
}
echo "</select>";
?>
<input type="submit" value="Submit">
</form></body></html>
Dont use mysql and mysqli together....you should use mysqli or PDO, but not a mix of both ;)
PS: Edited ;)
Saludos.
Apologies if this duplicates other answers, Here's an answer using mysql_ syntax although you should of course be using mysqli_ or PDO for this...
<form action="insert.php" method="post">
<select name="valuelist">;
<?php
//path to connection statements
include('path/to/connection/stateme.nts');
//fetch nurse name
$query = "SELECT nurse_name FROM nurse;";
$result = mysql_query($query) or die(mysql_error()); //note: use mysql_error() for development only
//print results
while($row = mysql_fetch_assoc($result)) {
echo '<option value=\"'.$row['nurse_name'].'">'.$row['nurse_name'].'</option>';
}
echo "</select>";
?>
<input type="submit" value="Submit">
</form>
Check your MySQL table and column name that you using. Sometimes it does not work if you don't write those names exactly which in your MySQL table. suppose,
$query = "SELECT nurse_name FROM nurse";
in above SQL if MySQL table name is 'NURSE' and column name is 'NURSE_NAME' then write exactly like this.
$query = "SELECT NURSE_NAME FROM NURSE";
So, you look that sometime MySQL table, column name work in case sensitive.

Categories