mikeb
mikeb

Reputation: 727

Extracting from database to textarea

I'm comparing current data with updated data to check whether there are changes in information, and add the changes to a new table changes:

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 the changes such as date user ... And put them in a 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...

Upvotes: 4

Views: 158

Answers (2)

gabe3886
gabe3886

Reputation: 4265

From your comment, you have the following code :

$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();

To get the information into a text area in the way you are doing, use:

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

Upvotes: 0

aorcsik
aorcsik

Reputation: 15552

Without knowing much about your data (for example what AFNumber is) I would suggest simply querying everything from the Changes table, and displaying them 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 simply use echo here, but using some template system shouldn't make much difference to the core concept of the solution.

Upvotes: 3

Related Questions