Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help rewriting MySQL update to proper PDO

php mysql pdo mysql update

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 zak

zak

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 24 January 2016 - 01:44 PM

Hi 
 
Can someone please help with rewriting this MySQL update I tried a few different methods and getting all kinds of errors. I'm new to php and trying to learn I rewritten the insert portion just having issues with update.
 
 
 
Previous working MySQL update:

$sqlStatment = "UPDATE products SET "
. "catid=" . $_REQUEST['cat']
. ", cattxt='" . $catText
. "', ptitle='" . $_REQUEST['ptitle']
. "', saleoffer='" . $_REQUEST['saleoffer']
. "', url='" . $_REQUEST['url']
. "', oldprice=" . $_REQUEST['oprice']
. ", nprice=" . $_REQUEST['nprice']
. ", availability=" . $_REQUEST['availability']
. ", shortdesc = :shortdescription"
. ", pdescription = :description"
. ", last_modified = NOW()"
. " WHERE id=" . $_REQUEST['pid'];

try{
$query = $db->prepare($sqlStatment);
$query->execute(array(':shortdescription'=>$_REQUEST['shortdescription'], ':description'=>$_REQUEST['description']));
}
catch(PDOException $e) {
die($e->getMessage());
}

 
New MySQL update I tried:
 

$catid = $_REQUEST['cat'];
$cattxt = $_REQUEST['cattxt'];
$ptitle = $_REQUEST['ptitle'];
$saleoffer = $_REQUEST['saleoffer'];
$url = $_REQUEST['url'];
$oldprice = $_REQUEST['oprice'];
$nprice = $_REQUEST['nprice'];
$availability = $_REQUEST['availability'];
$shortdesc = $_REQUEST['shortdescription'];
$pdescription = $_REQUEST['description'];
$id = $_REQUEST["id"];
 
try {
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = $pdo->prepare('UPDATE `products` SET `cat` = ?, `cattxt` = ?, `ptitle` = ?, `saleoffer` = ?, `url` = ?, `tag` = ?, `oprice` = ?, `nprice` = ?, `availability` = ?, `shortdescription` = ?, `description` = ?, WHERE `id` = ?');
    $sql->execute(array($catid, $catText, $ptitle, $saleoffer, $url, $tag, $oldprice, $nprice, $availability, $shortdesc, $pdescription, $pid));
} catch(PDOException $e) {
    echo $e->getMessage();
}

I know its wrong just been at it for a bit and getting tired of looking at it please help me so i can learn from my mistake. 

 

 



#2 adammary

adammary

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 24 January 2016 - 11:06 PM

Is there a special reason why you choose to use MYSQLi rather than PDO? Web Designing Training in Chennai  |   Web Designing Training in Chennai






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download