<?php
$sqlget = "SELECT * FROM news_events,news_events_images WHERE news_events.id = news_events_images.newsId";
$sqldata = mysql_query($sqlget) or die('error getting data');
echo " <table class=\"table table-striped table-hover\" style=\"width:100%;\"> <thead><th colspan=\"2\"> <h3 style= \"font-family:Georgia;font-size:22px;text-transform:capitalize; text-align:center; color:#ec1e2e; \">News and events</h3> </th>";
echo"</thead><tbody>";
while ($row = mysql_fetch_array($sqldata, MYSQLI_ASSOC)) {
echo "<tr><td colspan=\"2\" style=\"width:100%;text-align:center;font-size:16px;font-weight:bold;padding:3em 0em;\">";
echo $row['Title'];
echo "</td></tr><tr><td style=\"width:30%;\">";
echo "<img src=\"Admin/images/newsImages/";
echo $row['image'];
echo "\"";
echo" class=\"image-responsive\" style=\"display:block; width:100%; height:auto;\">";
echo "</td><td style=\"width:70%;text-align:justify;\">";
echo $row['description'];
echo "</td>
</tr>";
}
echo "</tbody></table>";
?>
This code is displaying all the images row by row, but i want only one image should be displayed, In database i have 4 images under same id, from that i want to display one of the image
if you want to show only 1 image using same id then you group by
$sqlget = "SELECT * FROM news_events,news_events_images WHERE news_events.id = news_events_images.newsId group by news_events.id";
for more information group by
https://www.w3schools.com/sql/sql_groupby.asp
I have an issue with the below code, it is showing the html part but not the data from my database. I am just trying to have the data from the database in the drop down list and when I click find to display it on the table below. Can you please help?
<?php
include 'config.php';
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<select name="manufacturer">
<option value="">----ALL----</option>
<?php
$sql = "SELECT id, manufacturer FROM coop";
$result = $conn->query($sql);
while ($r = mysqli_fetch_assoc($result))
{
echo "<option value='$r[id]'> $r[manufacturer]</option>";
}
?>
</select>
<input type="submit" name="find" value="find"/>
<br><br>
<table border="1">
<tr align="center">
<th>ID</th> <th>Manufacturer</th>
</tr>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$des=$_POST["manufacturer"];
if($des=="")
{
$result= mysqli_query("SELECT id, manufacturer FROM coop");
}
else
{
$result= mysqli_query($sql);
}
echo "<tr><td colspan='5'></td></tr>";
while($r= mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td align='center'>$r[0]</td>";
echo "<td width='200'>$r[1]" . " $r[2]</td>";
echo "<td alig='center' width='40'> $r[3]</td>";
echo "<td align='center' width='200'>$r[4]</td>";
echo "<td width='100' align='center'>$r[5]</td>";
echo "</tr>";
}
}
?>
</table>
</body>
</html>
your queries are wrong here
<?php
$sql = "SELECT id, manufacturer FROM coop";
$result = $conn->query($sql);
while ($r = mysqli_fetch_assoc($sql))
{
echo "<option value='$r[0]'> $r[0]</option>";
}
?>
this while ($r = mysqli_fetch_assoc($sql)) should be while ($r = mysqli_fetch_assoc($result ))
you should fetch your assoc with $result not with$sql
still you have some error bellow part
I have the following code:
$sql = "SELECT * FROM Tickets WHERE stat='Open'";
$result = mysql_query($sql);
mysql_close($con);
?>
<!DOCTYPE>
<html>
<body>
<table class="striped">
<tr class="header">
<td>Username</td>
<td>Title</td>
<td>Description</td>
<td>Admin Name</td>
<td>Category</td>
<td>Status</td>
<td>Urgency</td>
<td>Time In</td>
<td> </td>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row[username]."</td>";
echo "<td>".$row[title]."</td>";
echo "<td>".$row[description]."</td>";?>
<td><select>
<?php
echo "<option value'".$row[admin_name]."'>".$row[admin_name]."</option>";
$sql = mysql_query("SELECT username FROM Users WHERE user_type='admin'");
while ($u = mysql_fetch_array($sql)){
echo "<option value='".$u['username']."'>".$u['username']."</option>";
}
?>
</select></td>
<?php
echo "<td>".$row[category]."</td>";
echo "<td>".$row[stat]."</td>";
echo "<td>".$row[urgency]."</td>";
echo "<td>".$row[time_in]."</td>";
echo "<td><a href='close.php'>Close Ticket</a></td>";
echo "</tr>";
}
?>
</table>
<a href='update.php'>Update</a>
</body>
</html>
I have two links on this page. Both of them need to update a SQL database. The Close ticket link needs to just update the single row, while the update link should update all of them. I am not sure how to get the info from one php to the next. It seems like you can put the individual row information into a Post array for the close ticket link, but I am not sure how. For the update link it needs to take the value of the dropdown in the table and change the admin_name field to that value.
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>
<?php
require 'database.php';
$query = "SELECT id, date, ponumber FROM so";
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if ($result) {
echo "<form method='post' action='delete.php'>";
echo "<table cellspacing='0' cellpadding='15' border='1'>
<th >DELETE</th>
<th >VIEW</th>
<th >ID</th>
<th >DATE</th>
<th >PO NUMBER</th>";
while ($row = $result->fetch_object()) {
$date = $row->date ;
$ponumber = $row->ponumber;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
<td>
<input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id />
</td>
<td>
$id
</td>
<td>
view
</td>
<td>
$date
</td>
<td>
$ponumber
</td>
</tr>";
}
echo "</table><p><input id='delete' type='submit' class='button' name='delete'
value='Delete Selected Items'/></p></form>";}
?>
i have a sort of an online order form which enable the sales rep to input sales order,
i have done the insert and delete using the code above now i want every row to be a hyperlink so that when they click view it will display only row that has been clicked, in my code above if you click :view" all the detail will display, how can i display only the row that i will click will display the detail of the record!
you need to pass the id in the url and you need to read it if it's there.
e.g.
<?php
require 'database.php';
$query = "SELECT id, date, ponumber FROM so";
/* Edit 1 */
if (!empty($_GET['id'])) {
$query .= " WHERE id = " . mysql_real_escape_string($_GET['id']);
}
/* Edit 1 end */
$result = $mysqli->query($query) or die(mysqli_error($mysqli));
if($result) {
echo "<form method='post' action='delete.php'>";
echo "<table cellspacing='0' cellpadding='15' border='1'>
<th >DELETE</th><th >VIEW</th><th >ID</th><th >DATE</th><th >PO NUMBER</th>";
while ($row = $result->fetch_object()) {
$date = $row->date ;
$ponumber = $row->ponumber;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "<tr>
<td><input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id /></td>
<td>$id</td>
<td>";
/* Edit 2 */
echo "<a href='view.php?id=$id'>view</a>";
/* Edit 2 End */
echo "</td>
<td>$date</td>
<td>$ponumber</td></tr>";
}
echo "</table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form>";}
?>
A style suggestion:
Don't do/stop doing this:
echo "<form method='post' action='delete.php'>";
echo ...
while
Where what you are echoing is a static string. Instead, do:
?>
<form method='post' action='delete.php'>
...
<?php
while
it's simply easier to read and maintain.