PDO passing the bindValue parameter

Asked

Viewed 258 times

1

I have the following parameter in my query UPDATE.

$consulta->bindValue(':foto', $foto, PDO::PARAM_STR);

What I would like to do is the following: If the variable $foto come blank, how could I pass the same value I have in the database?

I ask this because as the variable is coming blank it is doing the update in the field with no value.

  • Check whether $foto is empty, if you are not pass, do so: if (!empty($foto)) { $consulta->bindValue(':foto', $foto, PDO::PARAM_STR); } else { // $foto está vazio }

  • if you do not use the error parameter pq it is in the update clause

  • could make a select, but I believe there is another way to pass the same value of the field without needing to recover before.

  • Here’s what I do: when loading the data for change, create a field hidden with the name oldfoto when you receive the information for change make a simple if. If photo has nothing pass again to oldphoto. Just one point, paste your html on the screen!

  • Virgilio Novic, I have already thought about this tbm, but I don’t like to use the field hiden much because it can open security breach in the application, in case the user gives an F12 just fill the field.

  • 1

    Can’t this parameter be optional? Have you tried using COALESCE to indicate an alternative value? I don’t know if this is your case, but here has an example that can be adaptable.

  • stderr, PERFECT! I had forgotten about Coalesce, I did it this way and it worked out! photo=COALESCE(:photo, photo)

  • @William If possible post a reply and mark it as resolved!

Show 3 more comments

1 answer

1


I ended up using the mysql function that our friend @stderr posted in the comments.

foto=COALESCE(:foto, foto) 

If the parameter comes blank the COALESCE assigns a default value to the field, which in my case is the same value that is already saved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.