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