Create specific hyperlinks on search results - php

i'm doing a little web application that works like a little booking.com.
I've written the code for searching the city and I can see the results in a table, how can I make linkable all the results (the name of the hotels for example) and have the redirection to the right hotel page for each result?
I've already tried with but I had some problem to redirecting to the right page and it gave me some code errors too
<?php
$con= new mysqli("localhost","root","","registration");
$name = $_POST['search'];
//$query = "SELECT * FROM hotels
// WHERE city LIKE '%{$name}%'";
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM hotels
WHERE city LIKE '%{$name}%'");
echo "
<table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='532' height='23' id='AutoNumber1'>
<tr>
<td width='120' height='23' align='center'>Name</td>
<td width='179' height='23' align='center'>Address</td>
<td width='100' height='23' align='center'>Phone number</td>
<td width='150' height='23' align='center'>E-mail</td>
<td width='50' height='23' align='center'>Stars</td>
<td width='100' height='23' align='center'>Price single room</td>
<td width='100' height='23' align='center'>Price double room</td>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "
<tr>
<td width='120' height='23'>$row[namehotel]</td>
<td width='179' height='23'>$row[address]</td>
<td width='100' height='23'>$row[phonenumber]</td>
<td width='150' height='23'>$row[email]</td>
<td width='50' height='23'>$row[stars]</td>
<td width='100' height='23'>$row[pricesingle]</td>
<td width='100' height='23'>$row[pricedouble]</td>
</tr>";
echo "<br>";
}
mysqli_close($con);
?>
In the meanwhile I couldn't create the link, if I have as result for example Hilton, the name should be clickable and it should redirect to the page of the Hilton hotel (created by me in html)
Thank you!

Im not sure if im understand right.. you need a html hyperlink?
echo '<tr>
<td width="120" height="23">
<a href="your/target/with'.$row[hotelID].'" >'.$row[namehotel].'</a>
</td>
<td width="179" height="23">'.$row[address].'</td>
<td width="100" height="23">'.$row[phonenumber].'</td>
...
...
</tr>';

ok i was able to create the hyperlink but is it possible to create a different link for every result?
I created the hyperlink with
when I create this page I cannot be able to bring the $_POST['namehotel'], I have Undefined error
I post the code:
<?php
session_start();
$con= mysqli_connect("localhost","root","","registration");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM hotels
WHERE namehotel LIKE '%{$_POST['namehotel']}%'");
mysqli_close($con);
?>
How can I have $_POST['namehotel'] from the previous search?

Related

Images Not Showing in a table when PHP echo but will show if just in html

I am using the following code on a PHP page. When I load the page, the data shows up but the tag will not show anything other than a dot. If I add style="height:11px; width:16px", it shows a very very thin line (same goes if I just add height="11px" width="16px"
I can view the rendered html code and the img src is correct, it will display no problems. I can add, outside of my PHP script, an
as an example, and it displays just fine. So I am missing something within this snippet of code which is causing my grief?
I've looked in the Inspector tool in Chrome and I don't have any obvious errors other than a javascript error but I don't think that should be causing the issue.
The test page witho nly the two css. It works when I only have bootstrap.css but goes away when I add on the style.css
http://cronkflies.com/test5.php
<!-- Top Routes -->
<div class="col-lg-3" style="background:white;">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" colspan="2"><span style="font-size:18px; font-weight:bold; text-transform:uppercase"><?php echo $lang_top_routes ?></span></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/connections/mysqli.php";
require($path);
$sql = "SELECT vertrekluchthaven, lh.luchthavencode AS vertrek, lh.luchthavennaam AS vnaam, lh.countryflag AS vflag, aankomstluchthaven, lh2.luchthavennaam AS anaam, lh2.luchthavencode AS aankomst, lh2.countryflag AS aflag, COUNT(*) AS count
FROM tbl_vluchtgegevens vg
INNER JOIN tbl_luchthaven lh
ON vg.vertrekluchthaven = lh.luchthavenID
INNER JOIN tbl_luchthaven lh2
ON vg.aankomstluchthaven = lh2.luchthavenID
WHERE vg.vertrekdatum2 <=Now()
GROUP BY vertrekluchthaven, aankomstluchthaven
ORDER BY count DESC
LIMIT 10;";
$result = $result = $link->query($sql);
if ($result->num_rows > 0) {
echo" <table class='table'>";
echo " <tbody>";
while($row = $result->fetch_assoc()) {
$vflag = $row['vflag'];
$aflag = $row['aflag'];
echo " <tr>";
echo " <td align='center'><img src='http://cronkflies.com/img/flags/" . $vflag . "'></td>";
echo " <td ><div class='top10_luchthaven' data-tooltip=\"".htmlspecialchars($row['vnaam'])."\" >".$row['vertrek']."</div></td>";
echo " <td ><strong class='home_statistieks_label'> -</strong></td>";
echo " <td ><img src='http://cronkflies.com/img/flags/".$aflag."'></td>";
echo " <td ><div class='top10_luchthaven' data-tooltip=\"".htmlspecialchars($row['anaam'])."\" >".$row['aankomst']."</div></td>";
echo " <td ><strong class='home_statistieks_label'><div align='center'>".$row['count']."</div></strong></td>";
echo " </tr>";
}
echo " </tbody>";
echo "</table>";
}
$link->close();
?>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
It seems that you should have the same amount of columns in the header <th></th> as in the row <td></td>

Select an sql field from html field and show it in another page

I have displayed sql table in html table, made a hyperlink near all fields, when i click it the whole field details should be shows in other page(ie; i show only 2 fields of sql in the table and want to show rest in another page).
admin.php
<?php
$con= mysql_connect("localhost","root","");
mysql_select_db("main",$con);
echo"<form action=\"post\" class=\"form-horizontal\" role=\"form\">";
echo "<table width='700' height='150' onclick='myFun(event)'>";
echo" <tr>
<td width='100' align='center'></td>
<td width='100' align='center'><b><u>NAME</u></b></td>
<td width='100' align='left'><b><u>E-MAIL</u></b></td>
</tr>
";
$result=mysql_query("select NAME,EMAIL from admin order by AID");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo"<td width='100' align='center'><a href='viewadmin.php?name=".$row['NAME']."'>Select</a></td>";
echo"<td width='100' align='center'>".$row['NAME']."</td>";
echo"<td width='100' align='left'>".$row['EMAIL']."</td>";
echo"</tr>";
}
echo"</table>";
echo"</form> ";
?>
viewadmin.php
<?php
$name = $_GET['name'];
$result=mysql_query("SELECT NAME,DOB,MOB,EMAIL, FROM admin WHERE NAME = $name");
if (false === $result) {
echo mysql_error();
}
else {
$row=mysql_fetch_row($result);
}
echo" <form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>Name</td>
<td align='left'>".$row['NAME']."</td>
</tr>
<tr>
<td align='left'>E-mail</td>
<td align='left'>".$row['EMAIL']."</td>
</tr>
<tr>
<td align='left'>D.O.B</td>
<td align='left'>".$row['DOB']."</td>
</tr>
<tr>
<td align='left'>Mobile</td>
<td align='left'>".$row['MOBILE']."</td>
</tr>
<tr>
<td align='left'>Photo</td>
<td ><img src='uploads/grumpy.jpg' height='200' width='200'></td>
</tr>
</table>";
echo"</form> ";
?>
do something like this:
admin.php
$result=mysql_query("select NAME,EMAIL from admin order by AID");
while($row=mysql_fetch_array($result)) {
echo "<tr>";
echo"<td width='100' align='center'><a href='viewadmin.php?name=".$row['NAME']."'>Select</a></td>";
echo"<td width='100' align='center'>".$row['NAME']."</td>";
echo"<td width='100' align='left'>".$row['EMAIL']."</td>";
echo"</tr>";
}
echo"</table>";
and in viewadmin.php
$name = $_GET['name'];
$result=mysql_query("SELECT * FROM admin WHERE name = $name");
$row=mysql_fetch_row($result);
echo " <form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>".$row['NAME']."</td>
<td align='left'></td>
</tr>
<tr>
<td align='left'>".$row['EMAIL']."</td>
<td align='left'>...";
first rename the html page by php page, then you can pass the primary key or any key of the row from first page to admin page with the help of GET.
for eg:
first.php
<?php
$result=mysql_query("select ID,NAME,EMAIL from admin order by AID"); while($row=mysql_fetch_array($result)){
?><a hre='admin.php?id="$id=<?php $row[0] ?>"'></a>
<?php
}
?>
and in the admin.php page
you can access the value like
echo $_GET['id'];
stop using MySQL and use MySQLi, this code should work
<?php
$db_connect = mysqli_connect('localhost', 'root', 'pass', 'database');
if (mysqli_connect_errno($db_connect)) {
die('Some error occurred during connection to the database');
}
$name = mysqli_real_escape_string($db_connect,$_REQUEST['name']);
if($stmt = mysqli_prepare($db_connect, 'SELECT * FROM admin WHERE name = ?')){
mysqli_stmt_bind_param($stmt, 's', $name);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) !== 0){
$row = mysqli_fetch_assoc($result);
echo "<form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>".$row['NAME']."</td>
<td align='left'></td>
</tr>
<tr>
<td align='left'>".$row['EMAIL']."</td>
<td align='left'>..."
}
else{
echo 'not found';
}
}
else{
trigger_error('error:' . mysqli_errno($db_connect) . mysqli_error($db_connect));
}
?>

How do I hardcode a dynamic url to an image map?

I am working on a Zip Code search, and have successfully built the search. Now, I need to have the ability to click on an image map of a map, and hardcode an href to a dynamic URL
So for example, the search works well displaying contact information for a zip code in PA. But if I have an image map, I'd like the ability for it to display the same contact information if I click the image map of PA on the map image.
Here is the code I'm using to do the search, and at the bottom is the code I need help with for the image map.
<form action="search6.php" method="post">
<p><span class="orange16">Zip Code:</span>
<input type="text" name="search_name"> <input type="submit" value="Search" />
</p>
</form>
<br />
<table width="700" border="0">
<?php
if (isset($_POST['search_name'])) {
$search_name = $_POST['search_name'];
if (!empty($search_name)) {
if (strlen($search_name)>=5) {
$query = "SELECT * FROM `search4` WHERE `ZipCode` LIKE '%".mysql_real_escape_string($search_name)."%'";
$query_run = mysql_query($query);
if (mysql_num_rows($query_run)>=1) {
echo "<table width=700' border='0'>";
echo "<tr>";
echo "<td width='700' valign='top'><table width='100%' border='0'>";
echo "<tr>";
echo "<td><p><strong>Results found: </strong></p>";
while ($query_row = mysql_fetch_assoc($query_run)) {{
echo $query_row['ZipCode'].', ';
echo $query_row['ZipCity'].', ';
echo $query_row['ZipState'].'<br><br>';
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo '<span class="productdescription"><p>Office: </p></span></h2>';
echo $query_row['Office'].'<br>';
echo $query_row['Address1'].'<br>';
if(!empty($query_row['Address2'])) // This will skip if the field if it's empty
echo $query_row['Address2'].'<br>';
echo $query_row['City'].', ';
echo $query_row['State'].' ';
echo $query_row['Zip'].'<br>';
echo '<p><strong>Phone Number: </strong></p>';
echo $query_row['Phone'].'<br>';
echo '<p><strong>Fax Number: </strong></p>';
echo $query_row['Fax'].'<br><br>';
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
//BeginImage display result
$res=mysql_query("select * from Images");
{
echo "<td width='703' align='right' valign='top'>";?> <img src="<?php echo $query_row["Image"]; ?>"> <?php echo "</td>";
echo "</tr>";
}
//EndImage display result
echo ("<table width='700px' border='0' cellpadding='5' cellspacing='1'>
<tr>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Service Type:</strong></p></td>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Name:</strong></p></td>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Phone:</strong></p></td>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Email:</strong></p></td>
</tr>");
echo ("
<td align='left'><p><strong>Sales</strong></p></td>
<td align='left'><p>$query_row[SalesName]</p></td>
<td align='left'><p>$query_row[SalesPhone]</p></td>
<td align='left'><p><a href='mailto:$query_row[SalesEmail]'class='admin_links'>$query_row[SalesEmail]</p></a></td>
</tr>");
echo ("
<td align='left'><p><strong>Service</strong></p></td>
<td align='left'><p>$query_row[ServiceName]</p></td>
<td align='left'><p>$query_row[ServicePhone]</p></td>
<td align='left'><p><a href='mailto:$query_row[ServiceEmail]'class='admin_links'>$query_row[ServiceEmail]</p></a></td>
</tr>");
echo ("
<td align='left'><p><strong>Service Coordinator</strong></p></td>
<td align='left'><p>$query_row[ServiceCoorName]</p></td>
<td align='left'><p>$query_row[ServiceCoorPhone]</p></td>
<td align='left'><p><a href='mailto:$query_row[ServiceCoorEmail]'class='admin_links'>$query_row[ServiceCoorEmail]</p></a></td>
</tr>");
echo ("</table>");
}
}
}else{
echo 'No results found.';
}
}else{
echo 'Your search must be a 5-digit zip code.';
}
}
}
?>
</table>
This is the code for the Image map I need help with. I'd like to use the "CustClassID" data row for my value of lets say "23-LA".
<?php
$query = "SELECT * FROM search4 WHERE CustClassID = {$_GET['CustClassID']}";
echo ("<table width='600' border='0'>
<tr>
<td align='center'> <img src='images/greymap.png' border='0' usemap='#Map' />
<map name='Map' id='Map'>
<area shape='rect' coords='42,21,136,98' href='search6.php?CustClassID=23-LA' />
</map></td>
</tr>
</table>
");
?>
Basically, is there another way to get the same results as the search, by clicking on specific areas on the imagemap? Please point me in right direction.

PDO query() or fetch() not working

I have this code that I made for showing comments in my website from the database
but the comment is not shown.
I think maybe PDO query() or fetch() is not working but the problem is that I don't get any error.
This is the code that i use :
<?php
$showcom1 = $db->query("select * from comments where pc_active='no' order by p_id asc") or die (mysql_error());
$gpid = $_GET['p_id'];
$dcid = $_GET['dc_id'];
$ecpid = $_GET['ec_id'];
$hcpid = $_GET['hc_id'];
$scpid = $_GET['sc_id'];
echo "
<table width='100%' align='center' cellpadding='10' cellspacing='10'>
<tr>
<td class='bodymenu' style='color: #4276a1' align='center'>
الفئات <text style='color: #555'>::</text>
1 = القرآن الكريم
<text style='color: #555'>||</text>
2 = أدعية
<text style='color: #555'>||</text>
3 = قصص
<text style='color: #555'>||</text>
4 =مقالات
<text style='color: #555'>||</text>
5 = فتاوى
<text style='color: #555'>||</text>
6 = رمضانيات
<text style='color: #555'>||</text>
7 = اسلاميات
</td>
</tr>
</table>
<table width='100%' align='center' cellpadding='0' cellspacing='0'>
<tr>
<td class='tbl' colspan='7'>التعليقات</td>
</tr>
<tr align='center' >
<td class='tblbb2' >الفئة</td>
<td class='tblbb2' >اسم المعلق</td>
<td class='tblbb2' >بريد المعلق</td>
<td class='tblbb2' >اي بي المعلق</td>
<td class='tblbb2' >تاريخ التعليق</td>
<td class='tblbb2' >رابط الصفحة</td>
<td class='tblrlb' >الخيارات</td>
</tr>";
// TABLE comments = pc_id,pc_name,pc_mail,pc_ip,pc_date,pc_text,pc_active,p_id
while ($rcom1 = $showcom1->fetch(PDO::FETCH_OBJ)) {
echo "
<tr>
<td align='center' class='tblr' >".$rcom1->pc_cat."</td>
<td align='center' class='tblr' >".$rcom1->pc_name."</td>
<td align='center' class='tblr' >".$rcom1->pc_mail."</td>
<td align='center' class='tblr' >".$rcom1->pc_ip."</td>
<td align='center' class='tblr' >".$rcom1->pc_date."</td>
<td align='center' class='tblrl' ><a target='_blank' href='../pages.php?p_id=".$rcom1->p_id."'>مشاهدة</a></td>
<td align='center' class='tbll' >
<a href='index.php?cpages=comments&dc_id=".$rcom1->pc_id."'>حذف</a> -
<a href='index.php?cpages=comments&p_id=".$rcom1->pc_id."'>معاينة</a> -
<a href='index.php?cpages=comments&hc_id=".$rcom1->pc_id."'>اخفاء</a>
</td>
</tr>
";
}
echo " <tr colspan='7'>
<td colspan='7' class='tblb'>
</td>
</tr>";
?>
you want to show the comments but here in your query
$showcom1 = $db->query("select * from comments where pc_active='no' order by p_id asc") or die (mysql_error());
you have pc_active as no i guess no means the comment is hidden maybe and the other words that you set in your database means visible so .. maybe you dont have hidden comments in your database so try changing no to the other word or change pc_active in your database to be no
other thing i noticed that you use or die (mysql_error()); and that wont work with pdo you may want to change it to use the caught function :D
it works like this :
placing your query in a
try {
}
then placing the caught after the try like this :
catch(PDOException $e){
echo 'ERROR: '.$e->getMessage();
}
you should learn more about pdo error handling on here : PHP Oficcial Site For Error Handling
If you are using PDO then why are you handling error using or die(mysql_error()) at the end of this query???
$showcom1 = $db->query("select * from comments
where pc_active='no' order by p_id asc") or die
(mysql_error());
Anyway, I don't know your aim is, but error handling is done using try/catch blocks like this:
try{
$showcom1 = $db->query("select * from comments
where pc_active='no' order by p_id asc");
}catch(PDOException $e){
echo 'ERROR: '.$e->getMessage();
}
The above method, in-conjunction with your query will work/give you a human-readable error

Php Delete item from mysql data base

Hope some one can help me out here, i guess am not calling my functions right.
Am trying to retrieve some data from my database and have a delete link attached to each items being retrieved, so that when ever i click on delete, it will delete that particular item which have the delete function.
My Code to retrieve items from database are as follows.
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$count = 1;
$y = mysql_query("SELECT * FROM transaction");
if(mysql_num_rows($y) != 0){
echo "<table bgcolor=\"white\" width=\"1000\" bordercolor=\"grey\" border=\"5\" >";
echo "<tr>
<td align=\"center\">No</td>
<td align=\"center\">Date</td>
<td align=\"center\">Current Balance</td>
<td align=\"center\">Avaliable Balance</td>
<td align=\"center\">Account Status</td>
<td align=\"center\">Delete Account</td>
</tr>";
while ($z = mysql_fetch_array($y, MYSQL_BOTH)){
echo "<tr>
<td align=\"center\">".$count++."</td>
<td align=\"center\">".$z[1]."</td>
<td align=\"center\">".$z[2]."</td>
<td align=\"center\">".$z[3]."</td>
<td align=\"left\" width=\"300\">".$z[4]."</td>
<td>delete</td>
</tr>";
}
echo "</table>";
}
?>
And my code to delete
<?php
session_start();
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$id = $_GET['id'];
$sql = mysql_query("DELETE FROM transaction WHERE id='$id' LIMIT 1") or die (mysql_error());
header("Location: vacct.php");
?>
I know am missing out the logic here and hope somebody can direct me or show me the easy way out. at the moment i can successfully retrieve my items from the data base my only problem is to be able to apply the delete function each time the delete button is tapped.
You have to pass the id when you click on the delete link:
<a href=\"delete.php?id=$z[theIdKey]\">
Use the below code.I have added validation and encryption
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$count = 1;
$y = mysql_query("SELECT * FROM transaction");
if(mysql_num_rows($y) != 0){
echo "<table bgcolor=\"white\" width=\"1000\" bordercolor=\"grey\" border=\"5\" >";
echo "<tr>
<td align=\"center\">No</td>
<td align=\"center\">Date</td>
<td align=\"center\">Current Balance</td>
<td align=\"center\">Avaliable Balance</td>
<td align=\"center\">Account Status</td>
<td align=\"center\">Delete Account</td>
</tr>";
while ($z = mysql_fetch_array($y, MYSQL_BOTH)){
echo "<tr>
<td align=\"center\">".$count++."</td>
<td align=\"center\">".$z[1]."</td>
<td align=\"center\">".$z[2]."</td>
<td align=\"center\">".$z[3]."</td>
<td align=\"left\" width=\"300\">".$z[4]."</td>
<td>delete</td>
</tr>";
}
echo "</table>";
}
?>
code to delete
<?php
session_start();
$con = mysql_connect("localhost","root","");
mysql_select_db("uloaku", $con);
$id = base64_decode($_GET['id']);
if(!empty($id)){
$sql = mysql_query("DELETE FROM transaction WHERE id='$id' LIMIT 1") or die (mysql_error());
}
header("Location: vacct.php");
?>
<td>delete</td>
How are you passing the id to delete to your delete.php script?
Change:
<td>delete</td>
to:
<td>delete</td>
if $z[0] is the ID.
In your delete.php, make sure you also escape the word "transaction" using backtick:
DELETE FROM `transaction` WHERE id=123
this is because "transaction" is a reserved mysql keyword.
Please also read on SQL Injections.

Categories