douchan4674 2014-12-11 16:50
浏览 109

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

I am trying to post the data from the form field "title", bind it to ":title", INSERT it into sql and then return it to the previous page as part of a JSON string. The insertlastid function gets the id value of the sql row that title is being inserted into. Firebug is saying the id value is being passed successfully but the title is null. I'm suspicious the problem is with this (line 10):

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

Here is my script

    <?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(),
                         );

How could I fix this so 'title' would pass back to the page. Thank you in advance

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀