New to PHP. I’m making a web application. I was trying to make some editable tables in which you can change data directly and those changes then would be saved in database. Found some solutions on net, but I have trouble implement it. Can’t find where I’m wrong. Appreciate any help.
This is my PHP code with editable tables:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="author" content="Slavoljub">
<meta name="description" content="Sajt za obradu podataka na farmama">
<title>Prasence</title>
<link rel="icon" href="./img/pigicon.png" type="image/png" sizes="16x16">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@500&family=Titan+One&display=swap" rel="stylesheet">
<!-- Uključuje jQuery i BootStrap -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type="text/javascript" src="jquery.tabledit.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.9.4/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link rel="stylesheet" href="css/stilovi.css"/>
</head>
<body>
<header>
<h1>Prasence</h1>
<div class="login">
<a class="prijava" href="login.php">Prijava</a>
<a class="prijava" href="signUp.php">Registracija</a>
</div>
</header>
<nav>
<div class="navig">
<a href="index.php">Početna</a>
<a href="sow.php">Krmače</a>
<a href="barn.php">Štale</a>
<a href="data.php">Podaci</a>
</div>
</nav>
<div class="container">
<?php
session_start();
include "connection.php";
if (isset($_SESSION["user_id"])) {
$userId = $_SESSION["user_id"];
if (isset($_POST["farrowAll"])) {
$sql = "SELECT sowNumber, cycleNumber, penNumber, pigletBornNumber, livePigletBorn, farrowingDate FROM farrowing WHERE userId = '$userId'";
$result = $mysqli->query($sql);
if ($result) {
echo '<table id="data_table" class="table table-striped">';
echo '<tr>
<th>Korisnik</th>
<th>Broj krmače</th>
<th>Broj ciklusa</th>
<th>Broj boksa</th>
<th>Broj prasadi pri rođenju</th>
<th>Broj žive prasadi pri rođenju</th>
<th>Datum prašenja</th>
</tr>';
$totalPigletBorn = 0; // Inicijalizacija varijable za ukupan broj prasadi
$totalLivePigletBorn = 0;
while ($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $userId . '</td>';
echo '<td>' . $row['sowNumber'] . '</td>';
echo '<td>' . $row['cycleNumber'] . '</td>';
echo '<td>' . $row['penNumber'] . '</td>';
echo '<td>' . $row['pigletBornNumber'] . '</td>';
echo '<td>' . $row['livePigletBorn'] . '</td>';
echo '<td>' . $row['farrowingDate'] . '</td>';
echo '</tr>';
$totalPigletBorn += $row['pigletBornNumber'];
$totalLivePigletBorn += $row['livePigletBorn'];
}
// Prikazuje ukupan broj prasadi ispod tabele
echo '<tr>';
echo '<td colspan="3"></td>';
echo '<td>Total : ' . $totalPigletBorn . '</td>';
echo '<td>Total : ' . $totalLivePigletBorn . '</td>';
echo '<td colspan="2"></td>';
echo '</tr>';
echo '</table>';
$result->close();
} else {
echo "Greška pri izvršavanju upita: " . $mysqli->error;
}
} else if (isset($_POST["farrowCurrent"])) {
$sql = "SELECT sowNumber, cycleNumber, penNumber, pigletBornNumber, livePigletBorn, farrowingDate FROM farrowing WHERE userId = '$userId' AND currentInFarrHouse = 1";
$result = $mysqli->query($sql);
if ($result) {
echo '<table id="data_table" class="table table-striped">';
echo '<tr>
<th>Korisnik</th>
<th>Broj krmače</th>
<th>Broj ciklusa</th>
<th>Broj boksa</th>
<th>Broj prasadi pri rođenju</th>
<th>Broj žive prasadi pri rođenju</th>
<th>Datum prašenja</th>
</tr>';
$totalPigletBorn = 0; // Inicijalizacija varijable za ukupan broj prasadi
$totalLivePigletBorn = 0;
while ($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $userId . '</td>';
echo '<td>' . $row['sowNumber'] . '</td>';
echo '<td>' . $row['cycleNumber'] . '</td>';
echo '<td>' . $row['penNumber'] . '</td>';
echo '<td>' . $row['pigletBornNumber'] . '</td>';
echo '<td>' . $row['livePigletBorn'] . '</td>';
echo '<td>' . $row['farrowingDate'] . '</td>';
echo '</tr>';
$totalPigletBorn += $row['pigletBornNumber'];
$totalLivePigletBorn += $row['livePigletBorn'];
}
// Prikazuje ukupan broj prasadi ispod tabele
echo '<tr>';
echo '<td colspan="3"></td>';
echo '<td>Total : ' . $totalPigletBorn . '</td>';
echo '<td>Total : ' . $totalLivePigletBorn . '</td>';
echo '<td colspan="2"></td>';
echo '</tr>';
echo '</table>';
$result->close();
} else {
echo "Error executing the SQL query: " . $mysqli->error;
}
} else {
echo "Greška pri unosu podataka";
}
} else {
echo "Morate se ulogovati kako biste unosili podatke";
}
?>
</div>
<script type="text/javascript" src="custom_table_edit.js"></script>
</body>
</html>
There is javascript file I linked in head section:
<script type="text/javascript" src="jquery.tabledit.js"></script>
Other javascript file is , and there is the code:
$(document).ready(function () {
$("#data_table").Tabledit({
deleteButton: false,
editButton: false,
columns: {
identifier: [0, "userId"],
editable: [
[1, "sowNumber"],
[2, "cycleNumber"],
[3, "penNumber"],
[4, "sectionNumber"],
[5, "pigletBornNumber"],
[6, "livePigletBorn"],
[7, "farrowingDate"],
],
},
hideIdentifier: true,
url: "live_edit.php",
});
});
Here is the code for live_edit.php file:
<?php
session_start();
include_once("connection.php");
$input = filter_input_array(INPUT_POST);
if ($input['action'] == 'edit') {
$update_field = '';
if (isset($input['userId'])) {
$update_field .= "userId='" . $input['userId'] . "'";
} else if (isset($input['sowNumber'])) {
$update_field .= "sowNumber='" . $input['sowNumber'] . "'";
} else if (isset($input['cycleNumber'])) {
$update_field .= "cycleNumber='" . $input['cycleNumber'] . "'";
} else if (isset($input['penNumber'])) {
$update_field .= "penNumber='" . $input['penNumber'] . "'";
} else if (isset($input['sectionNumber'])) {
$update_field .= "sectionNumber='" . $input['sectionNumber'] . "'";
} else if (isset($input['pigletBornNumber'])) {
$update_field .= "pigletBornNumber='" . $input['pigletBornNumber'] . "'";
} else if (isset($input['livePigletBorn'])) {
$update_field .= "livePigletBorn='" . $input['livePigletBorn'] . "'";
} else if (isset($input['farrowingDate'])) {
$update_field .= "farrowingDate='" . $input['farrowingDate'] . "'";
}
if ($update_field && $input['id']) {
$sql_query = "UPDATE farrowing SET $update_field WHERE userId='" . $input['userId'] . "'";
$result = $mysqli->query($sql_query);
if (!$result) {
echo "Error executing the SQL query: " . $mysqli->error;
die("database error:" . $mysqli->error);
}
}
}
?>
It wont update the database