1

我在获取正确的 sql 查询以更新用户配置文件时遇到问题。我使用(基本上)相同的查询来插入数据并且它工作正常(只是没有 WHERE id=clientid 并且在执行数组中没有 clientid。下面的查询不会更新数据库中的任何数据。

我测试并确保所有变量都被发布并且它们是。作为旁注,这个查询对sql注入安全吗?

$conn = new PDO("mysql:host=$DB_HOST;dbname=$DB_DATABASE",$DB_USER,$DB_PASSWORD);

// Deal with the POST variables here...(excluded)

$sql = "UPDATE clients (firstname, lastname, origincountry, dob, gender, email, phone, address, postal, city, province, referred, notes)
        VALUES (:firstname, :lastname, :origincountry, :dob, :gender, :email, :phone, :address, :postal, :city, :province, :referred, :notes)
        WHERE id = :clientid" ;

$q = $conn->prepare($sql);

$q->execute(array(':firstname'=>$firstname,
                  ':lastname'=>$lastname,
                  ':origincountry'=>$origincountry,
                  ':dob'=>$dob,
                  ':gender'=>$gender,
                  ':email'=>$email,
                  ':phone'=>$phone,
                  ':address'=>$address,
                  ':postal'=>$postal,
                  ':city'=>$city,
                  ':province'=>$province,
                  ':referred'=>$referred,
                  ':notes'=>$notes,
                  ':clientid'=>$clientid));
4

1 回答 1

3

您的 SQL 无效。请参阅更新。(感谢@rambocoder 指出这一点)。

使用这个 SQL:

UPDATE clients SET firstname = :firstname, lastname = :lastname, origincountry = :origincountry, dob = :dob, gender = :gender, email = :email, phone = :phone, address = :address, postal = :postal, city = :city, province = :province, referred = :referred, notes = :notes
WHERE id = :clientid
于 2012-08-26T22:39:55.720 回答