简体   繁体   中英

PDO MYSQL update not working

I am having problems getting an sql query correct to update user profiles. I use (basically) the same query to INSERT the data and it works fine (just without the WHERE id=clientid and without clientid in the execute array. The query below does not update any data in the database.

I tested and made sure that all the variables are being posted and they are. As a sidenote, is this query safe from sql injection?

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

Your SQL is invalid. See UPDATE . (thanks to @rambocoder for pointing that out).

Use this 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM