i am new to php
How do i make my search result,ie title is show such that is hyper link to a page?
i have already iput a url column in the table in phpmyadmin
the php code is shown below
<?php
$connect = mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("books",$connect) or die(mysql_error());
$sql = "SELECT * FROM bookinfo";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['searchbox']);
$sql .= " WHERE title = '{$search_term}'";
$sql .= " OR author = '{$search_term}'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<form name="searchform" method="POST" >
Search:<input type="text" name="searchbox" />
<input type="submit" name="search" value="search book" />
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<td>Title</td>
<td>Author</td>
<td>Price</td>
</tr>
<?php while ($row = mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['author']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php }
?>
</table>
Hyperlinks are made with the a (or "anchor") tag in HTML. Simply include one in your output:
<td><?php echo $row['title']; ?></td>
Related
I'm trying to search by name and display the results on the same page.
My php code and the html code are in 2 separate files. Given below is my search.php code.
<?php
include_once '../connection.php';
include_once '../session.php';
$output = '';
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
if($count == 0) {
$output = 'There was no search result!';
}else{
while($row = mysqli_fetch_array($result)) {
$ID = $row['id'];
$Name= $row['customername'];
$Type = $row['type'];
$Phone = $row['phoneno'];
$Email = $row['email'];
$output .= '<div>' .$Name.' '.$Type.' '.$Phone.' '.$Email.'<div>';
}
}
}
?>
<?php print("$output");?>
Given below is the html code for my form.
<form action="search.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
When I search, the results are displayed on a completely new page. But I want the results to be displayed on the same page with all the existing page layout, headers and footers.
How can I achieve this?
Thank in Advance!
Assuming your first file name is index.php.
Then need to call this file on form action & little bit refactor a file. Now it have $output array which is empty. If it contains search parameter, it will fill data to $output array and then on html part it checks if $output array has data to display it.
<?php
include_once '../connection.php';
include_once '../session.php';
$output = array();
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) {
$output[] = $row;
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<?php if(count($output) > 0): ?>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php foreach($output as $row): ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</form>
</body>
</html>
I have 17 records inserted in my table "book".
<?php
include 'config.php';
session_start();
$sql = "SELECT * from book";
$result = mysqli_query($db, $sql);
$count = mysqli_num_rows($result);
if($count == 0){
$error = "There is no books in the list";
}
?>
<html>
<body>
<table border="2" style="width:80%;" align="center">
<tr>
<th>ISBN</th>
<th>Title of the book</th>
<th>Cost</th>
<th>Copies</th>
<th>Edition</th>
<th>Publisher</th>
<th>Copy Year</th>
<th>Shelf Number</th>
<th>Subject</th>
</tr>
<?php while($row = mysqli_fetch_array($result)){
$ISBN = $row['ISBN'];
$Title = $row['Title'];
$Cost = $row['Cost'];
$Copies = $row['Copies'];
$Edition = $row['Edition'];
$Publisher = $row['Publisher'];
$CopyYr = $row['CopyYr'];
$Shelf = $row['ShelfNo'];
$Subject = $row['SubName'];
?>
<tr>
<td><?php echo $ISBN; ?></td>
<td><?php echo $Title; ?></td>
<td><?php echo $Cost; ?></td>
<td><?php echo $Copies; ?></td>
<td><?php echo $Edition; ?></td>
<td><?php echo $Publisher; ?></td>
<td><?php echo $CopyYr; ?></td>
<td><?php echo $Shelf; ?></td>
<td><?php echo $Subject; ?></td>
</tr>
<?php
}
?>
</table>
<br>
<table align="center" style="width: 30%">
<tr>
<th><form action="AddBook.php" method="post">
<input type="submit" value="Add Book"/>
</form></th>
<th><form action="editbook.php" method="post">
<input type="submit" value="Edit List"/>
</form></th>
<th><form action="deletebook.php" method="post">
<input type="submit" value="Delete Book/s"/>
</form></th>
<th><form action="AdminSummary.php" method="post">
<input type="submit" value="Back"/>
</form></th>
</tr>
</table>
</center>
</body>
</html>
This will display 17 records in the table. But when I add another record to the table, It will display an incomplete record, a "500 server error", or an empty table.
I have been in the same problem for days, searching for a solution, and I can't find any.
I don't know what the problem is, so any help would be appreciated. Thank You :)
So, I have 3 tables in my database (phpMyAdmin) bikes, book and users. I have made bike_id and user_id a foreign key in the book table. Now I want these 2 to get inserted into the book table from the web page.
I've got 3 php files which are book1.php, book2.php and book3.php. In book1, you can select a bike which you want to book which results in showing that particular bike in new page. In book2, you select the date when you want to book and when you go to book3, the user_id and bike_id should be automatically inserted into the book table? But I only get ERROR when I press book in book2 page.
Can someone help me plz.. Below is the php codes for 3 php files.
book1.php:
<?php
require 'connect.php';
$select_posts = "select * from bikes ";
$run_posts = mysql_query($select_posts);
?>
<table cellpadding="2" cellspacing="2" border="2">
<tr>
<th>Name</th>
<th>Image</th>
<th>Available</th>
<th>Select Bike</th>
</tr>
<?php while($row=mysql_fetch_array($run_posts)){ ?>
<tr>
<td><?php echo $row[bike_name]; ?></td>
<?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
<td><?php echo $row[avail]; ?></td>
<td><a href="book2.php?id=<?php echo $row[bike_id];?>">Select</td>
</tr>
<?php } ?>
book2.php:
<?php
session_start();
?>
<?php
require 'connect.php';
if(isset($_GET['id'])){
$took_id = $_GET['id'];
$select_query = "select * from bikes where bike_id='$took_id'";
$run_query = mysql_query($select_query);
?>
<table cellpadding="2" cellspacing="2" border="2">
<tr>
<th>Name</th>
<th>Image</th>
<th>Available</th>
<th></th>
<th>Select Date</th>
<th>Book</th>
</tr>
<?php while($row=mysql_fetch_array($run_query)){ ?>
<tr>
<form action="book3.php" method="POST">
<td><?php echo $row[bike_name]; ?></td>
<?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
<td><?php echo $row[avail]; ?></td>
<td><?php echo $took_id; ?></td>
<td>Select Date: <input type="text" id="datepicker" name="datepicker"></td>
<td><input type="submit" name="Submit" value="Book"></td>
</tr>
<?php }} ?>
</table>
book3.php:
<?php
session_start();
?>
<?php
require 'connect.php';
// Get values from form
$datepicker = $_POST['datepicker'];
$sql="INSERT INTO $tbl_name(datepicker)VALUES('$datepicker')";
$sql="INSERT INTO $tbl_name(user_id)VALUES(select user_id from users)";
$sql="INSERT INTO $tbl_name(bike_id)VALUES(select bike_id from bikes)";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
?>
How would I be able to add an Edit to the table after the table.
Any help would be greatly appreciated. I've tried a number of variations such as : Trying to add Edit
<center><?php
//connect to mysql for search
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `2016server` WHERE CONCAT(`ServerGroup`,
`Week`, `Status`, `ServerName`, `IP`, `DateComplete`, `DateSched`,
`HeatTicket`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `2016server`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
//start the webpage
<html>
<head>
<title>SEARCH 2016 Servers</title>
</head>
<body>
<center><h3>Type in your query to search all of the 2016
Servers</h3><br>
<form action="search2016.php" method="post">
<input type="text" name="valueToSearch" placeholder="SEARCH">
<input type="submit" name="search" value="GO!"><br><br>
table
<table>
<tr>
<th>ServerGroup</th>
<th>Server Name</th>
<th>Date Scheduled</th>
<th>Heat Ticket</th>
<th>Status</th>
<th>Date Complete</th>
<th>Week</th>
<th>Downtime</th>
<th>IP Address</th>
<th>Operating System</th>
</tr>
populate table from mysql database
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['ServerGroup'];?></td>
<td><?php echo $row['ServerName'];?></td>
<td><?php echo $row['DateSched'];?></td>
<td><?php echo $row['HeatTicket'];?></td>
<td><?php echo $row['Status'];?></td>
<td><?php echo $row['DateComplete'];?></td>
<td><?php echo $row['Week'];?></td>
<td><?php echo $row['DT'];?></td>
<td><?php echo $row['IP'];?></td>
<td><?php echo $row['os'];?></td>
This is the section that I'm trying to add the edit to in the table. From the edit code above, it's trying to go to the 2016edit.php page with the ID how ever since there is no such page, it gets a 404 error.
</tr>
<?php endwhile;?>
</table>
</form>
</body>
</html>
close the first <form tag early (after the <input type="submit")
add new column named action where you'll put your edit button
use this for the edit
<form action="youreditform.php" method="post">
<button type="submit" name="edit" value="<?= $theidofthis; ?>">Edit</button>
</form>
Add another "td" tag and make it a link and it will go to the next edit page on click. but for editing purpose you have to post id in the url to check that which record was clicked to edit. hope it will be helpful.
<tr>
<td><?php echo $row['ServerGroup'];?></td>
<td><?php echo $row['ServerName'];?></td>
<td><?php echo $row['DateSched'];?></td>
<td><?php echo $row['HeatTicket'];?></td>
<td><?php echo $row['Status'];?></td>
<td><?php echo $row['DateComplete'];?></td>
<td><?php echo $row['Week'];?></td>
<td><?php echo $row['DT'];?></td>
<td><?php echo $row['IP'];?></td>
<td><?php echo $row['os'];?></td>
<td><a href='edit.php'>edit</a></td>
</tr>
So, I am trying to sort a database by clickable links. I have a function that is supposed to order them, but for some reason it isn't working. Also, once I add something to the database and submit, if I click on the links to order, it sends me to my actual function holding file in the url, which is weird.
Forgive table formatting issues. I'm still debugging that.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Project 1</title>
</head>
<body>
<h1 style='text-align:center;'>Movie Collection Database</h1>
<form method='post' action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table align='center' border='1' cellpadding='5'>
<tr>
<td>
<input type="text" size='40' name="movieTitle" value="Movie Title">
</td>
<td>
<input type="text" name="studioName" value="Studio Name">
</td>
<td>
<select name="movieRating">
<option value="G">G</option>
<option value="PG">PG</option>
<option value="PG-13">PG-13</option>
<option value="R">R</option>
<option value="NC-17">NC-17</option>
<option value="Not Rated">Not Rated</option>
</select>
</td>
<td>
<input type="text" name="publicationYear" value="2015">
</td>
<td>
<input type="number" name="imdbRating" value="10.0">
</td>
<td>
<input type="number" name="runTime" value="0">
</td>
<td>
<input type="checkbox" name="add">Add
</td>
</tr>
</table>
<?php
$db = new PDO("mysql:host=localhost;dbname=berkleyr", "berkleyr", "12345");
?>
<?php include_once("Project1DB.php"); ?>
<?php if ($_SERVER['REQUEST_METHOD'] === "POST"):
delete($db);
if(isset($_POST['add']))
{
try
{
Insert($db, $_POST['movieTitle'], $_POST['studioName'], $_POST['movieRating'], $_POST['publicationYear'], $_POST['imdbRating'], $_POST['runTime']);
}
catch (PDOException $error)
{
$db->rollback();
echo "Bad things Happened";
$db = null;
die("Connection failed: " . $error->getMessage());
}
}
?>
<table align='center' border='1'>
<tr>
<th>Title</th>
<th>Studio Name</th>
<th>Rating</th>
<th>Pub Year</th>
<th>IMDB Rating</th>
<th>Run Time</th>
<th>Delete</th>
</tr>
<?php foreach (Select($db) as $row): ?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['studio']; ?></td>
<td><?php echo $row['rating']; ?></td>
<td><?php echo $row['pub_year']; ?></td>
<td><?php echo $row['imdb_rating']; ?></td>
<td><?php echo $row['run_time']; ?></td>
<?php echo "<td><input type='checkbox' name='". $row['id'] . "' value='" . $row['id'] . "'></td>";?>
</tr>
<?php endforeach ?>
<?php
if (isset($db))
{
$db = null; // make sure an exception didn't already close the connection. If not, close it now.
}
?>
<?php else: ?>
<table align='center' border="1">
<tr>
<th>Title</th>
<th>Studio Name</th>
<th>Rating</th>
<th>Pub Year</th>
<th>IMDB Rating</th>
<th>Run Time</th>
<th>Delete</th>
</tr>
<?php foreach (Select($db) as $row): ?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['studio']; ?></td>
<td><?php echo $row['rating']; ?></td>
<td><?php echo $row['pub_year']; ?></td>
<td><?php echo $row['imdb_rating']; ?></td>
<td><?php echo $row['run_time']; ?></td>
<?php echo "<td><input type='checkbox' name='". $row['id'] ."' value='" . $row['id'] . "'></td>";?>
</tr>
<?php endforeach ?>
<?php endif ?>
<td colspan="7" align="center">
<input type='submit' name='submit' value='Update Database' />
</td>
</table>
</form>
</body>
</html>
Here is my function for sorting:
function orderBy($order)
{
switch($order)
{
case "title":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.title ASC");
break;
case "studio":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.studio ASC");
break;
case "rating":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.rating ASC");
break;
case "pub_year":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.pub_year ASC");
break;
case "imdb_rating":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.imdb_rating ASC");
break;
case "run_time":
$sorted = $this->db->prepare("SELECT * FROM movie ORDER BY movie.run_time ASC");
break;
}
$sorted->execute();
}