I am making a page where it displays a table with staffID and staffName using PHP. When the user clicks the staffID it should then display a new table using the code from another file with extra details about that staffID such as shippingDate, OrderID, etc.
task9.php file below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Task 9</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<?php
$sID= isset($_GET['staffID']) ? $_GET['staffID'] : '';
$conn = mysqli_connect('localhost', 'TWA', 'TWA_test', 'factory');
if ( !$conn ) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT s.staffID,s.staffName
FROM staff s";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));
?>
<h1> Staff Table </h1>
<table>
<tr>
<th>Staff ID</th>
<th>Staff Name</th>
</tr>
<?php while($row = mysqli_fetch_array($results)) { ?>
<tr>
<td> <?php echo $row[0] ?></td>
<td><?php echo $row[1] ?></td>
</tr>
<?php } ?>
<?php mysqli_close($conn); ?>
</table>
</body>
</html>
task8.php asks a user to enter a staffID and it displays all of the details, if the staffID does not exist then it displays an error. This file works fine and displays everything correctly. task8.php file below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Task 8</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<?php
$conn = mysqli_connect('localhost', 'TWA', 'TWA_test', 'factory');
if ( !$conn ) {
die("Connection failed: " . mysqli_connect_error());
}
//obtain the staff ID input from the $_GET array
$sID= isset($_GET['staffID']) ? $_GET['staffID'] : '';
$sql = "SELECT s.staffID, p.orderID, p.orderDate, p.shippingDate,s.staffName
FROM purchase p
INNER JOIN staff s
ON p.staffID = s.staffID
WHERE p.staffID = '$sID'";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));
?>
<h1> Order Details </h1>
<?php $rows = mysqli_num_rows($results); ?>
<?php if($rows <= 0){ ?>
<p><?php echo "The staff ID entered is invalid"; ?></p>
<?php } else { ?>
<table>
<tr>
<th>Staff ID</th>
<th>Order ID</th>
<th>Order Date</th>
<th>Shipping Date</th>
<th>Staff Name</th>
</tr>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<?php if($row[0] != ""): ?>
<td><?php echo $row[0]; ?></td>
<?php else: ?>
<td><?php echo "N/A"; ?></td>
<?php endif; ?>
<?php if($row[1] != ""): ?>
<td><?php echo $row[1]; ?></td>
<?php else: ?>
<td><?php echo "N/A"; ?></td>
<?php endif; ?>
<?php if($row[2] != ""): ?>
<td><?php echo $row[2]; ?></td>
<?php else: ?>
<td><?php echo "N/A"; ?></td>
<?php endif; ?>
<?php if($row[3] != ""): ?>
<td><?php echo $row[3]; ?></td>
<?php else: ?>
<td><?php echo "N/A"; ?></td>
<?php endif; ?>
<?php if($row[4] != ""): ?>
<td><?php echo $row[4]; ?></td>
<?php else: ?>
<td><?php echo "N/A"; ?></td>
<?php endif; ?>
</tr>
<?php } ?>
<?php } ?>
<?php mysqli_close($conn); ?>
</table>
</body>
</html>
The problem I am facing is that when I click the staffID from the table on task9.php it just shows the error I included in task8.php "The staff ID entered is invalid". I don't know why it isn't displaying the details from task8.php
you did not build your link correctly, it should be
<?php echo $row[0] ?>
also, i feel somehow uncomfortable echoing outputs without htmlentities().
Yes, it is boring to type that everytime so i usually keep a function in every of my PHP projects like this
function e($whatToConvert){
return htmlentities($whatToConvert); //or htmlspecialchars
}
so the above code for instance becomes
<?php echo e($row[0]) ?>
You need to pass the parameter like this
<a href = "task8.php?staffID=<?php echo $row[0]?>">
Related
I have a table named forums with 4 fields: id_forum, title, theme, fk_user.
I have an overview over some elements below:
I would like to click on the element of the theme and recuperate only the pseudo on another page. Is it possible?
Here is my code PHP for the overview
<?php
$bdd = new PDO('mysql:host=localhost;charset=utf8;dbname=exo', 'root', '');
$requestSQL = "SELECT forums.*, users.pseudo
FROM forums INNER JOIN users
ON forums.fk_user=users.id_user
ORDER BY theme ASC";
$stm = $bdd->prepare($requestSQL);
$stm->execute();
?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Forum</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div class="title"><h1>List forum</h1></div>
</div>
<br /><br /><br /><br />
<table id="tab">
<tr>
<th>Id</th>
<th>Title</th>
<th>Theme</th>
<th>Pseudo</th>
</tr>
<?php
while($row = $stm->fetch()){ ?>
<tr>
<td><?php echo $row ['id_forum'];?></td>
<td><?php echo $row ['title'];?></td>
<td><?php echo $row ['theme'];?></td>
<td><?php echo $row['pseudo'];?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
How to add <a href> on my line <td><?php echo $row ['theme'];?></td> ?
I have tried this
<?php
while($row = $stm->fetch()){ ?>
<tr>
<td><?php echo $row ['id_forum'];?></td>
<td>?php echo $row ['title'];?></td>
<td><?php echo $row ['theme'];?></td>
<td><?php echo $row['pseudo'];?></td>
</tr>
<?php
}
?>
My overview is catastrophic...
when i clicked the link it does not redirect to the page that specified.i have the sqlite database and there is a database column for status(added,updated like wise).i want to redirect to the page when i clicked the status link in my link.php.Now Object not found error is getting.
this is my link.php code below.
Please can you help me guys.
<?php
// Includs database connection
include "db_connect.php";
// Makes query with rowid
$query = "SELECT rowid, * FROM registration";
// Run the query and set query result in $result
// Here $db comes from "db_connection.php"
$result = $db->query($query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Data List</title>
<script>
function pop_up(url){
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=1076,height=768,directories=no,location=no')
}
</script>
</head>
<body>
<br>
<br>
<!-- Add New-->
<table>
<tbody>
<tr>
<th style="">status</th>
<th style="">submitted</th>
<th style="">department</th>
<th style="">head</th>
<th style="">title</th>
<th style="">applicant</th>
<th style="">date</th>
</tr>
<?php while($row = $result->fetchArray()) {?>
<!--<tr class="table-row" data-href="update.php?id=<?php echo $row['rowid'];?>" data-target="_blank">-->
<tr>
<td>
</td>
<td>view
<?php
if ($row['rowid'] == "added"){
echo "<a href='updated.php" . $row['submitted'] . "updated'> </a>";
} else {
echo "<a href='next.php" . $row['submitted'] . "next'> </a>";
}
?>
</td>
<td><?php echo $row['status'];?></td>
<td><?php echo $row['submitted'];?></td>
<td><?php echo $row['department'];?></td>
<td><?php echo $row['head'];?></td>
<td><?php echo $row['title'];?></td>
<td><?php echo $row['applicant'];?></td>
<td><?php echo $row['date'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
<script>
var tableRows = document.getElementsByClassName('table-row');
for (var i = 0, ln = tableRows.length; i < ln; i++) {
tableRows[i].addEventListener('click', function() {
window.open(this.getAttribute('data-href'), this.getAttribute('data-target'));
});
}
</script>
<br>
<br>
</body>
</html>
here's my code i don't know where i do wrong here
<?php
$conn=oci_connect("martin","123","localhost/XE");
If (!$conn)
echo 'Failed to connect to Oracle';
else
echo 'Succesfully connected with Oracle DB';?>
<table border="1">
<?php $stid = oci_parse($conn, "select*from MsNama");
oci_execute($stid);
while ($row =
oci_fetch_array(
$stid,
OCI_ASSOC+OCI_RETURN_NULLS)
) {?>
<tr>
<td><?php echo $row[0]; ?></td>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
</tr>
<?php }?>
and here's my select on oracle database:
SQL> select * from MsNama
2 ;
NAMA ANGKA G
---------------------- -------- -
martin 1 m
this is what's it's look like
I changed the code with what i found on internet but still not working
<html>
<head><title>Oracle demo</title>
</head>
<body>
<?php
$conn=oci_connect("martin","123","localhost/XE");
If (!$conn)
echo 'Failed to connect to Oracle';
else
echo 'Succesfully connected with Oracle DB';
?>
<table border="1">
<?php $stid = oci_parse($conn, "select * from MsNama");
oci_execute($stid,OCI_DEFAULT);
while (oci_fetch($stid)) {
$empno = oci_result($stid, "NAMA");
$ename = oci_result($stid, "angka");
$job = oci_result($stid, "gender");
?>
<tr>
<td><?php echo $empno; ?></td>
<td><?php echo $ename;?></td>
<td><?php echo $job; ?></td>
</tr>
<?php }?>
</table>
</body>
</html>
i tried and always no result. nothing showing
oh my code not wrong but here's the code and it worked
<html>
<head><title>Oracle demo</title></head>
<body>
<?php
$conn=oci_connect("martin","123","localhost/XE");
If (!$conn)
echo 'Failed to connect to Oracle';
else
echo 'Succesfully connected with Oracle DB';
?>
<table border="1">
<?php $stid = oci_parse($conn, "select*from MsNama");
$r=oci_execute($stid);
while ($row = oci_fetch_array($stid, OCI_BOTH)) {?>
<tr>
<td><?php echo $row[0]; ?></td>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
</tr>
<?php }?>
</table>
</body>
</html>
the problem that i forgot to commit so .-.there's no result i'm sorry
I have a shopping cart in which the orders are being shown, i am using session to store the cart contents. Now what i want to do is to insert the cart contents into a database upon the press checkout button. But everytime any user checks out only the word "Array" is being inserted into the DB. What i have tried -
$sqlimp = implode(",", $_SESSION["cart"] );
and the n print_r the $sqlimp and it shows Array,Array,ArayArray,Array,Array (if there is 2 items). Below is my code -
index.php
<?php
session_start();
// print_r($_SESSION["user"]);
if(! isset($_SESSION["user"])){
header("Location: index.php");
}
require("connection.php");
if(isset($_GET['page'])){
$pages=array("products", "cart");
if(in_array($_GET['page'], $pages)) {
$_page=$_GET['page'];
}else{
$_page="products";
}
}else{
$_page="products";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- <link rel="stylesheet" href="css/reset.css" /> -->
<link rel="stylesheet" href="styles.css" />
<title></title>
</head>
<body>
<h1> Welcome to our site! </h1>
Logout
<?php
// Echo session variables that were set on previous page
echo "Welcome " . $_SESSION["user"] . ".<br>";
?>
<div id="container">
<div id="main">
<?php require($_page.".php"); ?>
</div><!--end of main-->
<div id="sidebar">
<h1>Cart</h1>
<?php
if(isset($_SESSION['cart'])){
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
// $sql1= "INSERT INTO cart (contents) VALUES ('" . $_SESSION["cart"]. "')";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
// $query1= mysql_query($sql1);
while($row=mysql_fetch_array($query)){
?>
<p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p>
<?php
}
?>
<hr />
Go to cart
<?php
}else{
echo "<p>Your Cart is empty. Please add some products.</p>";
}
?>
</div><!--end of sidebar-->
</div><!--end container-->
</body>
</html>
products.php
<?php
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
}else{
$sql_s="SELECT * FROM products
WHERE id_product={$id}";
$query_s=mysql_query($sql_s);
if(mysql_num_rows($query_s)!=0){
$row_s=mysql_fetch_array($query_s);
$_SESSION['cart'][$row_s['id_product']]=array(
"quantity" => 1,
"price" => $row_s['price']
);
}else{
$message="This product id it's invalid!";
}
}
}
?>
<h1>Product List</h1>
<?php
if(isset($message)){
echo "<h2>$message</h2>";
}
?>
<table>
<tr>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php
$sql="SELECT * FROM products ORDER BY name ASC";
$query=mysql_query($sql);
while ($row=mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['price'] ?>$</td>
<td>Add to cart</td>
</tr>
<?php
}
?>
</table>
cart.php
<?php
if(isset($_POST['submit'])){
foreach($_POST['quantity'] as $key => $val) {
if($val==0) {
unset($_SESSION['cart'][$key]);
}else{
$_SESSION['cart'][$key]['quantity']=$val;
}
}
}
?>
<h1>View cart</h1>
Go back to the products page.
<form method="post" action="home.php?page=cart">
<table>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Items Price</th>
</tr>
<?php
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while($row=mysql_fetch_array($query)){
$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
$totalprice+=$subtotal;
?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>" /></td>
<td><?php echo $row['price'] ?>$</td>
<td><?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?>$</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4">Total Price: <?php echo $totalprice ?></td>
</tr>
</table>
<br />
<button type="submit" name="submit">Update Cart</button>
Checkout
</form>
<br />
<p>To remove an item set its quantity to 0. </p>
checkout.php
<?php
session_start();
include("connection.php");
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
$sqlimp = implode(",",$_SESSION['cart'] );
print_r($sqlimp);
$sql1= "INSERT INTO cart (contents) VALUES ('" . $_SESSION["cart"]. "')";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$query1= mysql_query($sql1);
// or die("Query to store cart failed");
?>
any help would be appreciated.
In your foreach loop in checkout.php you are trying to INSERT the $_SESSION['cart'] instead of your computed $sqlimp. So you need to change the line to:
$sql1= "INSERT INTO cart (contents) VALUES ('$sqlimp')";
I am new is PHP, doing my final year project in Student Result Online System.
In my system, I have an error in session, Once the user/student logs in using their user name and password, the dashboard displays his details properly.
But, as soon as the page reloads or you click to view other contents or just simply click on the student's tool bar within dashboard, the page loses all of its relevant contents and becomes idle or blank.
I want to prevent auto session destroy or loss while the user remains on the same page until he/she logs-out. any help would be grateful. Interesting thing is that, this codes works really well in Localhost (Wamp Server), but it loses its session in Online (cpanel).
My codes pages are:
login.php
<?php
include('dbcon.php');
if (isset($_POST['login'])){
session_start();
$student_no = $_POST['student_no'];
$password = $_POST['password'];
$query = "SELECT * FROM students WHERE student_no='$student_no' AND password='$password' and status = 'active' ";
$result = mysql_query($query)or die(mysql_error());
$num_row = mysql_num_rows($result);
$row=mysql_fetch_array($result);
if( $num_row > 0 ) {
header('location:dasboard.php');
$_SESSION['id']=$row['student_id'];
}
elae{
header('location:access_denied.php');
}
}
?>
session.php
<?php
session_start();
if (!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
header("location: index.php");
exit();
}
$session_id=$_SESSION['id'];
?>
dasboard.php
<?php include('session.php'); ?>
<?php include('header.php'); ?>
<?php include('navbar.php'); ?>
<?php
$query=mysql_query("select * from students where student_id='$session_id'")or die(mysql_error());
$row=mysql_fetch_array($query);
$year_level = $row['year_level'];
$term = $row['term'];
$status = $row['student_status'];
$school_year = $row['year_level'];?>
<div class="container">
<div class="margin-top">
<div class="row">
<?php include('head.php'); ?>
<div class="span12">
<div class="grade">
<?php include('grade_option.php'); ?>
</div>
</div>
<div class="span2">
<?php include('user_sidebar.php'); ?>
</div>
<div class="span10">
<table cellpadding="0" cellspacing="0" border="0" class="table table-bordered" id="example">
<thead>
<tr>
<th width="100">Code</th>
<th width="300">Subject</th>
<th width="50">Units</th>
<th>Gen Ave.</th>
<th>Term</th>
<th>Year Level</th>
<th>Remarks</th>
</tr>
</thead>
<tbody>
<?php $user_query=mysql_query("select * from grade where student_id = '$session_id' and school_year = '$year_level'
and semester = '$term'
")or die(mysql_error());
while($row=mysql_fetch_array($user_query)){
$id=$row['grade_id'];
$remarks = $row['remarks'];
$subject_id = $row['subject_id'];
$subject_query = mysql_query("select * from subject where subject_id = '$subject_id'")or die(mysql_error());
while($subject_row=mysql_fetch_array($subject_query)){
?>
<tr>
<td>
<?php echo $subject_row['code']; ?></td>
<td><?php echo $subject_row['title']; ?></td>
<td><?php echo $subject_row['unit']; ?></td>
<td><?php echo $row['gen_ave']; ?></td>
<td><?php echo $row['semester']; ?></td>
<td><?php echo $row['school_year']; ?></td>
<?php if ($remarks == 'Very Good'){ ?>
<td><span class="very_good"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Excellent'){ ?>
<td><span class="Excellent"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Satisfactory'){ ?>
<td><span class="sat"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Fair'){ ?>
<td><span class="fair"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Failed'){ ?>
<td><span class="failed"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Incomplete'){ ?>
<td><span class="failed"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Officially Dropped'){ ?>
<td><span class="drop"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'PASS'){ ?>
<td><span class="Excellent"><?php echo $row['remarks']; ?></span></td>
<?php }else if ($remarks == ''){ ?>
<td><?php echo $row['remarks']; ?></td>
<?php } ?>
</tr>
<?php }} ?>
</tbody>
</table>
</div>
<?php include('units_table.php'); ?>
<?php include('gwa_table.php'); ?>
<?php include('cwa_table.php'); ?>
</div>
<?php include('grading_system.php') ?>
</div>
</div>
</div>
<?php include('footer.php') ?>
In your following code:
if( $num_row > 0 ) {
header('location:dasboard.php');
$_SESSION['id']=$row['student_id'];
}
You do
header('location:dasboard.php');
Before you set the session:
$_SESSION['id']=$row['student_id'];
Meaning you redirect first, and session is NEVER set.
By the way header.php, includes header.php?
And
elae{
header('location:access_denied.php');
}
Should be else of course.