我有两个表可以应用更改。但我需要回应基于某个标准所做的更改。现在对于第一个表,所做的任何更改都会被回显,但是如果对第二个表(else)进行更改,我不确定如何回显更改。
if (isset($_POST['submit']))
{
if (isset($_POST['ID'])) {
$sql = "SHOW COLUMNS FROM Employees";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
$tempname = $row['Field'];
// Changes Function
$sqlCheck = "SELECT * FROM Employees WHERE ID='".$_GET["id"]."' AND (".$row['Field']." NOT LIKE '".$_POST[$tempname]."')";
$result3 = mysqli_query($con,$sqlCheck);
if (mysqli_num_rows($result3) > 0) {
// output data of each row
while($row3 = mysqli_fetch_array($result3)) {
$sql3 = "INSERT INTO `Changes` (`Table`, `ID`, `Attribute`, `DateChanged`, `HRUser`, `OldValue`, `NewValue`) VALUES ('Employees', '".$_GET["id"]."', '".$row["Field"]."', '".date("d/m/Y h:i:sa")."', '$login_session', '$row3[$tempname]', '$_POST[$tempname]')";
if (mysqli_query($con,$sql3) === TRUE) {
} else {
echo "Error: " . $sql3 . "<br>" . mysqli_error($con);
}
}
}
//End Changes Function
$sql2 = "UPDATE Employees SET ".$row['Field']."= '$_POST[$tempname]' WHERE ID='".$_GET["id"]."'";
$result2 = mysqli_query($con,$sql2);
if (mysqli_query($con,$sql2) === TRUE) {
} else {
echo "Error: " . $sql2 . "<br>" . mysqli_error($con);
echo '<script>swal("Error", "Something went wrong '.mysqli_error($con).'", "error");</script>';
}
}
echo '<script>swal("Success", "Changes have been saved", "success");</script>';
}
//End If POST Submit True
else
{
$sql = "SHOW COLUMNS FROM Candidates";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
$tempname = $row['Field'];
// Changes Function
$sqlCheck = "SELECT * FROM Candidates WHERE ID='".$_GET["cid"]."' AND (".$row['Field']." NOT LIKE '%".$_POST[$tempname]."%')";
$result4 = mysqli_query($con,$sqlCheck);
if (mysqli_num_rows($result4) > 0) {
// output data of each row
while($row4 = mysqli_fetch_array($result4)) {
$sql4 = "INSERT INTO `Changes` (`Table`, `ID`, `Attribute`, `DateChanged`, `HRUser`, `OldValue`, `NewValue`) VALUES ('Candidates', '".$_GET["cid"]."', '".$row["Field"]."', '".date("d/m/Y h:i:sa")."', '$login_session', '$row4[$tempname]', '$_POST[$tempname]')";
if (mysqli_query($con,$sql4) === TRUE) {
} else {
echo "Error: " . $sql4 . "<br>" . mysqli_error($con);
}
}
}
答案 0 :(得分:1)
使用mysqli_affected_rows()
。
mysqli_affected_rows
返回先前MySQL操作中受影响的行数
因此,您可以判断是否已根据以前的数据进行了更改。