php - 正确使用 PDO 创建表单以 POST 到 MySQL 中?

标签 php mysql pdo prepared-statement insert-into

我正在尝试使用 PDO 包装类,并且一般来说,为了防止 SQL 注入(inject)的适当安全性,这是一种良好的做法。尝试学习填写表单以 POST 到 MySQL 的基本要素。

因此,在某一时刻,我的表单正在向 MySQL 表中插入数据,但在提交后刷新时会执行多条记录。然后我研究了一种更干净的方法来编写处理器,但现在无法将其插入表中。不确定处理器和“class.db.php”文件之间是否存在差异?

我搜索了很多“操作方法”,但没有成功找到一致的答案。试图了解我做错了什么,希望得到最佳实践的答案。我所看到的一切都在 map 上。

这是我现在的位置: 作为引用,我首先从这里开始 http://webdevelopingcat.com/php-mysql-tutorial-for-beginners-inserting-rows-with-pdo/

如果你用谷歌搜索,那么在我要包含的文档顶部,https://code.google.com/p/php-pdo-wrapper-class/项目作为类实现的基础。

<?php
include("class.db.php");
$version = "1.0.2";
$released = "December 9, 2010";
?>

然后在体内形成一个简单的形式。

<?php
if ( empty( $_POST ) ){
?>

<form name='registration' action='success.php' method='POST'/>
<label for 'FName'>First Name: </label>
<input type="text" name="FName" />

<label for 'LName'>Last Name: </label>
<input type="text" name="LName" />

<label for 'Age'>Age: </label>
<input type="number" name="Age" />

<label for 'Gender'>Gender: </label>
<input type="text" name="Gender" />

<button type="submit">Submit</button>
</form>

最后,表单处理器也在体内。

<?php
} else {
//process the form here
//
// Connect to database
$db = new db("mysql:host=localhost;dbname=pdodb", "root", "root");

$form = $_POST;
$first = $form[ 'FName' ];
$last = $form[ 'LName' ];
$myage = $form[ 'Age' ];
$gen = $form[ 'Gender' ];

$sql = "INSERT INTO mytable ( FName, LName, Age, Gender ) VALUES ( :first, :last, :myage, :gen )";

$query = $db->prepare( $sql );
$query->execute( array( ':first'=>$first, ':last'=>$last, ':myage'=>$myage, ':gen'=>$gen ) );

}
?>

手动方式有效。引用了 culttt.com 帖子:prevent-php-sql-injection-with-pdo-prepared-statements

// Create array of data to insert
$insert = array(
"FName" => "John",
"LName" => "Doe",
"Age" => 26,
"Gender" => "male"
);
// Insert the array into the table
$db->insert("mytable", $insert);

最佳答案

您的表单将发布到 success.php,因此请确保插入代码位于 success.php 文件中:

<?php
// Get POST data
$first = (!empty($_POST['FName']) ? $_POST['FName'] : '');
$last = (!empty($_POST['LName']) ? $_POST['LName'] : '');
$myage = (!empty($_POST['Age']) ? $_POST['Age'] : '');
$gen = (!empty($_POST['Gender']) ? $_POST['Gender'] : 0);

try {
    // Connect to db
    $db = new db('mysql:dbname=pdodb;host=localhost', 'root', 'root');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Set SQL
    $sql = 'INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:first, :last, :myage, :gen)';
    // Prepare query
    $query = $db->prepare($sql);
    // Execute query
    $query->execute(array(':first' => $first, ':last' => $last, ':myage' => $myage, ':gen' => $gen));
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}

谢谢

安德鲁

关于php - 正确使用 PDO 创建表单以 POST 到 MySQL 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27298539/

相关文章:

php - CodeIgniter 3 实现 Promise

php - 如何将 PHP 输出插入 html div 替换临时图形

php - 如何避免在查询中包含子句?

php - 按位置重定向页面

php - 如何一次轻松地插入、更新或删除记录

php - 通过 PHP 将 CSV 文件导入 SQLite 数据库

mysql - 如何将 ORDER 和 LIMIT 应用于 IN() 子句中的每个子查询?

php - PDO 事务不工作

php - 收到错误 'Call to undefined method PDOStatement::store_result()'

php - 如何获取mysql中的所有行仅在两个表中相同的列值