尝试替换MySQL数据库中的数据

时间:2019-05-13 13:03:28

标签: php mysql

我已经开始构建一个包含客户数据的基本系统,我可以添加和删除行没有问题。我现在添加了一个额外的表格来更新信息。我无法使其正常工作并替换数据,它只是添加了一行新数据。我是PHP和mySQL的新手。我使用MongoDB的时间更长了。 PHP代码如下:

<?php 

$thisId = $_GET['id'];


    $thisClient = "SELECT * FROM clients WHERE id='".$thisId."'";
    $results = mysqli_query($link, $thisClient);

    if (mysqli_num_rows($results)){
        while ($row = mysqli_fetch_array($results)) {
            $client_name = $row['client_name'];
            $client_email = $row['client_email'];
            $client_number = $row['client_number'];
            $business_name = $row['business_name'];
            $business_email = $row['business_email'];
            $business_number = $row['business_number'];
            $address_1 = $row['address_1'];
            $address_2 = $row['address_2'];
            $address_town = $row['address_town'];
            $address_county = $row['address_county'];
            $address_code = $row['address_code'];
            $business_url = $row['business_url'];
            $colours = $row['colours'];
            $notes = $row['notes']; 
        }
    }

    if ($_SERVER["REQUEST_METHOD"]=="POST"){
    include("config.php");

    $client_name     = $_POST['client_name'];
    $client_email    = $_POST['client_email'];
    $client_number   = $_POST['client_number'];
    $business_name   = $_POST['business_name'];
    $business_email  = $_POST['business_email'];
    $business_number = $_POST['business_number'];
    $address_1       = $_POST['address_1'];
    $address_2       = $_POST['address_2'];
    $address_town    = $_POST['address_town'];
    $address_county  = $_POST['address_county'];
    $address_code    = $_POST['address_code'];
    $business_url    = $_POST['business_url'];
    $colours         = $_POST['colours'];
    $notes           = $_POST['notes'];

    $sql = "UPDATE INTO clients SET (client_name, client_email, 
                        client_number, business_name, business_email, 
                        business_number, address_1, address_2, 
                        address_town, address_county, address_code, 
                        business_url, colours, notes) 
            VALUES ('$client_name', '$client_email', 
                    '$client_number', '$business_name', '$business_email', 
                    '$business_number', '$address_1', '$address_2', 
                    '$address_town', '$address_county', '$address_code', 
                    '$business_url', '$colours', '$notes') 
            WHERE id='".$thisId."'";

        if (mysqli_query($link, $sql)) {
              header('Location:/Client');
        } else {
              echo "Error: " . $sql . "<br>" . mysqli_error($link);
        }
        mysqli_close($link);
}

?>

1 个答案:

答案 0 :(得分:1)

您的查询应该是

$sql = "UPDATE clients SET 
            client_name = ?, client_email = ?, client_number = ?, 
            business_name = ?, business_email = ?, business_number = ?, 
            address_1 = ?, address_2 = ?, address_town = ?, 
            address_county = ?, address_code = ?, business_url = ?, 
            colours = ?, notes = ? 
        WHERE id=?";

$statement = $link->prepare($sql);
//s here means string and i indicates integer
$statement->bind_param("ssssssssssssssi",  $client_name, $client_email, 
                        $client_number, $business_name, $business_email, 
                        $business_number, $address_1, $address_2, 
                        $address_town, $address_county, $address_code, 
                        $business_url, $colours, $notes, 
                        $_GET['id']);
$statement->execute();

请仔细阅读here中的文档。