如何将以下 INSERT 转换为准备好的语句以确保其安全?

发布于 2024-09-19 12:29:37 字数 1450 浏览 7 评论 0原文

我正在努力理清准备好的陈述。

基本上,我会像平常一样进行插入:

$sql = '
    INSERT INTO customers 
      (customer_first, customer_last, customer_address, customer_email)
    VALUES
      (' . mysql_real_escape_string($_POST['customer_first']) . ', 
      ' . mysql_real_escape_string($_POST['customer_last']) . ', 
      ' . mysql_real_escape_string($_POST['customer_address']) . ', 
      ' . mysql_real_escape_string($_POST['customer_email']) . '  )  
  ';
  mysql_query($sql);

据我所知,但是有一种更安全的方法可以使用准备好的语句来执行此操作。

到目前为止,我认为这样做是这样的:

$stmt = $dbh->prepare("INSERT INTO customers (customer_first, customer_last, customer_address, customer_email) VALUES (:customer_first, :customer_last, :customer_address, :customer_email)");
$stmt->bindParam(':customer_first', $_POST['customer_first']);
$stmt->bindParam(':customer_last', $_POST['customer_last']);
$stmt->bindParam(':customer_address', $_POST['customer_address']);
$stmt->bindParam(':customer_email', $_POST['customer_email']);

$stmt->execute();

这是正确的吗?或者有更好的方法来实现我想要实现的目标吗?如果我尝试上述操作,我会收到错误“调用非对象上的成员函数prepare()” - 这是什么意思?

我正在使用示例@ http://php.net/manual/en /pdo.prepared-statements.php。它指的是 $dbh - 它从哪里得到的? $dbh 是否应该在其他地方引用 - 我假设它与数据库连接有关?

除了这些问题之外,我还可以使用像上面这样的准备好的语句来使用 mysql_insert_id() 或 mysql_error() 之类的东西吗?

I'm trying to get my head around prepared statements.

Basically, I would do a insert like so normally:

$sql = '
    INSERT INTO customers 
      (customer_first, customer_last, customer_address, customer_email)
    VALUES
      (' . mysql_real_escape_string($_POST['customer_first']) . ', 
      ' . mysql_real_escape_string($_POST['customer_last']) . ', 
      ' . mysql_real_escape_string($_POST['customer_address']) . ', 
      ' . mysql_real_escape_string($_POST['customer_email']) . '  )  
  ';
  mysql_query($sql);

From what I've been told however there is a more secure way to do this using Prepared Statements.

So far I think it is done like so:

$stmt = $dbh->prepare("INSERT INTO customers (customer_first, customer_last, customer_address, customer_email) VALUES (:customer_first, :customer_last, :customer_address, :customer_email)");
$stmt->bindParam(':customer_first', $_POST['customer_first']);
$stmt->bindParam(':customer_last', $_POST['customer_last']);
$stmt->bindParam(':customer_address', $_POST['customer_address']);
$stmt->bindParam(':customer_email', $_POST['customer_email']);

$stmt->execute();

Is this correct? Or is there a better way to do what I'm trying to achieve? If I try the above I get an error "Call to a member function prepare() on a non-object" - what does that mean?

I'm using the examples @ http://php.net/manual/en/pdo.prepared-statements.php. It refers to a $dbh - where does it get that from? Is $dbh supposed to be referenced elsewhere - I'm assuming it is something to do with the database connection?

In addition to those questions, can I still use things like mysql_insert_id() or mysql_error() using prepared statements like above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-09-26 12:29:37

如果您是 OOP 新手,您可以从 mysqli 开始,以便立即熟悉功能和语法以及对函数式编程的支持,但值得回顾一下 PDO 类 如果您准备好应对 OOP。

值得回顾一下以帮助决定从哪里开始: mysqli 或 PDO - 什么有什么优点和缺点?

它指的是 $dbh - 它在哪里
从哪里得到的? $dbh 应该是
在其他地方引用 - 我假设是这样
与数据库有关
连接?

$dbh = 数据库句柄(如 PDO > 连接和连接管理< 中定义) /a> 文档)

我还能使用类似的东西吗
mysql_insert_id() 或 mysql_error()
使用像上面这样的准备好的语句吗?

PDO::lastInsertId, PDO::errorInfo

If you are new to OOP, you could start with mysqli for more immediately familiar functionality and syntax and support for functional programming, but it's worth reviewing the PDO class if you're ready to tackle OOP.

Worth reviewing to help decide where to start: mysqli or PDO - what are the pros and cons?

It refers to a $dbh - where does it
get that from? Is $dbh supposed to be
referenced elsewhere - I'm assuming it
is something to do with the database
connection?

$dbh = database handle (as defined in the PDO > Connections and Connection Management doc)

Can I still use things like
mysql_insert_id() or mysql_error()
using prepared statements like above?

PDO::lastInsertId, PDO::errorInfo

网名女生简单气质 2024-09-26 12:29:37

错误消息表明您的问题出在 $dbh 对象上——它是如何初始化的?

The error message indicates your problem is with the $dbh object -- how is it initialized?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文