gpt4 book ai didi

php - 使用php pdo向数据库中插入记录

转载 作者:行者123 更新时间:2023-11-29 07:25:19 27 4
gpt4 key购买 nike

我使用下面的 php pdo 将用户博客文章插入到我的数据库中,但收到这样的错误 fatal error :在线/public_html/postaction.php 中的非对象上调用成员函数prepare() 34 我尝试修复但不断给我不同的错误。有没有其他方法可以做到这一点或解决这个问题正在使用?

<?php
if($_POST) {

$sql = "INSERT INTO blog_post(BID,
blog_title,
blog_body,
comments,
UserName,
Time,
Date,
likes,
ip) VALUES (
:BID,
:blog_title,
:blog_body,
:comments,
:UserName,
:Time,
:Date,
:likes,
:ip)";

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

$stmt->bindParam(':BID', $newId, PDO::PARAM_STR);
$stmt->bindParam(':blog_title', $_POST['blog_title'], PDO::PARAM_STR);
$stmt->bindParam(':blog_body', $_POST['blog_body'], PDO::PARAM_STR);
$stmt->bindParam(':comments', $_POST['comments'], PDO::PARAM_STR);
$stmt->bindParam(':UserName', $_POST['UserName'], PDO::PARAM_STR);
$stmt->bindParam(':Time', $_POST['Time'], PDO::PARAM_STR);
$stmt->bindParam(':Date', $_POST['Date'], PDO::PARAM_STR);
$stmt->bindParam(':likes', $_POST['likes'], PDO::PARAM_STR);
$stmt->bindParam(':ip', $_POST['ips'], PDO::PARAM_STR);

$stmt->execute();
$newId = $pdo->lastInsertId();
header('location: postblog.php?d=1');
}
else if(!isset($_POST)){
header('location: postblog.php?err=1');
echo "Sorry!";
}
else{
header('location: postblog.php?else=1');
}
?>

最佳答案

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

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

// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO table(firstname, lastname, email)
VALUES (:firstname, :lastname, :email)");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':email', $email);

// insert a row
$firstname = "sample";
$lastname = "lastsamp";
$email = "sample@example.com";
$stmt->execute();

echo "New records created successfully";

//handle the rest action here

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

要将其与准备语句一起使用,请尝试此示例

<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";

class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}

function current() {
return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
}

function beginChildren() {
echo "<tr>";
}

function endChildren() {
echo "</tr>" . "\n";
}
}

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
$stmt->execute();

// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>

最后试试这个:

<?php 
define('DB_SERVER', "localhost");
define('DB_USER', "root");
define('DB_PASSWORD', "123");
define('DB_DATABASE', "test");
define('DB_DRIVER', "mysql");

$country = 'Canada';
$capital = 'Ottawa';
$language = 'English & French';

try {
$db = new PDO(DB_DRIVER . ":dbname=" . DB_DATABASE . ";host=" . DB_SERVER, DB_USER, DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $db->prepare("INSERT INTO countries(name, capital, language) VALUES (:country, :capital, :language)");

$stmt->bindParam(':country', $country, PDO::PARAM_STR, 100);
$stmt->bindParam(':capital', $capital, PDO::PARAM_STR, 100);
$stmt->bindParam(':language', $language, PDO::PARAM_STR, 100);

if($stmt->execute()) {
echo '1 row has been inserted';
}

$db = null;
} catch(PDOException $e) {
trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR);
}

?>

或者

$sql = "INSERT INTO movies(filmName,
filmDescription,
filmImage,
filmPrice,
filmReview) VALUES (
:filmName,
:filmDescription,
:filmImage,
:filmPrice,
:filmReview)";

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

$stmt->bindParam(':filmName', $_POST['filmName'], PDO::PARAM_STR);
$stmt->bindParam(':filmDescription', $_POST['filmDescription'], PDO::PARAM_STR);
$stmt->bindParam(':filmImage', $_POST['filmImage'], PDO::PARAM_STR);
// use PARAM_STR although a number
$stmt->bindParam(':filmPrice', $_POST['filmPrice'], PDO::PARAM_STR);
$stmt->bindParam(':filmReview', $_POST['filmReview'], PDO::PARAM_STR);

$stmt->execute();

关于php - 使用php pdo向数据库中插入记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34748662/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com