Select and update radio options of a multiple id with PHP MySQL - php

I'm trying to get all the students test reports of a specific date in a table and display whether they have passed or failed in a radio button for each student. From the table, I'm trying to update their test result (passed/failed). But in my query, I'm getting only result of either one of the students and when I update, only that specific student gets updated, none other does.
Here is my index.php file. In the table only one students report radio button is active:
<?php
$mysqli = new MySQLi( 'localhost', 'root', '', 'students' )or die( mysql_error( $mysqli ) );
$result = $mysqli->query( "SELECT * FROM class_nine WHERE test_date = '2020-06-20' ORDER BY ID DESC" )or die( $mysqli->error );
?>
<div class="container">
<div class="row">
<form action="process.php" method="POST">
<input type="hidden" name="id" value="<?php echo $id;?>">
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Report</th>
</tr>
</thead>
<?php
while ( $row = $result->fetch_assoc() ):
?>
<tr>
<td><input type="text" name="id" value="<?php echo $row['id']; ?>"></td>
<td><?php echo $row['name'];?></td>
<td><label class="radio-inline">
<input type="radio" name="report" value="Passes" <?=$row['report'] == "Passed" ? "checked" : ""?>>
Passed</label>
<label class="radio-inline">
<input type="radio" name="report" value="Failed" <?=$row['report'] == "Failed" ? "checked" : ""?>>
Failed</label></td>
</tr>
<?php endwhile; ?>
</table>
<button type="submit" name="update">Update</button>
</form>
</div>
</div>
This is my process file:
<?php
session_start();
$mysqli = new MySQLi( 'localhost', 'root', '', 'students' )or die( mysql_error( $mysqli ) );
$id = 0;
$report = '';
if ( isset( $_POST[ 'update' ] ) ) {
$id = $_POST[ 'id' ];
$report = $_POST[ 'report' ];
$mysqli->query( "UPDATE class_nine SET
report = 'report'
WHERE id = $id" )or die( $mysqli->error );
header( "location: index.php" );
}
?>
This is how my MySQL table is structured:
CREATE TABLE class_nine(
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(1) NOT NULL
,test_date DATE NOT NULL
,report VARCHAR(6)
);
INSERT INTO class_nine(id,name,test_date,report) VALUES (23,'A','2020-06-20','Passed');
INSERT INTO class_nine(id,name,test_date,report) VALUES (33,'B','2020-06-20',NULL);
INSERT INTO class_nine(id,name,test_date,report) VALUES (35,'C','2020-06-20','Failed');
INSERT INTO class_nine(id,name,test_date,report) VALUES (45,'D','2020-06-22',NULL);

You are using the same name="report" for all radio buttons, that is the reason why only one is being checked. Use this instead
name="<?=$row['id']?>"
You will have each student result posted with the name being assigned to student id, In your process file loop through these value and update each student's result.

Related

PHP Output from mysql database is blank

I have this PHP page which accepts 3 items and stores into a database. The output of the results fetched from database is blank. I can see the records being saved in database via phpmyadmin. What's wrong with this code?
This is the code that displays results in a table.
// Add a shortcode to display the employee's attendance record
function attendance_record_shortcode( $atts ) {
global $wpdb;
$table_name = $wpdb->prefix . 'employee_attendance';
// Get the employee ID from the shortcode parameters
$atts = shortcode_atts( array(
'employee_id' => ''
), $atts );
$employee_id = $atts['employee_id'];
// Get the employee's attendance data from the database
$attendance_data = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM $table_name WHERE employee_id = %d",
$employee_id
)
);
// Display the attendance data in a table
ob_start();
?>
<table>
<thead>
<tr>
<th>Date</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ( $attendance_data as $attendance ) : ?>
<tr>
<td><?php echo $attendance->date; ?></td>
<td><?php echo $attendance->status; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
return ob_get_clean();
}
add_shortcode( 'attendance_record', 'attendance_record_shortcode' );
This is the code that accepts input and saves data to database.
// Create the attendance page and display the attendance form
function attendance_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'employee_attendance';
// If the form has been submitted, add the attendance data to the database
if ( isset( $_POST['submit_attendance'] ) ) {
$employee_id = $_POST['employee_id'];
$date = date( 'Y-m-d', strtotime( $_POST['date'] ) );
$status = $_POST['status'];
$wpdb->insert(
$table_name,
array(
'employee_id' => $employee_id,
'date' => $date,
'status' => $status
)
);
}
// Display the attendance form
?>
<h1>Employee Attendance</h1>
<form method="post">
<label for="employee_id">Employee ID</label>
<input type="text" name="employee_id" id="employee_id" required>
<br>
<label for="date">Date</label>
<input type="date" name="date" id="date" required>
<br>
<label for="status">Status</label>
<select name="status" id="status">
<option value="present">Present</option>
<option value="absent">Absent</option>
<option value="late">Late</option>
</select>
<br>
<input type="submit" name="submit_attendance" value="Submit">
</form>
<?php
}
phpMyAdmin Database Snapshot
Blank Output Page

php page to list and update sqlite

I have the following code to display and modify a simple sqlite table
<?php
$db = new SQLite3("my_sqlite.db");
$query = "SELECT rowid, * FROM students";
$result = $db->query($query);
if( isset($_POST['submit_data']) ){
// Gets the data from post
$name = $_POST['name'];
$email = $_POST['email'];
$query = "UPDATE students set name='$name', email='$email'";
if( $db->exec($query) ){
echo "Data is updated successfully.";
}else{
echo "Sorry, Data is not updated.";
}
}
?>
<table border="1">
<form action="" method="post">
<tr>
<td>Name</td>
<td>Email</td>
</tr>
<?php while($row = $result->fetchArray()) {?>
<tr>
<td><input name="name" type="text" value="<?php echo $row['name'];?>"></td>
<td><input name="email" type="text" value="<?php echo $row['email'];?>"></td>
</tr>
<?php } ?>
<input name="submit_data" type="submit" value="Update Data">
</form>
</table>
PROBLEM: When I change some of the information and update, the whole column changes into the same change. E.g.: if I write a the name Nick, every name changes into Nick.
First, you should only do updates for one record at a time so each record needs its own update button. Attached is the corresponding rʔwid of the record. you can use:
<input type="hidden" name="rowid" value="$row['rowid]">
You should add a WHERE clause to the update statement to know exactly which records should be updated.If you omit the WHERE clause, ALL records will be updated!

Can't edit data in the database and cant fetch name with id?

I am trying to edit data into the database I don't know why I cant do. I have tried something till now. maybe someone can have a look please. I am trying to built a update where i can change name, surname blah blah blah, but i cant config even just for a name first..
Home file
Managament System
<body>
<h1>TU Chemnitz Student managament system</h1>
<br>
ADD Person
Edit Person
Manage Boards
Manage Departments
Search N&S
Triple Search
Membership
<br>
<br>
<?php
include_once('coneksioni.php');
// create query
$querys = "SELECT * FROM tblperson";
// execute query
$result = mysql_query($querys) or die ("Error in query: $query. ".mysql_error());
$res = mysql_query("SELECT * FROM tblperson");
echo "<table border=1 align=center>
<tr>
<th>Personal ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Deparment</th>
<th>Board</th>
<th>Marticulation Number</th>
<th>Reg Date</th>
<th>Action</th>
</tr>";
while($row = mysql_fetch_array($res)) {
?>
<tr>
<td><?=$row['personid']?></td>
<td><?=$row['personname']?></td>
<td><?=$row['personsurname']?></td>
<td><?=$row['persondepartment']?></td>
<td><?=$row['personboard']?></td>
<td><?=$row['martinumber']?></td>
<td><?=$row['personregdate']?></td>
<td>
edit |
del
</td>
</tr>
<?php
}
?>
</body>
</html>
and edit20.php
<?php
include_once('coneksioni.php');
if( isset($_GET['edit']) )
{
$personid = $_GET['edit'];
$res= mysql_query("SELECT * FROM tblperson WHERE personid='$personid'");
$row= mysql_fetch_array($res);
}
if( isset($_POST['personname']) )
{
$personname = $_POST['personname'];
$personid = $_POST['personid'];
$sql = "UPDATE tblperson SET personname='$personname' WHERE personid='$personid'";
$res = mysql_query($sql)
or die("Could not update".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=home.php'>";
}
?>
<form action="edit20.php" method="POST">
Name: <input type="text" name="personname" value="<?php echo $row[1]; ?>"><br />
<input type="hidden" name="personid" value="<?php echo $row[0]; ?>">
<input type="submit" value=" Update "/>
</form>
and in the database primary key in my table is personid name field personname (not Primary key).
Please use Prepared Statement for reduce the risk of SQL Injection
check the coneksioni.php
$conn = new mysqli(HOST, USER, PASS, DBNAME);
the edit.php
require_once ('coneksioni.php');
$edit_person = $conn->prepare("
UPDATE tblperson SET
personname = ? WHERE personid = ?
");
$edit_person->bind_param(
"si",
$personname, $personid
);
if(isset($_POST['personname']) && isset($_POST['personid']) ) {
$personname = $_POST['personname'];
$personid = $_POST['personid'];
if (!$edit_person->execute()) {
// action if failed
} else {
// action if success
}
$edit_person->close();
}
the form.html
<form action="edit.php" method="POST">
Name: <input type="text" name="personname" value="<?php echo $row[1]; ?>"><br />
<input type="hidden" name="personid" value="<?php echo $row[0]; ?>">
<input type="submit" value=" Update "/>
</form>
Cheers

Sort from database tables by multiple checkbox php

I have a question about display data from the database alphabetically.
I have 2 ckeckboxes and if the nr.1 is checked, it must display data alphabetically from table "companyName" but if chechbox nr.2 is checked, it must display data alphabetically from table "stade".
I am very new to coding, so I will be happy if there was someone who could explain or show me exactly how it should look.
Here is the checkboxes:
<fieldset>
<dl">
<dt><label for="sleep">Sortering:</label></dt>
<dd>
<input type="checkbox" name="" value="Alfabetisk A-Z"/>
<label for="alfabetiskA-z" class="opt">Alfabetisk A-Z</label>
<input type="checkbox" name="" value="Alfabetisk Stade"/>
<label for="alfabetiskStade" class="opt">Alfabetisk Stade</label>
</dd>
</dl>
</fieldset>
Here is where I show my data fra the database:
echo '
<table class="tableUser">
<tr class="topColor">
<td>Stade</td>
<td>Firmanavn</td>
<td>Navn</td>
<td>Telefon nr.</td>
<td>E-mail adresse</td>
</tr>';
$class0 = "trColor0";
$class1 = "trColor1";
$query = mysqli_query($conn,"SELECT * FROM creatUser ORDER BY companyName ASC");
while( $result = mysqli_fetch_array($query)) {
$id = $result['id'];
$class = "trColor". $i%2;
echo '
<tr class="'.$class.'">
<td VALIGN=TOP>'.$result['locationNumber'].'</td>
<td VALIGN=TOP>'.$result['companyName'].'</td>
<td VALIGN=TOP>'.$result['fName'] . ' ' . $result['lName'].'</td>
<td VALIGN=TOP>'.$result['phone'].'</td>
<td VALIGN=TOP>'.$result['eMail'].'</td>
</tr>';
$i++;
}
You have to give names to your checkboxes, so you can get what the user selected:
<input type="checkbox" name="sort" value="Alfabetisk A-Z"/>
<label for="alfabetiskA-z" class="opt">Alfabetisk A-Z</label>
<input type="checkbox" name="sort" value="Alfabetisk Stade"/>
<label for="alfabetiskStade" class="opt">Alfabetisk Stade</label>
Then in your PHP, you do:
if (!isset($_POST['sort']) || $_POST['sort'] == 'Alfabetisk A-Z') {
$order = 'companyName'
} else {
$order = 'stade';
}
$sql = "SELECT * FROM creatUser ORDER BY $order ASC";
$query = mysqli_query($conn, $sql) or die (mysqli_error($conn));

How to insert checkbox data of selected value?

Hi i am trying to insert checkbox data Yes/No of selected customer. Selected customer (who's value is retrieved from database) if checked yes then Y will be inserted in table field and if checked no then N will be inserted in table field. I can't find out the solution to do so, tried it!
Thanks for help in advance.
Here is what i have so far;
<table width="400" border="1" bordercolor="#598DD5">
<tr>
<td bordercolor="#598DD5" bgcolor="#598dd5"><span class="style1">Projects</span></td>
<td bordercolor="#598DD5" bgcolor="#598dd5"><span class="style1">Hot Project</span>
</td>
</tr>
<tr>
<td bordercolor="#598DD5"><select name="customer">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY customer ORDER BY customer";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["customer"]."'".($row["customer"]==$_REQUEST["customer"] ? " selected" : "").">".$row["customer"]."</option>";
} ?>
</select></td>
<td bordercolor="#598DD5">
<div align="center">
Yes <input type="checkbox" name="hotproject" value="Y" <?php if( isset( $_GET['customer'] ) ){ ?> checked="checked" <?php } ?> />
No <input type="checkbox" name="hotproject" value="N" <?php if( isset( $_GET['customer'] ) ){ ?> checked="checked" <?php } ?> /></div> </td>
</tr>
You can use Javascript with jQuery for change the value of checkbox
OR
Change the type "checkbox" to "radio" with the same name of the input.
Here : http://jsbin.com/UPuQ/1/edit

Categories