从数据库提取到textarea

时间:2015-07-03 09:18:15

标签: php html sql

我正在将当前数据与更新数据进行比较,以检查信息是否有变化,并将更改添加到新表更改中:

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";
    }

}

现在我想提取有关更改的信息,例如日期用户......并将它们放在这种形式的textarea标签中:

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

但我不知道怎么做,请帮忙...

2 个答案:

答案 0 :(得分:3)

在不了解您的数据的情况下(例如AFNumber是什么),我建议您只需查询Changes表中的所有内容,并以所需的格式显示它们:

$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>";
}

要显示数据,我只需在此处使用echo,但使用某些模板系统不应对解决方案的核心概念产生太大影响。

答案 1 :(得分:0)

根据您的评论,您有以下代码:

cordova plugin add android

要按照您的方式将信息输入文本区域,请使用:

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