How to display database data to another page? - php

this is my code for the my table of rooms
<?php
include'db.php';
$sql=mysql_query("Select*from tb_rooms1");
echo "<div class='container'>";
echo "<div class='scrolltable'>";
echo "<table class= 'table table-striped table-bordered'>";
echo"<th><div align='center'>ID</div></th>";
echo"<th><div align='center'>Image</div></th>";
echo"<th><div align='center'>Room Name</div></th>";
echo"<th><div align='center'>Description</div></th>";
echo"<th><div align='center'>Price Per Night</div></th>";
echo"<th><div align='center'>Status</div></th>";
echo"<th><div align='center'>Reserve</div></th>";
while($row=mysql_fetch_array($sql))
{
#$id=$row['eid'];
echo"<tr align='center'>";
echo"<td><div style='font-size:11px;' align='center'>".$row['roomID']</div></td>";
echo"<td><div style='font-size:11px;' align='center'><img width=72 height=52 alt='Unable to View' src=".$row['image']."></div></td>";
echo"<td><div style='font-size:11px;' align='center'>".$row['name']."</div></td>";
echo"<td><div style='font-size:11px;' align='center'>".$row['description']."</div></td>";
echo"<td><div style='font-size:11px;' align='center'>".$row['price']."</div></td>";
echo"<td><div style='font-size:11px;' align='center'>".$row['status']."</div></td>";
echo"<td><div align='center' style='margin-top:30px'><input style='margin-top:-2px;' type='checkbox'></div></td>";
}
echo"</table>";
echo"</div>";
echo"</div>";
?>
I don't know how can I display the Information of the room I have selected to another page. Can someone help me? I'm

Make your room clickable and pass the room id through as a parameter
echo"<td><div style='font-size:11px;' align='center'><a href='otherpage.php?roomid={$row['roomID']}'> Room {$row['roomID']}</a></div></td>";
In your other php document you would have something like this:
otherpage.php
<?php
include'db.php';
$roomid = $_GET['roomid'];
$lookupQuery = mysqli_query("SELECT * FROM tb_rooms1 WHERE roomID = [$roomid]");
... Display the data however you want
One thing you will need to look into is mysql injection since you're code is using mysql and parameters are being passed straight into your query...

Related

Button not being called with isset() function

I'm having some trouble trying to build a very simple inventory management system.
What I'm doing is showing the data from a database in a html table and in each row a create two buttons: one to edit and one to delete the item. The problem is that I'm not being able to call these buttons with the isset() function and I can't understand why. I've tried to create a specific function for these but still doesn't work. Anybody has any idea?
Here is the code:
P.S.: Don't mind small english erros or a lack of of brackets. I had to change the code a little bit.
function searchTablet(){
if(isset($_POST['btnSearchTablet'])){
global $connection;
$query="SELECT * FROM tablet";
$run=mysqli_query($connection, $query);
echo "<table class='table table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Brand</th>";
echo "<th>Model</th>";
echo "<th>Color</th>";
echo "<th>Price</th>";
echo "<th>Fabrication Date</th>";
echo "<th>Provider</th>";
echo "<th>Registration Date</th>";
echo "<th>Edit</th>";
echo "<th>Delete</th>";
echo "</tr>";
echo "</thead>";
while($obj=mysqli_fetch_object($run)){
echo "<tr>";
echo "<td>$obj->id</td>";
echo "<td>$obj->idBrand</td>";
echo "<td>$obj->idModel</td>";
echo "<td>$obj->idColor</td>";
echo "<td>$obj->price</td>";
echo "<td>$obj->fabricationDate</td>";
echo "<td>$obj->idProvider</td>";
echo "<td>$obj->registrationDate</td>";
echo "<td><a href='resultTablet.php?btnEditTablet{$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a></td>";
echo "<td><a href='resultTablet.php?btnDeleteTablet{$obj->id}' class='btn btn-danger' name='btnDeleteTablet'>Excluir</a></td>";
echo "</tr>";
if(isset($_POST["btnDeleteTablet{$obj->id}"])){
$idTablet=$obj->id;
$delQuery="DELETE FROM tablet WHERE id='$idTablet'";
$delRun=mysqli_query($connection, $delQuery);
if($delRun){
echo "<div class='alert alert-success' role='alert'>Device was successfuly deleted.</div>";
}else{
echo "<div class='alert alert-danger' role='alert'>Error.</div>";
}
}
}
As always... looks like StackOverflow users are toxic and unfriendly and don't even try to help new people who want to get into programming.
So I will try to help you a little bit.
If you are passing parameters via URL you have to use $_GET not $_POST method.
Also, I would change
<a href='resultTablet.php?btnEditTablet{$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a>
to
<a href='resultTablet.php?btnEditTablet={$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a>
So you could do this:
if(isset($_GET["btnDeleteTablet"])){
and you would get id via $_GET like this:
$idTablet=$_GET["btnDeleteTablet"];
After this change, you can close while loop you used before
this if(isset($_GET["btnDeleteTablet"])) line, also you wont need $obj->id
anymore, because you will $_GET data decoupled from while loop or any other code you wrote before.
On another note, I see you forgot <tbody> </tbody> in your table.
EDIT:
Also, what are you doing with
if(isset($_POST['btnSearchTablet'])){
This wont work after delete button click.
You shouldn't use it like that, because after the delete button click your page will go to URL with $_GET parameters and that if logic will prevent
if(isset($_GET["btnDeleteTablet"])){
logic working.
So move whole
if(isset($_GET["btnDeleteTablet"])){
...
}
out of if(isset($_POST['btnSearchTablet'])){
Read up about $_POST and $_GET methods also, read about forms
you really need it.
Also, I recommend you to get program to profile requests so you could see how post and get data moves.
I recommend you to install Fiddler program, so you would be able to see how post, get data moves.
Ok, I will try to fix your code at last so it could work:
<?php
function searchTablet(){
global $connection;
if(isset($_GET['btnDeleteTablet'])){
deleteTablet();
}
//I dont why are you using it so I commented it out.
//if(isset($_POST['btnSearchTablet'])){
//}
displaySearchTablet();
}
function displaySearchTablet(){
global $connection;
$query = "SELECT * FROM tablet";
$run = mysqli_query($connection, $query);
while($obj = mysqli_fetch_object($run)){
//Combine all rows into one variable
$table_rows .= "
<tr>
<td>{$obj->id}</td>
<td>{$obj->idBrand}</td>
<td>{$obj->idModel}</td>
<td>{$obj->idColor}</td>
<td>{$obj->price}</td>
<td>{$obj->fabricationDate}</td>
<td>{$obj->idProvider}</td>
<td>{$obj->registrationDate}</td>
<td>
<a href='resultTablet.php?btnEditTablet={$obj->id}' class='btn btn-primary' name='btnEditTablet'>Alterar</a>
</td>
<td>
<a href='resultTablet.php?btnDeleteTablet={$obj->id}' class='btn btn-danger' name='btnDeleteTablet'>Excluir</a>
</td>
</tr>";
}
$result_table =
"<table class='table table-striped'>
<thead>
<tr>
<th>ID</th>
<th>Brand</th>
<th>Model</th>
<th>Color</th>
<th>Price</th>
<th>Fabrication Date</th>
<th>Provider</th>
<th>Registration Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
$table_rows
</tbody>
</table>";
echo $result_table;
}
function deleteTablet(){
global $connection;
$id = $_GET['btnDeleteTablet'];
$query = "DELETE FROM tablet WHERE id = '$id'";
$run = mysqli_query($connection, $query);
if($run){
echo "<div class='alert alert-success' role='alert'>Device was successfuly deleted.</div>";
}else{
echo "<div class='alert alert-danger' role='alert'>Error.</div>";
}
}
I kept it very basic, so you would be able to understand.
I didn't pass parameters or returned anything so it would be more understandable to you.
Good luck on your journey to programming.
isset() will return false if an empty string is being submitted, so try using !empty() instead.

I upload videos into into directory and i want them to display horizontall when i echo them

I have these codes and am not good at php please can someone help me on how to echo the videos to appear horizontall in my page thanks in advance
<?php
$h=2;
$k=mysql_query("SELECT * from aupload where type='audios' and view>='$h' order by view DESC");
while ($la=mysql_fetch_array($k)){
?>
<center>
<table width='100%'height=''>
<tr>
<?php echo"<td>" . "<a href='uploads/$la[filename]'><img src='uploads/$la[size]' width='180px' height='180px'>
<br><b>$la[filename]</b><br><a href='music1.php?id=".$la['id']."'><input type='button' value='DOWNLOAD'><input type='button' value='$la[view]'> </a></a>
"?></td>
</tr>
</table><br>
</center>
<?php }?>
<?php
include('connect.php');
$k=mysql_query("SELECT * from aupload where type='audios'");
#$la=mysql_fetch_array($k)
?>
<?php
$h=2;
$y=mysql_query("SELECT * from aupload where type='videos' and view>='$h' order by view DESC ");
?>
<table border='0' id='myTable'width='100%'>
<?php
while ($x=mysql_fetch_array($y)){
echo "<tr>";
echo "<td>" . "<video id='myVideo' onclick='message()' width='180px' height='180px' controls><source src='uploads/$x[filename]' '></video>" . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width='' height=''>"."<a href='music2.php?id=$x[id]' > <b>$x[filename]</b><br><input type='button' value='DOWNLOAD'></a>" ."<input type='button' value='$x[view]'></a></a>". "</td>";
echo "</tr>";
}
echo "</table>"; echo"</center>";
?>
the problem is when I upload two or more videos they appear vertically but I just want them to appear horizontally when echo them please someone help and thanks
Use frames is the best way to display a video, try this code
This is actually a HTML question, not a PHP question. In HTML, designates a table row, and can contain many or table cell elements. So you want to have the a echoed before the while loop, then print just the elements in the while loop, then print the . This will put all the elements in a single table row.

How can you update a query result performed by a query using php and mysql

I am trying to figure out how to update a "date" result from a query I perform.
Here is the code im using to perform the initial query:
<?php
include ("config.php");
$result = mysqli_query($con,"SELECT * FROM redmine.issues join redmine.projects ON issues.project_id=projects.id where projects.name='".$_SESSION['name']."';");
echo "<table border='0' width='80%' cellpadding='0' cellspacing='2' id='datatable' class='table table-bordered table-condensed table-hover table-striped'>
<tr>
<th width='50%' align='left'>Milestone</th>
<th width='25%' align='left'>Template Date</th>
<th width='15%' align='left'>Due Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='left'><input class='hidden' type='text' name='subject' id='subject' value='" . $row['subject'] . "'>" . $row['subject'] . "</td>";
echo "<td align='left'>" . $row['created_on'] . "</td>";
echo "<td align='left'><input type='text' name='due_date' id='due_date' value='".$row['due_date']."'></td>";
echo "<td align='left'><input type='submit' value='Submit New Date'></td>";
echo "</tr>";
}
echo "</table>";
echo "<br />";
mysqli_close($con);
?>
This will give me this result:
----------------------------------------
Milestone | Created Date | Due Date
----------------------------------------
Milestone 1 | 01/22/2015 | 01/22/2015
----------------------------------------
I want to be able to then type in a new date in the "Due Date" textbox for each milestone and update the due date in my mysql table for that milestone.
Any help is appreciated!
I now have this script.. but it only seems to update the last mysql query result. for example, if I have 5 milestones retrieved, I can only update the last mile stone date. If I try to update the other 4 milestone dates, they wont update.
<?php
include ("config.php");
// Get values from form
$subject=$_POST['subject'];
$due_date=$_POST['due_date'];
$result = mysqli_query($con,"UPDATE redmine.issues join redmine.projects ON issues.project_id=projects.id SET issues.due_date='$due_date' WHERE issues.subject='$subject' AND projects.name='".$_SESSION['name']."'");
// close connection
mysqli_close($con);
?>
Create a input[type="text"] field like this:
<input type="text" value="something" id="DueDate" data-id="12" onchange="function_update()"/>
In function_update() get current id by data-id="12" and value by id="DueDate" using ajax for onchange event.

How to echo html and row from database

I have a script written to grab a row from my database based on current session user, which outputs the row correctly, however I want to insert a small image to be displayed alongside of the echo'd row, and cannot figure out the proper syntax.
if ($row['lifetime']!="")
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div> ".$row['lifetime'];
else
echo '';
?>
basically I want the image to appear right before or after the .$row appears, either or.
You can try:
<?php
if ($row['lifetime'] !== "") {
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div>";
echo $row['lifetime'];
echo "<img src='' alt='' style='width:100px'/>";
}
?>
Just put the HTML for the image into the string you're echoing:
echo "<div style ='font:12px Arial;color:#2F6054'><img src="fill in URL here"> Lifetime Member: </div> ".$row['lifetime'];
You can try as below example
HTML
<table>
<thead>
<tr>
<th>No.</th>
<th>Customer Name</th>
<th>Photo</th>
<th ></th>
</tr>
</thead>
<tbody>
<?php
$tst=0;
$result = mysql_query("select * from acc_cust");
while($row = mysql_fetch_array($result))
{
echo "<tr class='odd gradeX'>";
echo "<td width=5%'>" . $row['ent_no']. "</td>";
echo "<td>" . $row['cust_name']. "</td>";
echo "<td><img src='[path]" . $row['cust_img'] . "' /></td>";
}
?>
</tbody>
</table>

Not updating in mysql , php

Ok, So I am creating a attendance system and I want to mark a student present or absent, this is my code
<?php
if (isset($_POST['submit'])) {
$present = $_POST['present'];
}
$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3) or die(mysql_error());
echo "</br><table border='1' align='center'><tr> <th><strong>Student ID</strong></th> <th><strong>First Name </strong></th> <th><strong>Last Name</strong></th> <th><strong>Present</strong></th> </tr> ";
while($rows=mysql_fetch_array($result)){
echo"<form name='Biology_lecture11.php' method='post'>";
echo "<tr><td width='100' align='center'>" .$rows['student_id'].
"</td><td width='120' align='center'>" .$rows['fname'].
"</td><td width='120' align='center'>" .$rows['lname'].
"</td><td><input type='text' name='present' value=" .$rows['present'] . ">";
}
echo "</table>";
?>
<input type='submit' name='Submit' value='Submit' >
</form>
<?php
$sql="UPDATE course_attendance SET present='$present' WHERE course_id='101' AND week_id='2' ";
$result=mysql_query($sql);
if($result){
echo "Successfully logged the attendance";
}
else {
echo"ERROR";
}
?>
The problem is , that it does not update the present field in the database, anyone know whats wrong
This should work for you. This will assign each student a unique present value, which is then checked on postback and if set, it is cleaned and used to update the student record in attendance.
I also extracted echo'd HTML in the PHP to HTML, and moved your form outside of your table (it can cause issues in some browsers).
<?php
// Update present values
if (isset($_POST['submit']))
{
// Get a list of student ids to check
$idsResult = mysql_query("SELECT student_id from students");
while($idRow = mysql_fetch_array($idsResult))
{
// if the textbox for this student is set
if(isset($_POST['present'.$idRow['student_id']]) && !empty($_POST['present'.$idRow['student_id']]))
{
// Clean the user input, then escape and update the database
$cleanedPresent = htmlspecialchars(strip_tags($_POST['present'.$idRow['student_id']]));
$sql = "UPDATE course_attendance SET present='".mysql_real_escape_string($present)."' WHERE course_id='101' AND week_id='2' AND student_id=".$idRow['student_id'];
$result = mysql_query($sql);
if($result){
echo "Successfully logged the attendance for ID ".$idRow['student_id'];
}
else {
echo "ERROR updating on ID ".$idRow['student_id'];
}
}
}
}
$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3) or die(mysql_error());
?>
<form name='Biology_lecture11.php' method='post'>
</br>
<table border='1' align='center'>
<tr>
<th><strong>Student ID</strong></th>
<th><strong>First Name </strong></th>
<th><strong>Last Name</strong></th>
<th><strong>Present</strong></th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
echo "<tr><td width='100' align='center'>" .$rows['student_id'].
"</td><td width='120' align='center'>" .$rows['fname'].
"</td><td width='120' align='center'>" .$rows['lname'].
"</td><td><input type='text' name='present".$rows['student_id']."' value=" .$rows['present'] . ">";
}
?>
</table>
<input type='submit' name='Submit' value='Submit'>
</form>
Alternative (better) method: If the present value can be set to a simple 0/1 or true/false, then it would be easier to use a checkbox for each student. In postback, you can then retrieve an array of values from checking each checkbox indicating students who are present, and then update the database table in one query. That also prevents from malicious text input.
Alternative code:
<?php
// Update present values
if (isset($_POST['submit']))
{
// Get a list of student ids to check
$idsResult = mysql_query("SELECT student_id from students");
$presentIds = array();
$absentIds = array();
while($idRow = mysql_fetch_array($idsResult))
{
// If the student's checkbox is checked, add it to the presentIds array.
if(isset($_POST['present'.$idRow['student_id']]))
{
$presentIds[] = $idRow['student_id'];
}
else
{
$absentIds[] = $idRow['student_id'];
}
}
// Convert array to string for query
$idsAsString = implode(",", $presentIds);
// You can set present to whatever you want. I used 1.
$sql = "UPDATE course_attendance SET present='1' WHERE course_id='101' AND week_id='2' AND student_id IN (".$idsAsString.")";
$result = mysql_query($sql);
if($result){
echo "Successfully logged the attendance for IDs ".$idsAsString;
}
else {
echo "ERROR updating on IDs ".$idsAsString;
}
// OPTIONAL: Mark absent students as '0' or whatever other value you want
$absentIdsAsString = implode(",", $absentIds);
// You can set present to whatever you want. I used 1.
$absentQuery = "UPDATE course_attendance SET present='0' WHERE course_id='101' AND week_id='2' AND student_id IN (".$absentIdsAsString.")";
$absentResult = mysql_query($absentQuery);
if($absentResult){
echo "Successfully logged absence for IDs ".$absentIdsAsString;
}
else {
echo "ERROR updating absence on IDs ".$absentIdsAsString;
}
}
$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3) or die(mysql_error());
?>
<form name='Biology_lecture11.php' method='post'>
</br>
<table border='1' align='center'>
<tr>
<th><strong>Student ID</strong></th>
<th><strong>First Name </strong></th>
<th><strong>Last Name</strong></th>
<th><strong>Present</strong></th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
echo "<tr><td width='100' align='center'>" .$rows['student_id'].
"</td><td width='120' align='center'>" .$rows['fname'].
"</td><td width='120' align='center'>" .$rows['lname'].
"</td><td><input type='checkbox' name='present".$rows['student_id']."' ";
// NOTE: REPLACE 1 with whatever value you store in the database for being present.
// I used 1 since the update at the top of the code uses 0 and 1.
if($rows['present']=='1')
{
echo "checked='checked' ";
}
// With a checkbox, you don't need to assign it a value.
echo "value=" .$rows['present'];
echo ">";
}
?>
</table>
<input type='submit' name='Submit' value='Submit'>
</form>
One mistake I see is, that you put this:
echo"<form name='Biology_lecture11.php' method='post'>";
in your while-loop. So it is put out more than one time. Try writing that part in the row before your loop.
A couple of issues I see:
1: You're UPDATE code is running every time the page is loaded. Move you update block into the if (isset($_POST['submit'])) {} block.
2: When you print out the students, you create an input called "present" for every student. If you were to fill this in and submit the data, only the last field will be added to the database.
3: You're not updating a specific student. I would change the input field to a checkbox and name it "present[$rows[student_id]]".
Then, once the page is being processed, loop through the key/values of $_POST['present']. and update any students that are in it.
foreach (array_keys($_POST['present']) as $student_id) {
if (is_numeric($student_id)) {
$sql="UPDATE course_attendance SET present='true' WHERE course_id='101' AND week_id='2' and student_id='$student_id'";
}
}
You'll have to modify the UPDATE if the attendance table isn't automatically filled in with students. If every student isn't already there, you'll have to run a query to see if they exist. If they don't insert the row. If they do, update the row.
4: Move the opening tag to before the opening of the table and OUTSIDE of the student loop.
Two things to take in consideration: First, you have form element dupplication. As the comments above said, take out the line
echo"<form name='Biology_lecture11.php' method='post'>";
from the loop.
Second, the UPDATE statatement updates all the students, you need a WHERE token in your SQL statement. Something like this:
<?php
if (isset($_POST['submit'])) {
$present = $_POST['present'];
}
$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3) or die(mysql_error());
echo "</br><table border='1' align='center'><tr> <th><strong>Student ID</strong></th> <th><strong>First Name </strong></th> <th><strong>Last Name</strong></th> <th><strong>Present</strong></th> </tr> ";
echo"<form name='Biology_lecture11.php' method='post'>";
while($rows=mysql_fetch_array($result)){
echo "<tr><td width='100' align='center'>" .$rows['student_id'].
"</td><td width='120' align='center'>" .$rows['fname'].
"</td><td width='120' align='center'>" .$rows['lname'].
"</td><td><input type='text' name='present' value=" .$rows['present'] . ">";
}
echo "</table>";
?>
<input type='submit' name='Submit' value='Submit' >
</form>
<?php
$sql="UPDATE course_attendance SET present='$present' WHERE course_id='101' AND week_id='2' AND student_id = the_student_id";
$result=mysql_query($sql);
if($result){
echo "Successfully logged the attendance";
}
else {
echo"ERROR";
}
?>
Hope it helps!
you have taken a form inside table tag and inside while loop this will not work, here is correct code.
<?php
if (isset($_POST['submit'])) {
$present = $_POST['present'];
$sql="UPDATE course_attendance SET present='$present' WHERE course_id='101' AND week_id='2' ";
$result=mysql_query($sql);
if($result) {
echo "Successfully logged the attendance";
}
else {
echo"ERROR";
}
}
?>
<form name='Biology_lecture11.php' method='post'>
<table border="1" align="center">
<tr>
<th><strong>Student ID</strong></th>
<th><strong>First Name </strong></th>
<th><strong>Last Name</strong></th>
<th><strong>Present</strong></th>
</tr>
<?php
$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3) or die(mysql_error());
while($rows=mysql_fetch_array($result)) {
echo "<tr><td width='100' align='center'>" .$rows['student_id']."</td>
<td width='120' align='center'>" .$rows['fname']."</td>
<td width='120' align='center'>" .$rows['lname']."</td>
<td><input type='text' name='present' value=" .$rows['present']."></td></tr>";
}
echo "</table>";
?>
<input type='submit' name='Submit' value='Submit' >
</form>

Categories