Jump to content

PDO update


rahjiggah
Go to solution Solved by rahjiggah,

Recommended Posts

Hello, can someone please let me know if my code here is wrong?

 

Im trying to upgrade old mysql to PDO.

 

Just a simple update command and on a few values if the $variable = "" then make the value null in DB.

 

thank you

try{
$sql1="UPDATE newone
SET corating=:corating,
cotarg=:cotarg,
coclose=:coclose,
ROI=:ROI,
est1=:est1,
est2=:est2,
basicos=:basicos,
mktcap=:mktcap,
coNav=:nav,
WHERE coID=:coID";

$q1 = $dbh->prepare($sql1);
$q1->execute(array(

':corating'=>$corating,
':cotarg'=>$cotarg,
':coclose'=>$coclose,
':ROI'=>$ROI,
':est1'=>isset($est1) ? $est1 : null,
':est2'=> isset($est2) ? $est2 : null,
':basicos'=>$basicos,
':mktcap'=>$mktcap,
':coNav'=>isset($nav) ? $nav : null,
':coID'=>$coID,
));

} catch(PDOException $e) {
  echo 'ERROR: ' . $e->getMessage();
}

Link to comment
Share on other sites

for the pdo statements to throw errors, you must configure your instance of the pdo class to do so, either when you create the connection/instance of the class or via a separate call to the PDO::setAttribute method.

 

the only thing apparent in the posted code is that the php null value the code could be assigning to the array entries isn't the same as a database null keyword and are likely producing query errors.

 

if you are trying to use a database null keyword, it needs to literally be the letters n,u,l, and l (with no quotes of any kind around it). however, when you supply the data via the ->execute() method (rather than binding the parameters), all the values are treated as string data and are enclosed by single-quotes when inserted into the query statement.

Edited by mac_gyver
Link to comment
Share on other sites

hmmmm so you mean the actual sql table would determine if the value is NULL?

 

basically this was to prevent that col from assigning 0.00 value, 0.00 can be a legitmate number for those columns but nothing (ie no value) also shows up as 0.00.

 

how would I set that up in mysql?

Link to comment
Share on other sites

  • Solution

got it 

 

just did a simple if for the variables in question... figured Id post it here in case anyone else has this prob...

 

Thx for the help Guru!

try{
$sql1="UPDATE newone
SET 
est1=:est1,
est2=:est2,
WHERE coID=:coID";

$q1 = $dbh->prepare($sql1);
if ($est1 == ""){
$est1 = null;
}
$q1->bindValue(':est1', $est1);

if ($est2 == ""){
$est2 = null;
}
$q1->bindValue(':est2', $est2);

$q1->execute();

Edited by rahjiggah
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.