Not updating in mysql , php - 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>

Related

How to Disable Specific Button rows in php

I want to disable the two buttons in a single row if the button will be clicked and won't affect the buttons of another row.
I dont know how to disable an echo button of the table. I want to disable the "Accept" and "Reject" button if one of those was been clicked.I provide a screenshot so that you can easily understand what I mean. Thank you in advance.
Here's my php code. It names as app.php
<?php
//connect to database
$con = mysqli_connect('127.0.0.1','root','');
//select database
mysqli_select_db($con, 'appointment');
//select query
$sql = "SELECT * FROM service";
//Execute the query
$records = mysqli_query($con,$sql)
?>
<html>
<head>
<title>Appointment Schedule</title>
</head>
<body>
<table width = "100%" border = "5px" height = "20%">
<tr align = "left">
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Date</th>
<th>Time</th>
<th>Office</th>
<th>Service</th>
<th>Contact No.</th>
<th>Remarks</th>
</tr>
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action = 'display.php' method = post>";
echo "<input type=hidden name=id value='".$row['ID']."'>";
echo "<td>".$row['fname']."</td>";
echo "<td>".$row['mname']."</td>";
echo "<td>".$row['lname']."</td>";
echo "<td>".$row['address']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['time']."</td>";
echo "<td>".$row['office']."</td>";
echo "<td>".$row['services']."</td>";
echo "<td><name = number>".$row['contactno']."</td>";
echo "<td>".$row['remarks']."</td>";
echo "<td><input type =submit value='Accepted' name=accept>";
echo "<td><input type =submit value='Rejected' name=reject>";
echo "</form></tr>";
}
?>
</table>
</body>
</html>
here's my another one php code. It names display.php
<?php
//connect to database
$con = mysqli_connect('127.0.0.1','root','');
//select database
mysqli_select_db($con, 'appointment');
if($_POST['accept'])
{
$sql = "UPDATE service SET remarks = 'Accepted' WHERE ID=$_POST[id]";
}
else if($_POST['reject'])
{
$sql = "UPDATE service SET remarks = 'Rejected' WHERE ID=$_POST[id]";
}
//Execute Query
if(mysqli_query($con,$sql))
header("refresh:1; url=app.php");
else
echo "Unsuccessful";
?>
here's the screenshot of my work
Sample of my database table using php
Kindly try the below code:
Provide the condition to display the buttuon
echo "<td><input type =submit value='Accepted' name=accept>";
echo "<td><input type =submit value='Rejected' name=reject>";
if($row['remarks'] == 'Accepted' || $row['remarks'] == 'Rejected')
{
echo "<td><input type =submit disabled value='Accepted' name=accept>";
echo "<td><input type =submit disabled value='Rejected' name=reject>";
}

Call Two MySql Table with A Button

I have these table that list all the users of the site. I would like to add a button where in, once that is clicked, the other details of the user will show.
This is my code so far.
echo "<table border=1 align=center><tr class=style2><td>+<td>Student Name<td>Age<td>Address<td>School<td>Email";
$query = mysql_query("SELECT name, age, address, school, email FROM mst_user",$cn) or die(mysql_error());
while($row=mysql_fetch_row($query))
{
echo "<tr class=style8><td>+<td>$row[0]<td align=center> $row[1]<td align=center>$row[2]<td align=center>$row[3]<td align=center>$row[4]";
}
echo "</table>";
The code shows all the user of the site. The <td>+will be a button that when click will show the other details of the user. The other details will show or call a file which is result.php
result.php
$rs=mysql_query("select t.test_name,t.total_que,r.test_date,r.score from mst_test t, mst_result r where
t.test_id=r.test_id and r.login='$login'",$cn) or die(mysql_error());
echo "<h1 class=head1> Result </h1>";
if(mysql_num_rows($rs)<1)
{
echo "<br><br><h1 class=head1> You have not given any quiz</h1>";
exit;
}
echo "<table border=1 align=center><tr class=style2><td width=300>Test Name <td> Total<br> Question <td> Score";
while($row=mysql_fetch_row($rs))
{
echo "<tr class=style8><td>$row[0] <td align=center> $row[1] <td align=center> $row[3]";
}
echo "</table>";
my tables
Any suggestions on how can I achieve it?

add rows from another table to another table

Suppose i have my table product (id,name,description,image,price) . I have another table product_add (id,name,price).
I have selected all from table product and display it
here are the codes that display all the data from table product
<html>
<head>
<script type="text/javascript" src="add.js"/>
</head>
<?php
include'dbConnect.php';
$image =$_GET['image'];
$id = $_GET['id'];
$name = $_GET['name'];
$price= $_GET['price'];
$sql="SELECT * FROM product";
$result = mysql_query($sql);
echo sizeof($result);
if($result>0){
echo "<table border='1'>
<tr>
<td>ID</td>
<td>Image</td>
<td>Name</td>
<td>Price MUR</td>
</tr>";
while ($row = mysql_fetch_array($result)){
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>$id</td>";
echo "<td><img src=$image width='120' height='100'/></td>";
echo "<td>$name</td>";
echo "<td class='textAlignRight'>$price</td>";
echo "<td class='textAlignCenter'>";
?>
<input type='button' name='Add_value' value='Add to Cart' onclick='addfunction()'/>
<?php
}//end while
echo "</table>";
}
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id', '$name','$price' ) ";
$insertQuery=mysql_query($insert);
?>
<div id='ajaxDiv'>Your result will display here</div>
</html>
But the problem is that when I click on the Add to cart button, only the last row is being added to the product_add table whenever I am adding a particular product to the cart.

Pass a dynamic variable through URL php

I'm not sure about the title, I tried my best.
I have a table displayed with information from a database using this file
display.php
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("tournaments") or die(mysql_error());
$result = mysql_query("SELECT * FROM tournies")
or die(mysql_error());
echo '<table id="bets" class="tablesorter" cellspacing="0" summary="Datapass">
<thead>
<tr>
<th>Tournament <br> Name</th>
<th>Pot</th>
<th>Maximum <br> Players</th>
<th>Minimum <br> Players</th>
<th>Host</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>';
while($row = mysql_fetch_array( $result )) {
$i=0; if( $i % 2 == 0 ) {
$class = "";
} else {
$class = "";
}
echo "<tr" . $class . "><td>";
echo $row['tour_name'];
$tour_id = $row['tour_name'];
echo "</td><td>";
echo $row['pot']," Tokens";
echo "</td><td class=\"BR\">";
echo $row['max_players']," Players";
echo "</td><td class=\"BR\">";
echo $row['min_players']," Players";
echo "</td><td class=\"BR\">";
echo $row['host'];
echo "</td><td>";
echo "<input id=\"delete_button\" type=\"button\" value=\"Delete Row\" onClick=\"SomeDeleteRowFunction(this)\">";
echo "</td><td>";
echo "<form action=\"join.php?name=$name\" method=\"POST\" >";
echo "<input id=\"join_button\" type=\"submit\" value=\"Join\">";
echo "</td></tr>";
}
echo "</tbody></table>";
?>
Basically I want the user to press a button from a row of the table and they go to a new page called join.php. I need the persons username and the name of the tournament from the row the clicked.
For example here's my page:
When they click the join button at the end of row one it should send them to
'join.php?name=thierusernamehere&tourname=dfgdds'
Any help much appreciated. Thanks.
echo '<td>Join</td>'
There are many way to approach.
The easiest way is just echo 'JOIN';
or you can use a form with hidden input and submit button.
BUT
Your code is really a mess, try to make your code more maintainable and readable. And do NOT use any mysql_* functions, they are deprecated.
Read more about PDO:
http://php.net/manual/en/book.pdo.php
http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

hyperlink on every row of mysql data

<?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.

Categories