Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Bind Value not inserting in database when updating row

I have a little problem when i bind the value for updating the row. My code is below:

$val="here is a quote's and another's hello ";

$db = new PDO("sqlsrv:server=(local);Database=testdb","root","****");

 $ps = $db->prepare("UPDATE notes_store SET notes=:placeholder WHERE firstname='sameer'
         IF @@ROWCOUNT=0
         INSERT INTO notes_store(firstname,notes) VALUES('sameer',:placeholder)");

            $ps->bindValue(":placeholder", "$val");

            $ps->execute();

3 Answers

You want $val to be interpreted, but the way you have it it is binding the string '$val' to :placeholder. Try this:

$ps->bindValue(":placeholder", $val);

$val is a string so it does not make any difference if we use it like '$val' or simple $val. The problem here is that UPDATE notes_store SET notes=:placeholder WHERE firstname='sameer' works perfectly when used alone and INSERT INTO notes_store(firstname,notes) VALUES('sameer',:placeholder) works perfectly when used alone but when i add condition IF @@ROWCOUNT=0 it does not work.

I am using SQLSRV database.with PDO.

First: turns out you're right about $val not needing quotes. I think I would still leave them off though. It makes your code more readable since most people probably expect that variables won't be surrounded by quotes.

Second: I don't really have any experience with SQLSRV, and I don't really have any way to check this out, but here are a couple of things to try -

  1. Try this query directly in your server's console. Does it work? If not, you can play with your syntax there until you get it right, then copy it into your script. If it does work in the console, and not through PDO, see #2.

  2. PDO isn't completely without it's own strange behavior. It could be that your are just trying to get too much out of one PDO query. You could try splitting it up and implementing the conditional in your script. This worked in MySQL (change the PDO configuration to fit your use case):

$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));

$sqlUpdate = "UPDATE notes_store SET notes=:placeholder WHERE firstname='sameer'";
$sqlInsert = "INSERT INTO notes_store (firstname, notes) VALUES ('sameer', :placeholder)";

$update = $db->prepare($sqlUpdate);
$update->bindValue(":placeholder", $val);
$update->execute();

if($update->rowCount() == 0) {
  $insert = $db->prepare($sqlInsert);
  $insert->bindValue(":placeholder", $val);
  $insert->execute();
}

I can appreciate wanting to only make one call to the database. You can offload some of the processing and make fewer requests. However, this method shouldn't give you much of a performance hit. If the update succeeds, you still only get one transaction on the database. If the update doesn't succeed you get a second transaction; probably not a big deal.