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

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!

Related

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>

Can't seem to add simple user input to my DB via a form

This is very quick.
I have a form that when submitted should add user input into my DB. I have had a similar form working well but for some reason this one is not working.
Please excuse my sloppy code but here is the whole page. For some reason 'observation' will not take the user input and display it on the next page (showOne.php). The next page simply shows a blank space for observation and I checked the DB and there was nothing in the observation field of course.
Is it something simple that I can't see? I've been at this for days since it is identical to another form I have that works well on another page.
Thank you for any insight.
<?php
//including the database connection file
include("config.php");
$activePage = "start.php";
if(isset($_POST['update'])) {
$id = $_POST['id'];
$observation = $_POST['observation'];
$result = mysqli_query($mysqli, "UPDATE tasks SET observation='$observation' WHERE id=$id");
header("Location: showOne.php?id=$id");
}
//getting id from url
$id = $_GET['id'];
//selecting data associated with this particular id
$result2 = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id ORDER BY id ASC");
$result = mysqli_query($mysqli, "SELECT * FROM tasks ORDER BY taskNumber ASC");
include_once "nav.php";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pathfinder - A User Testing Dashboard</title>
<meta name="description" content="Pathfinder - A User Testing Dashboard">
<meta name="author" content="James Stables - Thurs April 13th 2017">
<link rel="stylesheet" href="core.css">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,700" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"> </script>
<![endif]-->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<header>
<h1 class="logo"><img src="images/logo.png">Pathfinder</h1>
<h4>User Testing Dashboard</h4>
</header>
<section class="nonav">
<div class="showAllUsers">
<table border='0'>
<tr>
<?php while($res = mysqli_fetch_array($result2))
{ ?>
<td class="widthHundred no-border left"><h2><?php echo $res['nameFirst']; ?></h2></td>
<?php } ?>
</tr>
</table>
<table border='0'>
<thead>
<tr>
<th>Image</th>
<th>Category</th>
<th>Task Number</th>
<th>Task Name</th>
<th>Observation</th>
<td> </td>
<td> </td>
<td> </td>
</tr>
</thead>
<tbody>
<form name="start" method="post" action="start.php" enctype="multipart/form-data">
<tr>
<td><a href="<?php echo $res['$image'];?>" data-lightbox="example-1" data-title="Task Number <?php echo $res['taskNumber'];?>">
<img id="imageResize" src="<?php echo $res['image'];?>" /></a></td>
<td><?php echo $res['category'];?></td>
<td><?php echo $res['taskNumber'];?></td>
<td><?php echo $res['taskName'];?></td>
<td><input type="text" name="observation" value="<?php echo $res['observation'];?>"></td>
<td> </td>
<td> </td>
<td> </td>
<?php } ?>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><input type="hidden" name="id" value="<?php echo $_GET['id'];?>"><input type="submit" class="buttonUpdate right" name="update" value="Update"></td>
<td><a class="buttonCancel right" href="tasks.php" onClick="return confirm('Cancel and go back?')">Cancel</a></td>
</tr>
</form>
</tbody>
</table>
</div>
</section>
<script src="js/lightbox-plus-jquery.min.js"></script>
<script>
lightbox.option({
'resizeDuration': 200,
'showImageNumberLabel': false,
'fitImagesInViewport': true
})
</script>
</body>
</html>

How do i link search result with HTML page in 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
?>

Errors appear in searching if a record not found using searchtext

SearchText works if record is found but errors appear when record not found.
Here are the errors. Notice: Undefined variable: sample_list in C:\xampp\htdocs\viewsample\samplelist.php on line 166
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ viewsample\samplelist.php on line 166
If I click the search button again, errors disappear.
Is it possible to return to home page if record not found?
I searched here a possible solution to my problem but I found none.
Please help.
Thank you and more power.
Here's the code.
Index.php
//Perform Search from our database
if(isset($_POST['action_type']))
{
echo 'Welcome';
if ($_POST['action_type'] == 'search')
{
$search = mysqli_real_escape_string($link, strip_tags($_POST['searchText']));
$sql = "select samp_id, daterecv, datecoll, modcoll, aperson, estabname, estabadd, gname, bname, prodcat, dform, dstrength, from sample
where gname like '%$search%' or bname like '%$search%' or estabname like '%$search%'";
$result = mysqli_query($link, $sql);
if(!$result)
{
echo mysqli_error($link);
exit();
}
//Loop through each row on array and store the data to $sample_list[]
while($rows = mysqli_fetch_array($result))
{
$sample_list[] = array('samp_id' => $rows['samp_id'],
'daterecv' => $rows['daterecv'],
'datecoll' => $rows['datecoll'],
'modcoll' => $rows['modcoll'],
'apers' => $rows['apers'],
'estabname' => $rows['estabname'],
'estabadd' => $rows['estabadd'],
'gname' => $rows['gname'],
'bname' => $rows['bname'],
'prodcat' => $rows['prodcat'],
'dform' => $rows['dform'],
'dstrength' => $rows['dstrength']);
}
include 'samplelist.php';
exit();
}
}
samplelist.php
<?php
session_start();
$role = $_SESSION['sess_userrole'];
if(!isset($_SESSION['sess_username']) && $role!="sampler"){
header('Location: index.php?err=2');
}
?>
<?php
include_once 'index.php';
?>
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function GotoHome(){
window.location = 'index.php';
}
{
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
}
</script>
</head>
<body>
<div class="wrapper">
<div class="content">
<p>Logout</p>
<p><center><b><font style="arial" color="black">SAMPLE COLLECTION</font></b></center></p>
<div style="margin-bottom: 10px;">
<form method="POST" action="index.php">
<input type="text" id="searchText" name="searchText" style="width:300px"/>
<input type="hidden" name="action_type" value="search" onfocus="Clear (this);"/>
<input type="submit" value="search"/>
</form>
</div>
<div style="max-height: 740px; overflow:auto; max-width: 1600; overflow:auto;">
<table class="pbtable">
<thead>
<tr>
<th>
Date Received
</th>
<th>
Date Collected
</th>
<th style = "display:none">
Mode of Collection
</th>
<th style = "display:none">
Assigned Personnel
</th>
<th>
Establishment Name
</th>
<th style = "display:none">
Establishment Address
</th>
<th>
Generic Name
</th>
<th>
Brand Name
</th>
<th>
Product Category
</th>
<th style = "display:none">
Dosage Form
</th>
<th style = "display:none">
Dosage Strength
</th>
</thead>
<tbody>
<?php foreach($sample_list as $collection) : ?>
<tr>
<td>
<?php echo $collection["daterecv"]; ?>
</td>
<td>
<?php echo $collection["datecoll"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["modcoll"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["aperson"]; ?>
</td>
<td>
<?php echo $collection["estabname"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["estabadd"]; ?>
</td>
<td>
<?php echo $collection["gname"]; ?>
</td>
<td>
<?php echo $collection["bname"]; ?>
</td>
<td>
<?php echo $collection["prodcat"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["dform"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["dstrength"]; ?>
</td>
<form method="post" action="index.php">
<input type="hidden" name="ci"
value="<?php echo $collection["samp_id"]; ?>" />
<input type="hidden" name="action" value="edit" />
<input type="submit" value="Edit" />
</form>
<tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
If no records are found in DB the $sample_list array is empty.
So samplelist.php needs this check before foreach loop,
if (empty($sample_list))
{
header('Location: http://www.homesweet.home/');
exit;
}
foreach($sample_list as $collection) :
...
endforeach;

Repeated page design when retrieving entry from MySQL database

I just made a PHP script to retrieve entry from database. When I retrieve entry from a table, the page look like this. The header and form below table is repeated. I need help, thanks.
Here's the whole code I'm using:
<?php
session_start();
include('connection.php');
$username='username';
mysql_query("SELECT * FROM regmember WHERE username='$username'");
$query=("SELECT * FROM product");
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="index.css">
<title>Music Light</title>
<div align="center"><h1>Music Light</h1></div>
<div id="menu">
<ul>
<li><?php echo 'Welcome, '.$_SESSION['username']; ?></li>
<li>Home</li>
<li>Product</li>
<li>Profile</li>
<li>Cart</li>
<li>Testimony</li>
<li>Transaction</li>
<li>Logout</li>
</ul>
</div>
<br>
<br>
<div align="center">
<table border="0">
<tr>
<td>ID</td>
<td></td>
<td><?php echo $row[0];?></td>
</tr>
<tr>
<td>Brand</td>
<td></td>
<td><?php echo $row[1];?></td>
</tr>
<tr>
<td>Instrument Type</td>
<td></td>
<td><?php echo $row[2];?></td>
</tr>
<tr>
<td>Price</td>
<td></td>
<td><?php echo $row[3];?></td>
</tr>
<tr>
<td>Stock</td>
<td></td>
<td><?php echo $row[4];?></td>
</tr>
<tr>
<td>Image</td>
<td></td>
<td><img height="150" width="150" src="productimg/<?php echo $row[6];?>"/></td>
</table>
</div>
<div align="center">
<table>
<form name="deleteentry" action="delete.php" method="get">
<tr>
<td>Delete which entry? (enter product id)</td>
<td><input type="text" name="delete"></td>
<td><input type="button" name="deletebutton" value="Delete"></td>
</tr>
</form>
</table>
</div>
<?php
}
?>
<br>
<br>
<div align="center"><p>Description template</p></div>
<footer>
<p align="center">Copyright © 2013 Music Light</p>
</footer>
missing </head> and <body> and </body>
i would suggest that the <html> <head></head> and <body> to be used outside the loop if u donot actually want to repeat. by loop i mean the while loop
You didn't closed your header with </head>
<head>
<link rel="stylesheet" type="text/css" href="index.css">
<title>Music Light</title>
</head> <= Include this
.
.
.
</html> <= Include this as well at the end

Categories