Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change from mysql to pdo using prepared statements in PHP?

Tags:

php

pdo

$dml = "insert into bookmark(accountId,category,url,hash,title,created) value($_SESSION[accountId],$_POST[category],'$_POST[url]',md5('$_POST[url]'),'$_POST[title]',now())";

mysql_query($dml,$con);

How do I do this statement using prepared statements in PDO?

like image 467
user198729 Avatar asked Dec 11 '25 01:12

user198729


2 Answers

$dml = "INSERT INTO bookmark (accountId, category, url, hash, title, created) "
    . "VALUES (:accountId, :category, :url, MD5(:url), :title, NOW())";
$statement = $pdo->prepare($dml);
$parameters = array(
    ":accountId" => $_SESSION["accountId"],
    ":category" => $_POST["category"],
    ":url" => $_POST["url"],
    ":title" => $_POST["title"]);
$statement->execute($parameters);
like image 105
Adrian Avatar answered Dec 13 '25 13:12

Adrian


$dml = $db->prepare("INSERT INTO bookmark (accountId, category, url, hash, title, created) VALUES (:account_id, :category, :url, MD5(:url), :title, NOW());");

$dml->bindParam(':account_id', $_SESSION['accountId']);
$dml->bindParam(':category', $_POST['category']);
$dml->bindParam(':url', $_POST['url']);
$dml->bindParam(':title', $_POST['title']);

$dml->execute();
like image 24
Brock Batsell Avatar answered Dec 13 '25 15:12

Brock Batsell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!