How do i link search result with HTML page in PHP - php

Below is the code that i have written that would display data from phpmyadmin table and display the result. Now once the search result is displayed, let's say if i wish to click on one of the search result for e.g. i clicked on "Jon Doe" then upon click i should be redirected to the profile page (HTML page) of Jon Doe. May i know how do i do that ?
<?php
echo "<body style='background-color:gray'>";
include ("account.php");
( $dbh = mysql_connect( $hostname, $username, $password ))
or die ( "uable to connect to MYSQL database" );
mysql_select_db( $project );
if (isset($_POST['search'])) {
$sql= "SELECT * FROM registration ";
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE first_name= '{$search_term}'";
$sql .= " OR last_name= '{$search_term}'";
$query=mysql_query($sql) or die(mysql_error());
}
?>
<html>
<head>
<title>jon</title>
</head>
<body>
<form name="search_form" method="POST" action="retrieve.php">
<table width="599" border="1">
<tr>
<th>Search
<input type ="text" name ="search_box" value=""/>
<input type="submit" name="search" value="Find Users">
</tr>
</table>
</form>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">First Name </div></th>
<th width="98"> <div align="center">Last Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">City </div></th>
<th width="59"> <div align="center">Country </div></th>
<tr>
<?php if (isset($_POST['search'])) {
while ($row=mysql_fetch_array($query)){ ?>
<tr>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['address_city'];?></td>
<td><?php echo $row['address_country'];?></td>
<tr>
<?php }} ?>
</table>

As Funk Doc already suggested you should make a "profile.php" for example which then queries all informations.
First you need to link the profile page. You need a "id" from your database.
<td><?php echo $row['first_name'];?></td>
Now you will redirect your user to profile.php with a special id.
example: profile.php?id=87341
In your profile.php you now need to get the id variable.
profile.php
<?php
$userid = $_GET['id'];
?>
The id is now saved in ´$userid´. Just search your database for that id and you get all your informations.

Ok lets say you have 2 Users John and Nick. John has id=1 and Nick id=2
John
Nick
handler.php
<?php
$id=$_GET['id'];
$query='SELECT * FROM #__users where id='.$id;
//show your results
?>

Related

How do I make a button within an HTML table, to update info in my database

I am totally new to coding. That being said I hope you can help me with this issue I am having.
I use PHP and Mysqli to populate information within a html table. I have added a button in one of the columns. I want to click the button and update the information on that row of the table, in the database.
The users earn a commission, so by clicking the button, I want to move the amount in the "pay" column to the "paid" column and update the database.
When I click the button, I get the "success" message that says "Payment Processed Successfully", but nothing changes in the database!
Thanks ahead of time for any help you can provide.
Here's my code:
<html>
<body>
<div class="form">
<h2>View Records</h2>
<table width='100%'>
<thead>
<tr>
<th><strong>user ID</strong></th>
<th><strong>Fist Name</strong></th>
<th><strong>Last Name</strong></th>
<th><strong>pay commission</strong></th>
<th><strong>Paid</strong></th>
<th><strong>Pay Commission</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
<?php
$count=1;
include "db.php";
$sql = "SELECT * FROM users ";
$result = $conn-> query($sql);
if($result -> num_rows > 0){
while ($row = $result-> fetch_assoc()) { ?>
<td align="center"><?php echo $row["id"]; ?></td>
<td align="center"><?php echo $row["first_name"]; ?></td>
<td align="center"><?php echo $row["last_name"]; ?></td>
<td align="center"><?php echo $row["pay"]; ?></td>
<td align="center"><?php echo $row["paid"]; ?></td>
<td align="center">
<div class="input-group">
<form class="reset-form" action="members.php" method="post" >
<div class="input-group">
<button type="submit" class="button_1" name="pay-commission">Pay Commission</button></div></form>
</td>
<td align="center">
Delete
</td>
</tr>
<?php
}
// process commission*******
if(isset($_POST["pay-commission"])){
$id=$row['id'];
$pay=$row['pay'];
$paid=$row['last_paid'];
//date and time of transaction
$trn_date = date("Y-m-d H:i:s");
require('db.php');
$ins_query="update users last_paid='$pay' trm_date='$trn_date' where id= '$id'";
mysqli_query($conn,$ins_query);
if($ins_query){
echo "<p class= 'success'> Payment Processed Successfully <p>";
}else{
echo"<p class= 'error'>something went wrong!!</p>";
}
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
got it working except it updates all users with one click on any button in the "pay commission" column. may not be a bad thing to process all commissions with one click.
Maybe I move the button below the table, to update all users with one click. all of this code is on a page called members.php
<html>
<head></head> <header></header>
<body>
//table structure
<div class="form">
<h2>View Records</h2>
<table width='100%'>
<thead>
<tr>
<th><strong>user ID</strong></th>
<th><strong>Fist Name</strong></th>
<th><strong>Last Name</strong></th>
<th><strong>pay commission</strong></th>
<th><strong>Paid</strong></th>
<th><strong>Pay Commission</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
//populate table with user data
<?php
$count=1;
include "db.php";
$sql = "SELECT * FROM users ";
$result = $conn-> query($sql);
if($result -> num_rows > 0){
while ($row = $result-> fetch_assoc()) { ?>
<td align="center"><?php echo $row["id"]; ?></td>
<td align="center"><?php echo $row["first_name"]; ?></td>
<td align="center"><?php echo $row["last_name"]; ?></td>
<td align="center"><?php echo $row["pay"]; ?></td>
<td align="center"><?php echo $row["last_paid"]; ?></td>
<td align="center">
<div class="input-group">
<form action="members.php" method="post" >
<div class="input-group">
<button type="submit" class="button_1" name="pay-commission">Pay commission</button></div></form>
</td>
<td align="center">
Delete
</td>
</tr>
<?php
// process commission*******
if(isset($_POST["pay-commission"])){
$id=$row['id'];
$pay=$row['pay'];
$paid=$row['last_paid'];
//set the date and time of transaction
$trn_date = date('Y-m-d');
$conn->query("UPDATE users SET last_paid='$pay' WHERE id= '$id'");
$conn->query("UPDATE users SET pay=0.00 WHERE id= '$id'");
$conn->query("UPDATE users SET trn_date='$trn_date' WHERE id= '$id'");
if($conn){
//refresh the page to see changes
header ('Location: members.php');
echo '<p class="success"> A Payment of $$pay Processed Successfully for member ID $id</p>';
}else{
echo '<p class="error">Something went wrong! </p>';
}
}
}
}
$conn->close();
?>
</tbody>
</table>
</div>
</body>
</html>

php not returning any echo

I have a table which Displays the book list from mysql Database and search form where user can search books.
I"m looking forward to show the book list as per title and author when user search for by input value of title and author and display " NO Books by name or author" as echo when there is no any record.
My code is
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<center><h2>Central Department of Physics</h2></center>
<br>
<?php
include("DBConnection.php");
$search = isset($_REQUEST["search"]) ? $_REQUEST["search"] : '';
$query = "select ISBN,Title,Author,Edition,Publication from book_info where author like '%$search%' or title like '%$search%'";
//search with a book name in the table book_info
$result = mysqli_query($db,$query);
?>
Go Back
<table border="2" align="center" cellpadding="5" cellspacing="5">
<tr>
<th> ISBN </th>
<th> Title </th>
<th> Author </th>
<th> Edition </th>
<th> Publication </th>
</tr>
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row["ISBN"];?> </td>
<td><?php echo $row["Title"];?> </td>
<td><?php echo $row["Author"];?> </td>
<td><?php echo $row["Edition"];?> </td>
<td>Click Me</td>
</tr>
<?php
}
}
else{ ?>
<tr>
<td colspan="5">
<center>No books found in the library by the name $search </center>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
<br>
My Search form is
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<form action = "DisplayBooks.php" method="get">
<br>
<center>Enter the title of the book to be searched :
<input type="text" name="search" size="48">
<br></br>
<input type="submit" value="submit">
<input type="reset" value="Reset">
</center>
<br>
</form>
</body>
</html>
But it sucessfully displays list of books but when there is no any record ..it don't launch echo.
ps. How can I add link button such that it shows Back to search results to navigate user to Searchform and user can go back to previous form.
You have duplicated your check for "no results":
if(mysqli_num_rows($result)>0)if(mysqli_num_rows($result)>0)
Remove one.
Try This:
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<center><h2>Central Department of Physics</h2></center>
<br>
<?php
include("DBConnection.php");
$search = isset($_REQUEST["search"]) ? $_REQUEST["search"] : '';
$query = "select ISBN,Title,Author,Edition,Publication from book_info where author like '%$search%' or title like '%$search%'";
//search with a book name in the table book_info
$result = mysqli_query($db,$query);
?>
<table border="2" align="center" cellpadding="5" cellspacing="5">
<tr>
<th> ISBN </th>
<th> Title </th>
<th> Author </th>
<th> Edition </th>
<th> Publication </th>
</tr>
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row["ISBN"];?> </td>
<td><?php echo $row["Title"];?> </td>
<td><?php echo $row["Author"];?> </td>
<td><?php echo $row["Edition"];?> </td>
<td>Click Me</td>
</tr>
<?php
}
}
else{ ?>
<tr>
<td colspan="5">
<center>No books found in the library by the name $search </center>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
<br>

Data not being pulled when typing keyword in the search box

I have search feature that I setup. When I type the keyword in I get no records back and no error message. Just the table header. I see the department other in the database. When I type it in the keyword box I get nothing back.
<html>
<head>
<title></title>
</head>
<body>
<form name="frmSearch" method="get" action="">
<table width="599" border="1">
<tr>
<th>Keyword
<input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo $_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?php
if($_GET["txtKeyword"] != "")
{
$serverName = "localhost";
$objConnect = new PDO( "sqlsrv:server=$serverName ; Database=maintenance", "TestUser", "test") or die("Error Connect to Database");
// Search By lanId or department
$objQuery = $objConnect->prepare("SELECT * FROM requests WHERE (lanId LIKE '%".$_GET["txtKeyword"]."%' or department LIKE '%".$_GET["txtKeyword"]."%' ) ");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">lanId </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">department </div></th>
</tr>
<?php
while( $objResult = $objQuery->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><div align="center"><?php echo $objResult["lanId"];?></div></td>
<td><?php echo $objResult["name"];?></td>
<td><?php echo $objResult["department"];?></td>
<?php
}
?>
</table>
<?php
}
?>
</body>
</html>
When you use prepare() statement you should also use execute() :
http://coursesweb.net/php-mysql/pdo-prepare-execute

Submit data from PHP to HTML

I have created a search page on PHP that searches for user from database. After the results are retrieved, i have created a view button that would display profile information of that user in HTML page. I am not quite sure how to send information from PHP to HTML. Below is the code for your reference. I would appreciate if someone could shed some light on this.
retrieve.php
<?php
echo "<body style='background-color:#DCDCDC'>";
// echo '<img src="back to school.jpg'. $row['filename'] .'" style="width:600px; height:300px;" alt="" /><br />';
include ("account.php");
( $dbh = mysql_connect( $hostname, $username, $password ))
or die ( "unable to connect to MYSQL database" );
mysql_select_db( $project );
if (isset($_POST['search'])) {
$sql= "SELECT * FROM BPi_registration ";
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE first_name= '{$search_term}'";
$sql .= " OR last_name= '{$search_term}'";
$query=mysql_query($sql) or die(mysql_error());
}
?>
<html>
<head>
<title>Jon</title>
</head>
<body>
<form name="search_form" method="POST" action="retrieve.php">
<table width="599" border="2">
<tr>
<th>Search Here
<input type ="text" name ="search_box" value=""/>
<input type="submit" name="search" value="Find Users">
</tr>
</table>
</form>
<table width="600" border="2">
<tr>
<th width="91"> <div align="center">First Name </div></th>
<th width="98"> <div align="center">Last Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">City </div></th>
<th width="97"> <div align="center">State </div></th>
<th width="59"> <div align="center">Country </div></th>
<th width="59"> <div align="center">View </div></th>
<tr>
<?php if (isset($_POST['search'])) {
while ($row=mysql_fetch_array($query)){ ?>
<tr>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['address_city'];?></td>
<td><?php echo $row['address_state'];?></td>
<td><?php echo $row['address_country'];?></td>
<td><input type="button" value="view"/> <td>
<tr>
<?php }} ?>
</table>
handler.php
<?php
$userid = $_GET['id'];
?>
Maybe you can use PHP Sessions.
* edits *
In this part you nested a button inside a tag.
<td><input type="button" value="view"/> <td>
Try just adding text there, like:
<td>View<td>
This will appear as clickable text "View" in blue font color and will redirect to hander.php .
Hope that helps.

Select Data by check box click next and unselect the selected row

I want to create a crf form for my university project .
i created course table i select some data from course but where student click the completed course and next click .then next page show same data base table but not those courses, student already selected.and
then student can subset for applied courses.
But when i select some course and click next, But it show the previous row in course table. but i want selected course row not select when i click . cause completed course not want to select for applying course.
I want to select some row from a database when aply it show the same databases all row except the selected row
i add the same line in the problem line..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>
CRF Form
</title>
<link rel="stylesheet" href="CSS/admin_style.css" >
</head>
<body>
<div id="header">
<a href="index.php">
<h1>
Welcome to the CRF Form
</h1>
</a>
</div>
<form action="selection.php" method="POST" enctype="multipart/form-data" >
<div id="">
<table width="1000" border="5" align="center">
<tr>
<td colspan="8" align="center" bgcolor="yellow">
<h1>
Slect your completed course
</h1>
</td>
</tr>
<tr bgcolor="orange">
<th>
selection:
</th>
<th>
Course id:
</th>
<th>
Course title:
</th>
<th>
Course credits:
</th>
<th>
Course statust:
</th>
<th>
Delete Post:
</th>
<th>
Edit Post:
</th>
</tr>
<?php
include("includes/connect.php");
$query="select * from course";
$run=mysql_query($query);
while($row=mysql_fetch_array($run)){
$id=$row['id'];
$course_id=$row['course_id'];
$course_title=$row['course_title'];
$course_credits=$row['course_credits'];
$course_status=$row['course_status'];
?>
<tr align="center" bgcolor="pink">
<td>
<input type="checkbox" name="complete[]" value="<?php echo $id; ?>" />
</td>
<td>
<?php echo $course_id; ?>
</td>
<td>
<?php echo $course_title; ?>
</td>
<td>
<?php echo $course_credits; ?>
</td>
<td>
<?php echo $course_status; ?>
</td>
<td>
<a href="delete.php?del=<?php echo $post_id ?>">
Delete
</a>
</td>
<td>
<a href="Edit.php?edit=<?php echo $post_id ?>">
Edit
</a>
</td>
</tr>
<?php } ?>
<tr>
<td align="center" colspan="7">
<input type="submit" name="sub" value="NEXT">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
this is the selec.php
<html>
<head>
<title>
CRF Form
</title>
<link rel="stylesheet" href="CSS/admin_style.css" >
</head>
<body>
<div id="header">
<a href="index.php">
<h1>
Welcome to the CRF Form
</h1>
</a>
</div>
<div id="">
<table width="1000" border="5" align="center">
<tr>
<td colspan="8" align="center" bgcolor="yellow">
<h1>
Slect your completed course
</h1>
</td>
</tr>
<tr bgcolor="orange">
<th>selection:</th>
<th>Course id:</th>
<th>Course title:</th>
<th>Course credits:</th>
<th>Course statust:</th>
<th>Delete Post:</th>
<th>Edit Post:</th>
</tr>
<?php
include("includes/connect.php");
$check=$_POST['complete'];
foreach($check as $ch){
$select= " id!='".$ch."' and ";//here is the problem
}
$query="select * from course
where $select id!='0'";
$run=mysql_query($query);
while($row=mysql_fetch_array($run)){
$id=$row['id'];
$course_id=$row['course_id'];
$course_title=$row['course_title'];
$course_credits=$row['course_credits'];
$course_status=$row['course_status'];
?>
<tr align="center" bgcolor="pink">
<td>
<input type="checkbox" name="completed" /></input>
</td>
<td><?php echo $course_id; ?></td>
<td><?php echo $course_title; ?></td>
<td><?php echo $course_credits; ?></td>
<td><?php echo $course_status; ?></td>
<td>Delete</td>
<td>Edit</td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
I think that you're saying the once the course is selected it shouldn't be displayed on the next page where the student can have a look on the other courses?
If is so then you can use following sql query on the next page where you don't want to display the student's completed course.
SELECT * FROM course WHERE id != $course_id
Let me know if I'm wrong. I didn't comment out as my reputations were low and stackoverflow didn't allow me to.
[EDITED]
This is your complete code.
Your select php file:
//assuming that you are logging in the students with their username or email id, if so then store their username in a session where logging in.
<?php
$user = $_SESSION['username'];
include("includes/connect.php");
if (isset($_POST['submit'])){
$course_id= $_POST['course_id'];
$course_title= $_POST['course_title'];
$course_credits= $_POST['course_credits'];
$course_status= $_POST['course_status'];
$query="SELECT course.id,course.title,course.credits,course.status FROM course WHERE course.username = $user";
$run=mysqli_query($conn,$query);
while($row=mysqli_fetch_array($run)){
$course_id= $_SESSION['course_id'] = $row['course_id'];
$course_title=$row['course_title'];
$course_credits=$row['course_credits'];
$course_status=$row['course_status'];
}
?>
Now in your next php file :
$already_selected_course = $_SESSION['course_id'];
Now the query should look like.
$query = "SELECT course.id,course.title,course.credits,course.status FROM course WHERE course.id != $already_selected_course";
This is it. Note: This solution might contain some errors of brackets etc but the logic is clear.
For better knowledge have a look at my MySQL Complete Video Series here!

Categories