PHP SQL更新现有记录

时间:2014-12-31 18:08:17

标签: php mysql sql sql-update updates

我正在尝试从表中提取现有sql记录的网页,并使用表单更新这些记录中的空列。我似乎无法更新这些记录以在表中实际更新。我一直在打猎,我尝试过的一切都行不通。我相信我遇到的问题是让它从表中获取id,并使用它来更新logs表。我只是学习PHP和SQL,所以我完全有可能忽略一些非常愚蠢的东西。非常感谢任何帮助。

$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('there was a problem connecting to the database' . mysql_error());

$sql = "
SELECT Part
     , Lot
     , Qty
     , AnodTemp
     , Amp
     , SealTemp
     , PerformedBy
     , DateTimePerformed
     , FinalAnodThickness
     , QtyPass
     , CheckedBy
     , DateTimeChecked
     , id  
  FROM logs
";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $unapproved = $row['CheckedBy'];
        if($unapproved == null) {
            echo "<br><br><br> Part: " . $row['Part']. " / Lot: " . $row['Lot']. " / Qty: " . $row['Qty']. " / AnodTemp: " . $row['AnodTemp']. " / Amp: " . $row['Amp']. " / SealTemp: " . $row['SealTemp']. " / PerformedBy: " . $row['PerformedBy']. " / ID: " . $row['id']; ?>

        <form action="index.php" method="post">
            Final Anod Thickness:<br>
            <input type="text" name="FinalAnodThickness">
            <br><br>
            Qty Pass:<br>
            <input type="text" name="QtyPass">
            <br><br>
            <input type="submit" id="submit" value="Submit" name="submit">
            <br><br>
        </form> 
        _____________________________________________________________________
        <?php

            if (isset($_POST['submit'])){
                $FinalAnodThickness= $_POST['FinalAnodThickness'];
                $QtyPass= $_POST['QtyPass'];
                $CheckedBy= $_SESSION['CheckedBy'];

                $sql = "UPDATE logs SET FinalAnodThickness = $FinalAnodThickness WHERE EXISTS (SELECT id FROM logs)";
            }
        }
    }
}
else {
    echo "0 results";
}

$conn->close(); ?>

1 个答案:

答案 0 :(得分:1)

您只需要执行更新语句:

$sql = "UPDATE logs SET FinalAnodThickness = $FinalAnodThickness WHERE EXISTS (SELECT id FROM logs)";
if ($conn->query($sql) === TRUE) {
    echo "Records updated successfully";
} else {
    echo "Error updating records: " . $conn->error;
}