Php 将MySql插入转换为PDO

Php 将MySql插入转换为PDO,php,mysql,pdo,sql-insert,Php,Mysql,Pdo,Sql Insert,我正在尝试将其转换为PDO: echo 'sup 1'; $sql = "INSERT INTO blogData( title, content, category) VALUES ( :title, :content, :category)"; echo 'sup 2';

我正在尝试将其转换为PDO:

echo 'sup 1';                                        
$sql = "INSERT INTO blogData(
        title,
        content,
        category) 
        VALUES ( 
        :title, 
        :content, 
        :category)";
     echo 'sup 2';                                 
$stmt = prepare($sql);
                    echo 'sup 3';                          
$stmt->bindParam(':title', $_POST['title'], PDO::PARAM_STR);       
$stmt->bindParam(':content', $_POST['content'], PDO::PARAM_STR); 
$stmt->bindParam(':category', 'City Secrets', PDO::PARAM_STR);
                               echo 'sup 4';       
$stmt->execute(); 
echo 'sup 5';
      header('location: http://www.backToThePageIPostedOn.com');
这是我的当前代码,但未进入数据库:

$sql = "INSERT INTO blogData(
            title,
            content,
            category) 
            VALUES ( 
            :title, 
            :content, 
            :category)";

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

$stmt->bindParam(':title', $_POST['title'], PDO::PARAM_STR);       
$stmt->bindParam(':content', $_POST['content'], PDO::PARAM_STR); 
$stmt->bindParam(':category', 'City Secrets', PDO::PARAM_STR);

$stmt->execute(); 
 header('location: http://www.backToThePageIPostedOn.com');
它在脚本页面上停止。这是我第一次使用PDO,如果有人能指出我语法中的错误,我将不胜感激

我的代码没有通过
echo'sup2'
所以我相信错误就在这一行,$stmt=
$pdo->prepare($sql)

我按照一个教程来做这件事,我不明白他们为什么要添加
中的$pdo
。 我想这应该是我的联系,但我把它设定为
$con
当我改变
$pdo到$con我在echo'sup 2'仍然得到相同的截止值

语句bindParam方法通过引用接受第二个参数。只有变量可以通过引用传递

解决方案是将要绑定的参数指定给变量:

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

$title = $_POST['title'];
$content = $_POST['content'];
$category = 'City Secrets';

$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':content', $content, PDO::PARAM_STR);
$stmt->bindParam(':category', $category, PDO::PARAM_STR);

$stmt->execute();

这是上述问题的正确工作代码

$stmt->bindParam
改为

 $stmt->bindValue 
并为DB连接添加了
connection.php
文件

<?php                           
require_once( 'connection.php' );

$sql = "INSERT INTO blogData(
            title,
            content,
            category) 
            VALUES ( 
            :title, 
            :content, 
            :category)";

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

$stmt->bindParam(':title', $_POST['title'], PDO::PARAM_STR);       
$stmt->bindParam(':content', $_POST['content'], PDO::PARAM_STR); 
$stmt->bindValue(':category', 'City Secrets', PDO::PARAM_STR);

$stmt->execute(); 

 header('location: http://www.website.com');
?>           


This
$stmt->bindParam(':category',City Secrets',PDO::PARAM_STR)
最有可能是
$stmt->bindValue(':category',City Secrets',PDO::PARAM_STR)不,先生,这没用。错误在这一行,$stmt=$pdo->prepare($sql);它必须是$pdo,但我遵循了一个教程,我不确定应该是什么,然后您需要向我们展示您的pdo连接是什么,将凭据替换为
xxx
启用错误报告和/或检查错误日志以提供实际发生的错误。啊,这是固定的。这是三个问题。谢谢你的帮助!回答这个问题,这样我就可以给你一个cred了。问题在$stmt=$pdo->prepare($sql);你是对的,需要改变,但这不是我的问题。正确的答案是弗雷德在评论中发表的。