如何在为PDO绑定参数后使用该参数

时间:2014-12-11 16:50:30

标签: php

我正在尝试从表单字段" title"中发布数据,将其绑定到":title",将其插入到sql中,然后将其作为一部分返回到上一页一个JSON字符串。 insertlastid函数获取正在插入标题的sql行的id值。 Firebug表示 id 值正在成功传递,但标题为空。我怀疑这个问题(第10行):

$title = $conn->$_POST['title'];

这是我的剧本

    <?php
    //Connection 1
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("INSERT INTO listings (title) VALUES (:title)");
    //Bind
          $stmt->bindParam(':title', $_POST['title']); 
          $stmt->execute();
          $id = $conn->lastInsertId();
          $title = $conn->$_POST['title'];
    //if title is not null, then set the value of $showmsg to true
            $conn = null;
    //create private class
     class CropAvatar 
     {
          private $id;
          private $title;
    //add to construct
           function __construct($id, $title) 
           {
            $this -> setId($id);
            $this -> setTitle($title);
            $this -> crop($this -> id, $this -> title);
           }
    //set variables
              public function setId($id) 
              {
                $this->id = $id;
              }
              public function setTitle($title) 
              {
                $this->title = $title;
              }

    //*****Whole bunch of stuff here to work with variables*******
                public function getId() 
                {
                   return $this -> id;
                }
                public function getTitle() 
                {
                   return $this -> title;
                }
    //close class
     }
    //set up JSON response
        $crop = new CropAvatar($_POST['avatar_id'], $_POST['avatar_title']);
        $response = array(
    //a state of 200 will make the element on the response page, visible
                           'state' => 200,
                           'id' => $crop -> getId(),
                           'title' => $crop -> getTitle(),
                         );

我怎么能解决这个问题&#39;标题&#39;会传回页面。提前谢谢

0 个答案:

没有答案