我是编程的新手,刚刚从mysql更改为mysqli,但是当我在网上找到我的登录脚本时,它是用PDO编写的。所以现在我就是那个; D
如何更改此php文件以使用PDO更新我的数据库?!
的config.php
<?php
// These variables define the connection information for your 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 to connect to the 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 to ../index.php");
} else {
// get data from 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'
WHERE `objekt_id` = '$valt_objekt_id'";
//this is required for almost every mysqli_* function
$result = mysqli_query($connect, $sql); //the example
//mysqli can update multiple rows at a time
// if successfully updated.
if($result){
echo "Uppdateringen lyckades <br> <a href='../objekt.php'>Gå tillbaka</a>";
} else {
echo mysql_error();
}
}
}
?>
答案 0 :(得分:0)
以下是您脚本的PDO版本:
if($_POST) {
if(empty($_SESSION['user'])) {
header("Location: index.php");
die("Redirecting to ../index.php");
} else {
require("config.php");
// get data from 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
WHERE `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());
}
}
}