【问题标题】:Inserting data from a form to MYSQL using PDO and BindParameters使用 PDO 和 BindParameters 将数据从表单插入到 MYSQL
【发布时间】:2015-12-03 16:24:52
【问题描述】:

我正在尝试使用 PDO 和绑定参数将数据从表单插入到 MYSQL 数据库。我首先尝试在没有 PDO 和 bindParam 的情况下进行插入,并且成功地从表单中获取数据并插入到数据库中,但是该基本方法对 SQL 注入是开放的。现在我正在使用以下(参见下面的代码)PDO 和 bindParam 方法,但数据没有被插入到我的数据库中。

问题:我做错了什么?是否存在一些不允许我将数据插入数据库的语法问题?

<?php

$username = 'username'; 
$password = 'pass'; 
$host = 'localhost'; 
$dbname = 'db';


try {
 $dbh = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
 // set the PDO error mode to exception
 $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $stmt = $dbh->prepare("INSERT INTO table1 (issue, time, comments, lat, lng) VALUES (:issue, :time, :comments, :lat, :lng)");
    $stmt->bindParam(':issue', $issue);
    $stmt->bindParam(':time', $time);
    $stmt->bindParam(':comments', $comments);
    $stmt->bindParam(':lat', $lat);
    $stmt->bindParam(':lng', $lng);

    $stmt->execute();

        header("Location: main.php");

 }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$dbh = null;



?>

编辑:(仍然不工作)

<?php

$username = 'username';
$password = 'pass';
$host = 'localhost';
$dbname = 'db';


try {
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if (!isset($issue, $time, $comments, $lat, $lng)) {
    die('data set error;');
}
$stmt = $dbh->prepare("INSERT INTO table1 (issue, time, comments, lat, lng) VALUES (:issue, :time, :comments, :lat, :lng)");


$params = array(':issue' => $issue,
    ':time' => $time,
    ':comments' => $comments,
    ':lat' => $lat,
    ':lng' => $lng);

if (!$stmt->execute($params)) {
    print_r($stmt->errorInfo());
    die();
}

header("Location: main.php");
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
$dbh = null;

?>

【问题讨论】:

  • 你在$issue哪里插入数据?
  • 试试echo $issue; 看看字段里有没有值。

标签: php mysql pdo


【解决方案1】:

询问“查找语法问题”的问题在这里是题外话,因为它们产生的只是答案中的猜测,对未来的读者没有帮助。不幸的是,解决这样一个问题从来不需要足够的选票。

所以,这里是猜测。您尝试插入的数据变量未定义。

【讨论】:

  • 查看已编辑的问题。它可能不是语法(我不应该这样说)。
【解决方案2】:

将您的代码更改为:

$username = 'username';
$password = 'pass';
$host = 'localhost';
$dbname = 'db';


try {
    $dbh = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if (!isset($issue, $time, $comments, $lat, $lng)) {
        die('data set error;');
    }
    $stmt = $dbh->prepare("INSERT INTO table1 (issue, time, comments, lat, lng) VALUES (:issue, :time, :comments, :lat, :lng)");


    $params = array(':issue' => $issue,
        ':time' => $time,
        ':comments' => $comments,
        ':lat' => $lat,
        ':lng' => $lng);

    if (!$stmt->execute($params)) {
        print_r($stmt->errorInfo());
        die();
    }

    header("Location: main.php");
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$dbh = null;

【讨论】:

  • 我试过这个。但是数据仍然没有被插入到我的数据库中
  • 我对此很陌生。我是否应该检查 Web 控制台中的错误(我正在使用 firebug)?如果是这样,则没有错误,但数据未显示在我的 MySQL 数据库中
猜你喜欢
  • 2013-10-01
  • 2016-06-03
  • 1970-01-01
  • 2017-10-20
  • 2013-11-17
  • 2014-04-17
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
相关资源
最近更新 更多