php - How to change mysql_connect into PDO. Updating form -
i'm new programming , changed mysql mysqli, when found login script on net written pdo. i'm onto ;d
how can change php file use pdo update database?!
config.php
<?php // these variables define connection information mysql database $username = "usr"; $password = "pass"; $host = "host"; $dbname = "databasee"; $options = array(pdo::mysql_attr_init_command => 'set names utf8'); try { $db = new pdo("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); } catch(pdoexception $ex){ die("failed connect database: " . $ex- >getmessage());} $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); $db->setattribute(pdo::attr_default_fetch_mode, pdo::fetch_assoc); header('content-type: text/html; charset=utf-8'); session_start(); ?>
php:
<?php if($_post) { $connect = mysqli_connect('host', 'username', 'password', 'dbname'); require("config.php"); if(empty($_session['user'])) { header("location: index.php"); die("redirecting ../index.php"); } else { // data model form meny.php $valt_objekt_id = mysqli_escape_string($connect, strip_tags($_post['valt_objekt_id'])); $valt_objekt_nummer = mysqli_escape_string($connect, strip_tags($_post['valt_objekt_nummer'])); $valt_objekt_alias = mysqli_escape_string($connect, strip_tags($_post['valt_objekt_alias'])); $valt_objekt_leverans = mysqli_escape_string($connect, strip_tags($_post['valt_objekt_leverans'])); $valt_objekt_adress = mysqli_escape_string($connect, strip_tags($_post['valt_objekt_adress'])); // update database $sql = "update `objekt` set `objekt_nummer` = '$valt_objekt_nummer', `objekt_alias`= '$valt_objekt_alias', `objekt_leverans` = '$valt_objekt_leverans', `objekt_adress` = '$valt_objekt_adress' `objekt_id` = '$valt_objekt_id'"; //this required every mysqli_* function $result = mysqli_query($connect, $sql); //the example //mysqli can update multiple rows @ time // if updated. if($result){ echo "uppdateringen lyckades <br> <a href='../objekt.php'>gå tillbaka</a>"; } else { echo mysql_error(); } } } ?>
here pdo version of script:
if($_post) { if(empty($_session['user'])) { header("location: index.php"); die("redirecting ../index.php"); } else { require("config.php"); // data model form meny.php $valt_objekt_id = $_post['valt_objekt_id']; $valt_objekt_nummer = $_post['valt_objekt_nummer']; $valt_objekt_alias = $_post['valt_objekt_alias']; $valt_objekt_leverans = $_post['valt_objekt_leverans']; $valt_objekt_adress = $_post['valt_objekt_adress']; // update database $sql = "update `objekt` set `objekt_nummer` = :objekt_nummer, `objekt_alias`= :objekt_alias, `objekt_leverans` = :objekt_leverans, `objekt_adress` = :objekt_adress `objekt_id` = :objekt_id"; $stmt = $db->prepare($sql); $result = stmt->execute(array(':objekt_nummer' => $valt_objekt_nummer, ':objekt_alias' => $valt_objekt_alias, ':objekt_leverans' => $valt_objekt_leverans, ':objekt_adress' = $valt_objekt_adress, ':objekt_id' => $valt_objekt_id )); if($result){ echo "uppdateringen lyckades <br> <a href='../objekt.php'>gå tillbaka</a>"; } else { print_r($db->errorinfo()); } } }
learn more pdo prepared statments
Comments
Post a Comment