簡體   English   中英

使用PDO和BindParameters將數據從表單插入MYSQL

[英]Inserting data from a form to MYSQL using PDO and BindParameters

我正在嘗試使用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;

?>

詢問“查找語法問題”的問題在這里不合時宜,因為它們產生但答案中的猜測,對未來的讀者沒有幫助。 不幸的是,它永遠不需要足夠的投票就可以解決這個問題。

因此,這是一個猜測。 您嘗試插入的數據變量未定義。

將您的代碼更改為此:

$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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM