I am trying to make each row in my table clickable. So when a user clicks on a row it should go to a details page containing the data from that row.
Part of my html code:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
session_start();
include 'config.php';
$query_string = "SELECT * FROM tbl_User INNER JOIN tbl_Role ON tbl_User.role_id=tbl_Role.role_id";
$query = mysqli_query($con, $query_string);
?><!DOCTYPE html>
<html lang="en">
<head>
....
</head>
<body>
<table id="myTable" class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Profile Picture</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($query)){
echo "<tr>";
echo "<td>{$row['name']}</td>\n"
echo "<td>".$row['email']."</td>";
echo "<td><img class='picTable' src='plugins/images/".$row['profile_picture']."'></td>";
echo "<td>".$row['role']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</body>
<script>
$("#myTable").on("click", "tr", function(e){
// get the value
var value = $(this).find('td:first').text();
// redirect the user with the value as a GET variable
window.location = 'user_detail.php?name=' + value;
});
</script>
</body>
My table looks like this:
When I click on a row it does redirect to the page, with the id in the url
the url for the user details page looks like this:
http://localhost:8888/challenger/user_detail.php?user_id=5
Code for the user detail page is:
<?php
session_start();
include_once 'config.php';
$qry = "SELECT * FROM tbl_User WHERE user_id = {$_GET['user_id'] "
$query = mysqli_query($con, $qry);
while($row = mysqli_fetch_array($query)){
echo $row['email'];
echo $row.['name'];
}
?>
Then I click through to the details page, it show an error (internal server error, which means for me out of experience that my php code is incorrect).
So my question is how do i display all the data (name, email, role, etc) in the details page?
I figured out how to display the data, so on the user detail page I have the following code above the DOCTYPE html tag:
<?php
session_start();
include_once 'config.php';
$id = $_GET['user_id'];
$query_string = "SELECT * FROM tbl_User INNER JOIN tbl_Role ON tbl_User.role_id=tbl_Role.role_id WHERE user_id = '$id'";
$query = mysqli_query($con, $query_string);
?>
Then in the body i read out the data like so:
<h3 class="box-title">User Details</h3>
<?php
while($row = mysqli_fetch_assoc($query)){
echo "<h1>".$row['name']. "</h1>";
echo "<h1>".$row['email']. "</h1>";
echo "<h1>".$row['role']. "</h1>";
echo "<div><img class='picTable' src='plugins/images/".$row['profile_picture']."'></div>";
}
?>
Related
right now I have a page which displays homework set by teachers from a database. The students must be able to see all their homework on this page, with the due date and set date. As of now, it's working and after the due date, the task turns red, which is fine. However, I need to now add a small box or button which can be clicked by the student once they have completed the task. Once this is done, It would delete it ONLY for the student which has clicked it.
<?php
include_once("connection.php"); //including the database connection file
$id= $_GET['id'];
$result = $conn->prepare("SELECT * FROM homework WHERE class_id=? ORDER BY datedue DESC");
$result->bind_param("i", $id);
$result->execute();
$result2 = $result->get_result();?>
<html>
<head>
<title>View IS</title>
</head>
<body>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Task</td>
<td>Date Set </td>
<td>Date Due </td>
<td><button type="button">Click Me!</button></td>
</tr>
<?php
while($res = mysqli_fetch_array($result2)) {
if (strtotime(date("d-m-Y")) > strtotime($res['datedue'])) {
echo "<tr style=\"color: red;\">";
echo "<td>".$res['description']."</td>";
echo "<td>".$res['dateset']."</td>";
echo "<td>".$res['datedue']."</td>";
echo "<td>".<button type=button>Click Me!</button>."</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td>".$res['description']."</td>";
echo "<td>".$res['dateset']."</td>";
echo "<td>".$res['datedue']."</td>";
echo "<td>".<button type=button>Click Me!</button>."</td>";
echo "</tr>";
}
}
?>
</table>
</body>
How can I do this? Thank you
I couldn't test this, can you give this a try and let me know if error occurs
create new field name 'stud_completed' in homework table
homework.php page
<?php
include_once("connection.php"); //including the database connection file
$id = $_GET['id'];
$result = $conn->prepare("SELECT * FROM homework WHERE class_id=? ORDER BY datedue DESC");
$result->bind_param("i", $id);
$result->execute();
$result2 = $result->get_result();
$todayDate = strtotime(date("d-m-Y"));
$Log_student = $_SESSION['studentID'];
?>
<html>
<head>
<title>View IS</title>
</head>
<body>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Task</td>
<td>Date Set </td>
<td>Date Due </td>
<td>Action</td>
</tr>
<?php
while($res = mysqli_fetch_array($result2)) {
$redDueTask = null; // each loop $redDueTask will be set to NULL
$homeworkID = $res['id']; // Get the DueDate of each task
$dueDate = strtotime($res['datedue']); // Get the DueDate of each task
if ($todayDate > $dueDate) { $redDueTask = 'style="color: red;"'; } // Set $redDueTask if task has past duedate
$student_completed = explode(',',$res['stud_completed']); // get the coma seperated completed student list and convert it to array
if (!in_array($Log_student, $student_completed)) { // chk if logged in student ID is in array and if not in the list show task
?>
<tr <?php echo $redDueTask?>>
<td><?php echo $res['description']?></td>
<td><?php echo $res['dateset']?></td>
<td><?php echo $res['datedue']?></td>
<td>
<?php if (isset($redDueTask)) { // $redDueTask will bset if the task duedate has passed, so no need compelete button ?>
Time UP!
<?php } else { // $redDueTask is not set then show compelete button ?>
<button type='button'>Have Complete</button>
<?php } ?>
</td>
</tr>
<?php
}
}
?>
</table>
</body>
taskdone.php page
<?php
include_once("connection.php"); //including the database connection file
$tid = $_GET['tid']; // Get Homework Task ID from URL
$Log_student = $_SESSION['studentID']; // Get Loggedin Student ID from Session
// Get ROW Statment
$result = $conn->prepare("SELECT * FROM homework WHERE id=?");
$result->bind_param('i', $tid);
$result->execute();
$result2 = $result->get_result();
$res = mysqli_fetch_array($result2);
$stud_completed = $res['stud_completed']; // Get the current List of completed student
if ($stud_completed == "") { // If stud_completed is null or blank
$stud_completed = $Log_student; // add the current student ID with out coma
} else {
$stud_completed .= "," . $Log_student; // Inculde the current logged in student ID with coma
}
// Update ROW Statement
$sql = "UPDATE homework SET stud_completed=? WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $stud_completed, $tid);
if ($stmt->execute()) {
header("homework.php"); // if GOT updated go to home work task list page
}
?>
I guess it really depends on my code but if I have the following code, can I just open a HTML tag and create a table there? I find it easier to create table using HTML than using PHP. Would it be okay to add table in html or must I echo it out in PHP for the following code?
<?php
include_once 'header2.php';
if(!$_SESSION['u_uid']) {
header ("Location: index.php?display_music_forum=notlogin");
exit();
} else {
// select query drom the database to insert below in html
$admin = 1;
$sql = "SELECT * FROM display_music_forum WHERE admin = ?;";
if(!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL error";
} else {
mysqli_stmt_bind_param($stmt, "i", $admin)
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0 && $_SESSION['u_permission'] == 0) {
header("Location: header2.php?display_music_forum=nopermission");
exit();
} else {
$row = mysqli_fetch_assoc($result);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr>
<th>category</th>
<th>Creator</th>
<th>Date Created</th>
</table>
</body>
</html>
You should not "echo" HTML with PHP. It is better to leave all the text and html outside of PHP.
In your case, in order to have better oraginized code you may do all the php work in the top of your file, and do only the loop (foreach or for) inside the html body.
For exampleQ
<?php
include_once 'header2.php';
if(!$_SESSION['u_uid']) {
header ("Location: index.php?display_music_forum=notlogin");
exit();
} else {
// select query drom the database to insert below in html
...
...
$results = YOUR DATABASE RESULTS;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr>
<th>category</th>
<th>Creator</th>
<th>Date Created</th>
</tr>
<?php foreach ($results as $result) { ?>
<tr>
<td><?=$result['category'];?></td>
<td><?=$result['creator'];?></td>
<td><?=$result['date_created'];?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
You can embed all the html tags inside PHP by using echo.
Here is an example:
$query = $db->rawQuery("SELECT last_modified,version,last_saved_by,comments from tbl_templates WHERE slug = '".$slug."' order by id desc limit 5");
$i = 1;
echo '<table class="lastmodified" temp="true" border="1" align="center" width="100%">';
echo '<tr><td align="center"><h4><b>SL No.</b></h4></td><td align="center"><h4><b>Last version modified</b></h4><td align="center"><h4><b>Last Modified Date and Time</b></h4></td><td align="center"></td><hr><td align="center"><h4><b>Last Saved By</b></h4></td><td align="center"><h4><b>Comments</b></h4></td></tr>';
foreach ($query as $row){
echo "<tr><td align='center'><h4>".$i++."</h4></td><td align='center'><h4>{$row['version']}</h4></td><td align='center'><h4>{$row['last_modified']}</h4></td><td><hr></td><td align='center'><h4>{$row['last_saved_by']}</h4></td><td align='center'><h4>{$row['comments']}</h4></td></tr>";
}
echo '</table>';
l have created an application using php,html and mysql. The application can store a user's information such as id, name, bio, and date created into the database and display in html table. The id is an auto increment value which increases with every data entered by the user. The insert part of the application works fine but when l try to delete a record nothing happens. An html form is part of the code which l have intentionally decided not to include. Here is a snapshot of my code:
$records = array();
if(!empty($_POST)) {
if(isset($_POST['firstName'],$_POST['lastName'], $_POST['bio'])){
$firstName = trim($_POST['firstName']);
$lastName = trim($_POST['lastName']);
$bio = trim($_POST['bio']);
if(!empty($firstName) && !empty($lastName) && !empty($bio)) {
$insert = $db->prepare("INSERT INTO people (firstName, lastName,
bio, created) VALUES (?, ?,?, NOW())");
$insert->bind_param('sss', $firstName, $lastName, $bio);
if($insert->execute()){
header('Location: addressbook.php');
die();
}
}
}
}
if($results = $db->query("SELECT * FROM people")){
if($results->num_rows){
while($row = $results->fetch_object()){
$records[] = $row;
}
$results->free();
}
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div class = "container">
<?php
if(!count($records)){
echo 'No records found';
}
else{
?>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Bio</th>
<th>Created</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
foreach ($records as $r) {
?>
<tr>
<td><?php echo escape($r->id);?></td>
<td><?php echo escape($r->firstName); ?></td>
<td><?php echo escape($r->lastName); ?></td>
<td><?php echo escape($r->bio); ?></td>
<td><?php echo escape($r->created); ?></td>
<td>
<a onclick="return confirm('Do you want to delete the
record')" href="addressbook.php?idd=<?php echo $row['id'] ?>"
class="btn btn-
danger">Delete</a></td>
<?php
}
?>
</tr>
//My guess is the problem is with this code down here for deleting
<?php
if(isset($_POST['idd'])){
$idd = $_POST['idd'];
$results = $db->query("DELETE FROM people WHERE id=$idd");
if($results){
header('Location: addressbook.php');
}
}
?>
</tbody>
</table>
<?php
}
?>
you need to use $_GET because by default href tag sends the data with GET method.
your code should be
if(isset($_GET['idd'])){
$idd = $_GET['idd'];
$results = $db->query("DELETE FROM people WHERE id='$idd'");
if($results){
header('Location: addressbook.php');
}
}
NOTE- use prepared statement for avoiding sql injection attack
`
<?php
//database connectivity
$con=mysqli_connect("localhost","root","");
mysqli_select_db($con,"<db_name>");
$idd = $_REQUEST['idd'];
$sql= "DELETE FROM people WHERE id='$idd' ";
$result = mysqli_query($con,$sql) or die(mysql_error());
header("refresh:0.1; addressbook.php");
?>`
if(isset($_GET['idd'])){
$idd = $_GET['idd'];
$results = $db->query("DELETE FROM people WHERE id='{$idd}'");
Try adding a single quote.
If it still doesn't work, please see if the $_POST is actually posting correctly.
Try $results = $db->query("DELETE * FROM people WHERE id=$idd"); instead of $results = $db->query("DELETE FROM people WHERE id=$idd"); in the delete User Function :)
noob here. I am trying to create a table where I can put in separate buttons that link to my next .php page and to certain information from the next page tables. I have tried for hours and hours to get a style of table pdo that will allow me to add my own separate buttons with link to my next .php page.
But without adding one input href that creates a button for all of the rows.
Unless there is a way to add it to the single href section. But I have no idea. The links work but just point to the next page as a whole instead of choosing a table.
There are 3 tables on the next .php page, taken from mysql databases. Here is my index.php page. It is just basic for now until I can get the php code to work. I take it there is something to do with superglobals or sessions?? Thanks in advance.
<html>
<head>
<title>Blah</title>
<H1>Blah</H1>
<H2>Blah</H2>
<body>
<?php
include 'mystyles.css';
?>
<?php
$user = '123';
$pass = '456';
$db = new PDO( 'mysql:Myhost;dbname=123', $user, $pass );
$sql = "SELECT * FROM Production";
$query = $db->prepare( $sql );
$query->execute();
$results = $query->fetchAll( PDO::FETCH_ASSOC );
?>
<table class="table">
<tr>
<th>Title</th>
<th>Ticket Cost</th>
<th></th>
<?php
foreach( $results as $row ){
echo "<tr><td>";
echo $row['Title'];
echo "</td><td>";
echo $row['BasicTicketPrice'];
echo ('<td><a href="perf.php"><input type="submit" name="submit"
value="Register" class="register" /></a></td>');
echo "</tr>";
}
?>
</table>
</body>
</html>
One way is You need to pass the value to next page via GET ie in the URL
For eg:
....
This will result like ......
Once user clicks the link value will be passed to next page via GET, u can u use that value for computation
Sample
<html>
<head>
<title>Blah</title>
<H1>Blah</H1>
<H2>Blah</H2>
<body>
<?php
include 'mystyles.css';
?>
<?php
$user = '123';
$pass = '456';
$db = new PDO( 'mysql:Myhost;dbname=123', $user, $pass );
$sql = "SELECT * FROM Production";
$query = $db->prepare( $sql );
$query->execute();
$results = $query->fetchAll( PDO::FETCH_ASSOC );
?>
<table class="table">
<tr>
<th>Title</th>
<th>Ticket Cost</th>
<th></th>
<?php
foreach( $results as $row ){
echo "<tr><td>";
echo $row['Title'];
echo "</td><td>";
echo $row['BasicTicketPrice'];
echo ('<td><a href="perf.php?id=".$row['TicketId'].""><input type="submit" name="submit"
value="Register" class="register" /></a></td>');
echo "</tr>";
}
?>
</table>
</body>
</html>
I have an HTML table being displayed from php data and I'm trying to move data from one table to another based on what is clicked. I cannot get it to move from table to another but I"m not getting any code error.
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests
</center>
</div>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
// get results from database
$result = mysql_query("SELECT * FROM temp")
or die(mysql_error());
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['User_id'] . '</td>';
echo '<td>' . $row['First_name'] . '</td>';
echo '<td>' . $row['Last_name'] . '</td>';
echo '<td>' . $row['Username'] . '</td>';
echo '<td>Approve</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
</body>
</html>
PHP EDIT SCRIPT:
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests · All Users
</center>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
$id = $_GET['id'];
$sql = ("INSERT INTO users
SELECT * FROM temp WHERE User_id = $id ");
mysql_query($sql)
or die(mysql_error());
?>
</body>
</html>
I needed to use
$id = $_get['id];
and in the insert statement i had to replace id with User_id
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests · All Users
</center>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
$id = $_GET['id'];
$sql = ("INSERT INTO users
SELECT * FROM temp WHERE User_id = $id ");
mysql_query($sql)
or die(mysql_error());
?>
</body>
</html>