inserting html table values in mysql table - php

I am trying to submit a html table values into mysql table. First two columns of html table are fetched from mysql table 'student' and third column is to be filled online. I want to insert all three columns to another table in msyql. I am just facing problem in inserting all table values. code is as under:-
<html>
<body>
<?php
$connection = mysqli_connect ('localhost', 'user', 'password', 'db');
if (!$connection){
echo 'Not connected to server';
}
$select_db = mysqli_select_db($connection, 'db');
if (!$select_db){
echo 'Not connected to database';
}
$SelectClass = $_POST ['selectclass'];
$sql= "SELECT * FROM students WHERE class = '$SelectClass'";
$query = mysqli_query($connection, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($connection));
}
mysqli_close($connection);
?>
<body>
<div class="container">
<h1><strong>Please enter marks of each student for subject</strong></h1>
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Marks Obtained</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$row['student_id'].'</td>
<td>'.$row['student_name'].'</td>
<td>'."<div class='search-block clearfix'><input name='obtmarks' placeholder='' type='number'></div>".'</td>
</tr>';
$total += $row['stu_id'];
$no++;
}?>
</tbody>
</table>
<button type="submit" class="btn btn-warning" value="insert" align="right">Update<span class="glyphicon glyphicon-send"></span></button>
</form>
</div>
</body>
</html>

Have a look at the updated code below :
<body>
<?php
$connection = mysqli_connect ('localhost', 'user', 'password', 'db');
if (!$connection){
echo 'Not connected to server';
}
$select_db = mysqli_select_db($connection, 'db');
if (!$select_db){
echo 'Not connected to database';
}
$SelectClass = $_POST ['selectclass'];
$sql= "SELECT * FROM students WHERE class = '$SelectClass'";
$query = mysqli_query($connection, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($connection));
}
mysqli_close($connection);
//***********Form Submit Goes Here***********//
if($_POST)
{
$student_id = $_POST['student_id'];
$student_name = $_POST['student_name'];
$student_marks = $_POST['obtmarks'];
//your code for insertion....
}
?>
<body>
<div class="container">
<h1><strong>Please enter marks of each student for subject</strong></h1>
<form action="" method="post">
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Marks Obtained</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$row['student_id'].'</td>
<input type="hidden" name="student_id" value='.$row['student_id'].'>
<td>'.$row['student_name'].'</td>
<input type="hidden" name="student_name" value='.$row['student_name'].'>
<td>'."<div class='search-block clearfix'><input name='obtmarks' placeholder='' type='number'></div>".'</td>
</tr>';
$total += $row['stu_id'];
$no++;
}?>
</tbody>
</table>
<button type="submit" class="btn btn-warning" value="insert" align="right">Update<span class="glyphicon glyphicon-send"></span></button>
</form>
</div>
</body>
</html>

Related

TWO MySQL commands (SELECT to View), but only one is showing output

I have a table and each , I want to select a data from the same table in my database.
For example, first <td> is first name, then the second <td> is phone number.
I got the command, but only the first command is showing output.
This is my php codes to open and connect to the database :
<?php
include("./inc/db_connect.php");
$conn = OpenCon();
?>
This is the php codes for the table including <th> and <td> :
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<th>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
echo "Name";
}
}
?>
</th>
<th>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
echo "Phone Number";
}
}
?>
</th>
</tr>
<tr>
<td>
<?php
$sql = "SELECT first_name FROM sharp_emp WHERE employee_id = 'AA170336'";
while ($row = $result->fetch_array()) {
echo "" . $row['first_name'] . "";
}
?>
</td>
<td>
<?php
$sql = "SELECT phone FROM sharp_emp WHERE employee_id = 'AA170336'";
while ($row = $result->fetch_array()) {
echo "" . $row['phone'] . "";
}
?>
</td>
</tr>
</tbody>
</table>
</h3>
</div>
This is the php codes for db_connect.php :
<?php
function OpenCon()
{
$dbhost = "localhost";
$dbuser = // Hidden;
$dbpass = // Hidden;
$db = "sharp_db";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
?>
The expected output :
|----------|----------|
|Name |Phone Number|
|----------|----------|
|John |179898765 |
The current output :
|----------|----------|
|Name |Phone Number|
|----------|----------|
|John |Null (empty) |
You are running the same query multiple times, overwriting the $result variable for no reason, having useless $sql for the later 2 fetch without using them, and fetching a single $result twice by mistake.
So there are multiple concept problem with your code. I think your current code is something equivalant to this:
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
?>
<th>Name</th>
<th>Phone Number</th>
<?php } else { ?>
<th></th>
<th></th>
<?php } ?>
<?php } ?>
</tr>
<tr>
<?php if ($row = $result->fetch_array()) { ?>
<td><?php echo "" . $row['first_name'] . ""; ?></td>
<td><?php echo "" . $row['phone'] . ""; ?></td>
<?php } else { ?>
<td></td>
<td></td>
<?php } ?>
</tr>
</tbody>
</table>
</h3>
</div>
But frankly, it makes no sense to me to print an empty table when there is no result. So what you need is probably something like this.
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if (
($result = $conn->query($sql))
&& ($result->num_rows > 0)
&& ($row = $result->fetch_array())
):
?>
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<th>Name</th>
<th>Phone Number</th>
</tr>
<tr>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['phone']; ?></td>
</tr>
</tbody>
</table>
</h3>
</div>
<?php endif; ?>

Deleting record from database with button in PHP

I have to delete a record from database using a button but my delete query does not work. Records are entered in database successfully with insertion query. I followed exact tutorial for php code available on YouTube "How to delete records from database with PHP & MySQL" by "kanpurwebD". The code in tutorial works fine but my code still does not delete record. (I have 2 records entered in database).
My code is as follows:
<div class="container">
<div class="row">
<form action='add_record.php' method='get'><button type='submit' name='id' value='submit' class='btn btn-default'>ADD RECORD</button><br />
</form>
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Topic #</th>
<th>Name</th>
<th>Admin ID</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
echo '<br />';
$query = "SELECT * FROM tb_topic";
$result = $con->query($query);
if(isset($_POST['submitDeleteBtn'])){
$key = $_POST['keyToDelete'];
$check = "Select * from tb_topic where topic_id = '$key'";
if(mysqli_num_rows($con, $check)>0){
$query_delete = mysqli_query($con,"Delete from tb_topic where topic_id = '$key'");
echo 'record deleted';
}
}
while($query_row = mysqli_fetch_array($result)) {?>
<tr>
<td><?php echo $query_row['topic_id'];?></td>
<td><?php echo $query_row['topic_name'];?></td>
<td><?php echo $query_row['aid'];?></td>
<td><input type = 'checkbox' name = 'keyToDelete' value = "<?php echo $query_row['topic_id'];?>" required></td>
<td><input type="submit" name="submitDeleteBtn" class="btn btn-danger"></td>
</tr>
<?php }
?>
</html>
I got it resolved by using following statement:
if(isset($_GET['delete'])) {
$page = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$sql = "DELETE FROM tb_topic WHERE topic_id = $page";
}
$_GET() was not taking id as int so I tried typecasting it and it worked for me.

How to Add update button in html table for editing table data?

Following code extracting values from MySQL table using PHP. I want to add an update button in the last column of this table. I have done some coding, but when I am clicking on update its showing white screen. I want HTML table to become editable when the user clicks on update button and after submitting the values will be changed in MySQL table.
Here is code:
<?php
$connection = mysqli_connect ('localhost', 'user', 'password', 'testdb');
if (!$connection){
echo 'Not connected to server';
}
$select_db = mysqli_select_db($connection, 'testdb');
if (!$select_db){
echo 'Not connected to database';
}
$sql= "SELECT * FROM students";
$query = mysqli_query($connection, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($connection));
}
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE `students` SET `elected_subject`='$ElectedSubject'";
mysql_query($UpdateQuery, $con);
};
?>
<h1><strong>Student Record</strong></h1>
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Father Name</th>
<th>Class</th>
<th>Selected Subject</th>
<th>View/Update</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
echo "<form action=update_students.php method=post>";
$student = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$row['student_id'].'</td>
<td>'.$row['student_name'].'</td>
<td>'.$row['father_name'].'</td>
<td>'.$row['class'].'</td>
<td>'.$row['elected_subject'].'</td>
<td>' . "<input type='submit' name='update' value='update'>" . '</td>
</tr>';
$total += $row['stu_id'];
$no++;
}?>
</tbody>
</table>
update_students.php is the same file which is having this code.

Script for deleting table rows not working in MySQL & PHP

I want to delete a table row from my database with MySQL and PHP. I have searched through my code and I can't figure out what I'm doing wrong. I think I am close to getting it, probably something simple that I'm not realizing.
If I hover over the delete link there is a link showing with the correct ID number of the row to delete. But if I click it, it isn't working. It just refreshes the screen.
This is my code for index.php:
<!-- Table -->
<form action="index.php" method="get" id="dispatch">
<div class="col-md-8 column">
<fieldset>
<legend>Incident Board (Incidents in red are active)</legend>
<div class="scroll-bar">
<table>
<thead>
<tr>
<th>Incident #</th>
<th>Town</th>
<th>Location</th>
<th>Incident Type</th>
<th>Time/Date</th>
<th>Admin</th>
<th>Delete Entry</th>
</tr>
</thead>
<tbody>
<?php
if( isset($_POST['town']) )
{
$town = $_POST['town'];
}
if( isset($_POST['location']) )
{
$location = $_POST['location'];
}
if( isset($_POST['incident_type']) )
{
$incident_type= $_POST['incident_type'];
}
if( isset($_POST['time_date']) )
{
$time_date= $_POST['time_date'];
}
if( isset($_POST['admin']) )
{
$admin = $_POST['admin'];
}
if( isset($_POST['id']) )
{
$id = $_POST['id'];
}
$db = mysqli_connect('localhost','root','') or die("Database error");
mysqli_select_db($db, 'cad');
$result= mysqli_query($db, "SELECT * FROM `cad` ORDER BY `time_date` DESC LIMIT 20");
while($row = mysqli_fetch_array($result))
{
$town = $row['town'];
$location = $row['location'];
$incident_type = $row['incident_type'];
$time_date = $row['time_date'];
$admin = $row['admin'];
$id = $row['id'];
echo "
<tr>
<td class=\"id-center\">
".$id."
</td>
<td >
".$town."
</td>
<td>
".$location."
</td>
<td >
".$incident_type."
</td>
<td >
".$time_date."
</td>
<td >
".$admin."
</td>
<td>
<span class=\"glyphicon glyphicon-trash\"></span>
</td>
</tr>";
}
mysqli_close($db);
?>
</tbody>
</table>
</div>
</fieldset>
</div>
</form>
<!-- End -->
This is my delete.php code:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
$selected = mysql_select_db("cad", $dbhandle);
mysql_query("DELETE FROM cad WHERE id = ".$_GET['id']."");
header('location: index.php');
?>
mysql is deprecated, instead of this use mysqli
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$con=mysqli_connect($hostname, $username, $password,"cad");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"DELETE FROM cad WHERE id = ".$_GET['id']."");
mysqli_close($con);
?>

retrieve and add the data into database

I need to retrieve the relevant data from database, and add to the attendance table again. this code shows many error. actually i dunno how to use the array to add the data. can anyone help?? thankssssssssssss.
<?php
$sql = "select p_module_ID, student_ID,
p_attendance_Date, p_attendance_Time,
p_attendance_Status,p_attendance_reason
from attendance";
$result = mysqli_query($con, $sql);
if(!$result)
{
echo mysqli_error($con);
exit();
}
while($rows = mysqli_fetch_array($result))
{
$attendance_list[] = array('p_module_ID' => $rows['p_module_ID'],
'student_ID' => $rows['student_ID'],
'p_attendance_Date' => $rows['p_attendance_Date'],
'p_attendance_Time' => $rows['p_attendance_Time'],
'p_attendance_Status' => $rows['p_attendance_Status'],
'p_attendance_reason' => $rows['p_attendance_reason']);
}
?>
<html>
<body>
<form action="attendance.php" method="post" accept-charset='UTF-8'>
<table border="0" cellspacing="20" >
<tr>
<td>
<select name="p_module_ID">
<?php
$sql = "SELECT p_module_ID FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_module_ID']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<select name="p_attendance_Date" >
<?php
$sql = "SELECT p_StartDate FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_StartDate']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<select name="p_attendance_Time">
<?php
$sql = "SELECT p_Time FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_Time']."</option>";
}
?>
</select>
</td>
</tr>
<table id="t01">
<tr>
<th> Student ID </th>
<th> Name </th>
<th> Attendance </th>
<th> Reason </th>
<?php foreach($attendance_list as $attend) : ?>
<tr>
<td>
<?php
$sql = "SELECT p.student_ID,CONCAT(s.student_fname, ' ', s.student_lname) AS fullname FROM Pals p JOIN student s ON p.student_ID = s.student_ID WHERE student_role = 'Student' GROUP BY student_ID";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo $attend[$row['student_id']];
}
?>
</td>
<td>
<?php
$sql = "SELECT p.student_ID,CONCAT(s.student_fname, ' ', s.student_lname) AS Fullname FROM Pals p JOIN student s ON p.student_ID = s.student_ID WHERE student_role = 'Student' GROUP BY student_ID";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo $attend['Fullname'];
}
?>
</td>
<td>
<?php echo $attend["p_attendance_Status"]; ?>
</td>
<td>
<?php echo $attend["p_attendance_reason"]; ?>
</td>
</tr>
</tr>
<?php endforeach; ?>
</table>
</table>
</form>
</body>
</html>
Use $row['student_Id'] instead of $row['student_id']. Same as for Fullname. You selecting AS fullname not AS Fullname. Listen to the case sensitiveness.

Categories