6

我的代码中有一些旧的 mysql_query 查询,我想将其转换为 PDO,但正在努力工作。

我的原始代码是:

mysql_query("UPDATE people SET price='$price', contact='$contact', fname='$fname', lname='$lname' WHERE id='$id' AND username='$username' ")
or die(mysql_error()); 

现在我正在尝试:

$sql = "UPDATE people SET price='$price', contact='$contact', fname='$fname', lname='$lname' WHERE id='$id' AND username='$username'";
$q   = $conn->query($sql) or die("failed!");

但似乎无法让它工作,有什么想法吗?

更新代码:

$conn = new PDO("mysql:host=$host;dbname=$db",$user,$pass);


 // check if the form has been submitted. If it has, process the form and save it to the   database
 if (isset($_POST['submit']))
 { 
 // confirm that the 'id' value is a valid integer before getting the form data
 if (is_numeric($_POST['id']))
  {
 // get form data, making sure it is valid
 $id = $_POST['id'];
 $fname = mysql_real_escape_string(htmlspecialchars($_POST['fname']));
 $lname = mysql_real_escape_string(htmlspecialchars($_POST['lname']));
 $contact = mysql_real_escape_string(htmlspecialchars($_POST['contact']));
 $price = mysql_real_escape_string(htmlspecialchars($_POST['price']));


 // check that firstname/lastname fields are both filled in
 if ($fname == '' || $lname == '' || $contact == '' || $price == '' )
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 //error, display form
 renderForm($id, $fname, $lname, $contact, $price, $error);
 }
 else
 {
 // save the data to the database
 $username = $_SESSION['username'];

 $query = "UPDATE people 
         SET price=?, 
             contact=?, 
             fname=?, 
             lname=? 
          WHERE id=? AND 
                username=?";
$stmt = $db->prepare($query);
$stmt->bindParam(1, $price);
$stmt->bindParam(2, $contact);
$stmt->bindParam(3, $fname);
$stmt->bindParam(4, $lname);
$stmt->bindParam(5, $id);
$stmt->bindParam(6, $username);    
$stmt->execute();


 // once saved, redirect back to the view page
header("Location: view.php"); 
}
4

4 回答 4

6

有关更多信息,请访问此链接:PHP PDO

根据你的例子,

<?php

    $query = "UPDATE people 
             SET price=?, 
                 contact=?, 
                 fname=?, 
                 lname=? 
              WHERE id=? AND 
                    username=?";
    $stmt = $dbh->prepare($query);
    $stmt->bindParam(1, $price);
    $stmt->bindParam(2, $contact);
    $stmt->bindParam(3, $fname);
    $stmt->bindParam(4, $lname);
    $stmt->bindParam(5, $id);
    $stmt->bindParam(6, $username);    
    $stmt->execute();

?>

PDO 准备好的语句和存储过程

在此处输入图像描述

于 2012-09-07T00:32:33.933 回答
5

请注意,在使用 PDO 的 mysql 驱动程序时,您始终必须禁用模拟的准备好的语句

$dbConnection = new PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'pass');

$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = 'UPDATE people SET';
$sql.= ' price = :price,';
$sql.= ' contact = :contact,';
$sql.= ' fname = :fname,';
$sq;.= ' lname = :lname';
$sql.= ' WHERE id= :id AND username = :username';

$stmt = $pdo->prepare($sql);

$stmt->execute(array(
    ':price' => $price,
    ':contact' => $contact,
    ':fname' => $fname,
    ':lname' => $lname,
    ':id' => $id,
    ':username' => $username,
));

正如你所看到的,我使用了命名参数,因为当你有很多它们时,你在做什么会更清楚。

注意:ircmaxell目前正在努力让默认值始终使用真正的准备好的语句,但在此之前(可能需要一些时间)你总是必须为 mysql 禁用它们。

于 2012-09-07T00:37:35.503 回答
4

如果您要使用 PDO,则需要查看,prepare()否则execute您将失去 PDO 提供的安全性并保留 SQL 注入。所以,给出你的例子:

$conn = new PDO(/*connection info*/);

$query = $conn->prepare("UPDATE people "
                      . "SET    price    = :price, "
                      . "       contact  = :contact, "
                      . "       fname    = :fname, "
                      . "       lname    = :lname "
                      . "WHERE  id       = :id "
                      . "  AND  username = :username");
$result = $query->execute(array(
  ':price'    => $price,
  ':contact'  => $contact,
  ':fname'    => $fname,
  ':lname'    => $lname,
  ':id'       => $id,
  ':username' => $username
));

这更像是一种松散的方式,但您也bindParam可以明确说明它所期望的数据类型。

于 2012-09-07T00:37:54.883 回答
4

在使用 PDO 扩展时,您需要明确的几件事是有多种方法可以完成工作。

您当前使用的方式是其中之一,包括更多。然而,单独绑定参数总是一个好主意,因为这可以防止许多问题,如 SQL 注入等等。

其他需要注意的重要事项statementprepareexecute

$conn = new PDO("...."); //Creating the handler

//Create the statement
$stmt = $conn -> prepare("UPDATE people SET price = :price, contact = :contact, fname = :fname, lname = :lname WHERE id= :id AND username = :username");

// Bind the params
$stml -> bindParam(":contact", $contact, PDO::PARAM_STR); //This way you can also define the DATATYPE of the parameter

//Execute
$stmt -> execute(array(
   ":price" => $price, //another way of binding the params
   ":fname" => $fname, 
   ":lname" => $lname,
   ":id" => $id, 
   ":username" => $username));
于 2012-09-07T00:48:17.157 回答