使用/ update按钮更新MySQL字段

时间:2015-04-11 18:18:57

标签: php mysql sql-update

我正在尝试更新MySQL表中显示的所有字段。删除功能目前有效,但我没有获得更新功能的任何输出。我觉得我错过了一些非常基本的东西。任何输入都表示赞赏!

    <form action="" method="post">
    <?php
    $con = mysqli_connect($host,$username,$password,$dbname);
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $query = mysqli_query($con,"SELECT * FROM customers");
    while($row = mysqli_fetch_array($query))
    {
    echo "<tr>";
    echo "<td><input type=\"text\" name=\"FirstName\" value=\"" . htmlspecialchars($row['FirstName'], ENT_QUOTES, 'UTF-8') . "\"></td>";
    echo "<td><input type=\"text\" name=\"LastName\" value=\"" . htmlspecialchars($row['LastName'], ENT_QUOTES, 'UTF-8') . "\"></td>";
    echo "<td>" . htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8') . "</td>";   
    echo "<td>" . htmlspecialchars($row['address'], ENT_QUOTES, 'UTF-8') . "</td>"; 
    echo "<td>" . htmlspecialchars($row['phone'], ENT_QUOTES, 'UTF-8') . "</td>"; 
    echo "<td>" . htmlspecialchars($row['product'], ENT_QUOTES, 'UTF-8') . "</td>";
    echo "<td>" . htmlspecialchars($row['firmware'], ENT_QUOTES, 'UTF-8') . "</td>"; 
    echo "<td>" . htmlspecialchars($row['purchase_date'], ENT_QUOTES, 'UTF-8') . "</td>"; 
    echo '<td align="center"><input type="hidden" name="email" value="';
    echo htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8');
    echo '"><input type="submit" name="delete" value="X"> </td>';
    echo "</tr>";
    echo '<tr><td colspan="9"><input type="hidden" name="email" value="';
    echo htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8'); 
    echo '"><input type="submit" name="edit" value="Update"></td></tr>';
    }
    echo "</table>";

?>

<?php
if(isset($_POST['delete'])) {
    $email = $_POST['email'];
    $delete = "DELETE FROM customers WHERE email = '$email'";
    if ($con->query($delete) === TRUE) {
        echo "Record deleted successfully";
    } else {
        echo "Error deleting record: " . $con->error;
}
if (isset($_POST['edit'])) {

    $email = $_POST['email'];
    $FirstName = $_POST['FirstName'];
    $LastName = $_POST['LastName'];
    $edit = "UPDATE customers SET Firstname = '$FirstName', LastName = '$LastName' WHERE email = '$email'";
    if ($con->query($edit) === TRUE) {
    echo $edit;
        echo "Record updated successfully";
        } else {
        echo $edit;
        echo "Error updating record: " . $con->error;
}
}
$conn->close(); 
}  
?>
</form>

1 个答案:

答案 0 :(得分:0)

检查conn上的拼写 试试这个:

<form action="" method="post">
<?php
$conn = mysqli_connect($host,$username,$password,$dbname);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// $query = mysqli_query($conn,"SELECT * FROM customers");
$query = $conn->query("SELECT * FROM customers");
while($row = $query->fetch_assoc())
{
echo "<tr>";
echo "<td><input type=\"text\" name=\"FirstName\" value=\"" . htmlspecialchars($row['FirstName'], ENT_QUOTES, 'UTF-8') . "\"></td>";
echo "<td><input type=\"text\" name=\"LastName\" value=\"" . htmlspecialchars($row['LastName'], ENT_QUOTES, 'UTF-8') . "\"></td>";
echo "<td>" . htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8') . "</td>";   
echo "<td>" . htmlspecialchars($row['address'], ENT_QUOTES, 'UTF-8') . "</td>"; 
echo "<td>" . htmlspecialchars($row['phone'], ENT_QUOTES, 'UTF-8') . "</td>"; 
echo "<td>" . htmlspecialchars($row['product'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['firmware'], ENT_QUOTES, 'UTF-8') . "</td>"; 
echo "<td>" . htmlspecialchars($row['purchase_date'], ENT_QUOTES, 'UTF-8') . "</td>"; 
echo '<td align="center"><input type="hidden" name="email" value="';
echo htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8');
echo '"><input type="submit" name="delete" value="X"> </td>';
echo "</tr>";
echo '<tr><td colspan="9"><input type="hidden" name="email" value="';
echo htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8'); 
echo '"><input type="submit" name="edit" value="Update"></td></tr>';
}
echo "</table>";

?>

<?php
if(isset($_POST['delete'])) {
$email = $_POST['email'];
$delete = "DELETE FROM customers WHERE email = '$email'";
if ($conn->query($delete) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}
if (isset($_POST['edit'])) {

$email = $_POST['email'];
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$edit = "UPDATE customers SET Firstname = '$FirstName', LastName = '$LastName' WHERE email = '$email'";
if ($conn->query($edit) === TRUE) {
echo $edit;
    echo "Record updated successfully";
    } else {
    echo $edit;
    echo "Error updating record: " . $conn->error;
}
}
$conn->close(); 
}  
?>
</form>