Extract from database into text box

I compare the current data with the updated data to check if there are any changes in the information, and add the changes to the new table:

if (isset($_POST['submit'])) 
{
    $sql = "SHOW COLUMNS FROM Employees";
    $result = mysqli_query($con,$sql);
     while($row = mysqli_fetch_array($result)){
                $tempname = $row['Field'];
                $sql2 = "UPDATE Employees SET ".$row['Field']."= '$_POST[$tempname]' WHERE AFNumber='".$_GET["af"]."'";
                $result2 = mysqli_query($con,$sql2);
                if ($con->query($sql2) === TRUE) {
                } else {
                    echo "Error: " . $sql2 . "<br>" . $con->error;
                    echo '<script>swal("Error", "Something went wrong '.$con->error.'", "error");</script>';
                }

    $sqlOldData = "SELECT * FROM Employees WHERE AFNumber='".$_GET["af"]."' AND (".$row['Field']." NOT LIKE '".$_POST[$tempname]."')";
    $result3 = $con->query($sqlOldData);
    if ($result3->num_rows > 0) {

    while($row3 = $result3->fetch_assoc()) {
        $sql3 = "INSERT INTO Changes (Table, AFNumber, Attribute,DateChanged,HRUser,OldValue,NewValue)
VALUES ('Employees', '".$_GET["af"]."', '".$row["Field"]."', '".date('dd/m/Y HH:mm:ss')."', '$login_session', '.$row3[0]', '$_POST[$tempname]')";
        if ($con->query($sql3) === TRUE) {
        echo "New record created successfully";
        } else {
            echo "Error: " . $sql3 . "<br>" . $con->error;
        }
    }
    } else {
        echo "0 results";
    }

}

Now I want to extract information about changes, such as user date ... And put them in the textarea tag in this form:

<textarea name="changes" rows="50" cols="59" disabled>
  12/07/2015 - User:"Mike"  Changed:"Actual Location" From: "blabla" to "bla"
</textarea>

But I'm not sure how to do this, any help please ...

+4
source share
2 answers

Without knowing about your data (for example, which AFNumber), I would suggest simply querying everything from the table Changesand displaying it in the desired form:

$changes = $con->query("SELECT * FROM Changes WHERE Table = 'Employees'");
if ($changes->num_rows > 0) {
    echo '<textarea name="changes" rows="50" cols="59" disabled>' . "\n";
    while ($row = $changes->fetch_assoc()) {
        echo sprintf('%s - User:%s  Chnaged:"%s" From: "%s" to "%s"',
            $row['DateChanged'], $row['HRUser'], $row['Attribute'],
            $row['OldValue'], $row['NewValue']) . "\n";
    }
    echo "</textarea>";
}

To display the data, I just use it echohere, but the use of some template system should not make much difference to the basic concept of the solution.

+3

:

$sql = "SELECT id, firstname, lastname FROM MyGuests"; 
$result = $conn->query($sql); 
if ($result->num_rows > 0) 
{ 
// output data of each row 
    while($row = $result->fetch_assoc()) 
    { 
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; 
    } 
} 
else 
{ 
    echo "0 results"; 
} 
$conn->close();

, , :

echo '<textarea name="changes" rows="50" cols="59" disabled>id:' . $row['id' . ' - Name: ' . $row['firstname'] . ' ' .  $row['lastname'] . '</textarea>';
0

All Articles