Jump to content

All but one variable being stored in MySql


skurai

Recommended Posts

I am just tinkering around with OOP in PHP and have been making a photo gallery and I am having an issue I am pulling my hair out over. Everything works except for the comment function.

This is the comment class:

class Comment extends DatabaseObject {

    protected static $table_name = "comments";
    protected static $db_fields=array('id', 'photo_id', 'created', 'author', 'body');
    
    public $id;
    public $photographid;
    public $created;
    public $author;
    public $body;
    
    public static function make($photoid, $author="Anonymous", $body=""){
        if(!empty($photoid) && !empty($author) && !empty($body)){
            
        $comment = new Comment();
        
        $comment->photographid = (int)$photoid;
        $comment->created = strftime("%Y-%m-%d %H:%M:%S", time());
        $comment->author = $author;
        $comment->body = $body;
        return $comment;
        } else {
            return false;
        }
    }

And here is the code for when they ADD the comment:

if(isset($_POST['submit'])){
        $author = trim($_POST['author']);
        $body = trim($_POST['body']);
        $photo_id = $_GET['id'];
        
        $new_comment = Comment::make($photo_id, $author, $body);
        if($new_comment && $new_comment->create()){
            // Succeed 
            $message = "Comment successfully added";
        } else {
            // Failed
            $message = "There was an error.";
        }

 

and finally this is the "Create" function in the parent class:

    public function create(){
        global $database;
        $attributes = $this->s_attributes();
        $sql = "INSERT INTO " . static::$table_name . " (";
        $sql .= join(", ", array_keys($attributes));
        $sql .= ") VALUES ('";
        $sql .= join("', '", array_values($attributes));
        $sql .= "')";
        if($database->query($sql)){
            $this->id = $database->insert_id();
            return true;
        } else {
            return false;
        }

 

I use the create() function for a bunch of other things and it works fine, s_attributes just sets the attributes key and value, but like i said it works with all other times i use it except for this time.

 

I have even gone through and instead of printing "Success" it prints $new_comment->photographid  and it prints the correct ID, but it is still being store i nthe table as "0"

 

table fields are : id(int) AI, photo_id(int), created(DATETIME), author(varchar), body(text).

 

Any help with this would be extremely appreciated. THank you

I went ahead and edited for testing purposes another of the classes that uses create(), and put in a col in the table just for a number to reflect the photo_id, and submitted a random number the same way I am calling create() here, and it adds the number fine, so the attributes(), s_attributes() and create() seems to be working fine.

 

where else should i look?

 

PS. For reference, i am including the attributes() and s_attributes()

    protected function attributes(){
        $attributes = array();
        foreach(static::$db_fields as $field){
            $attributes[$field] = $this->$field;
        }
        return $attributes;
    }
    protected function s_attributes(){
        global $database;
        $clean_attributes = array();
        foreach($this->attributes() as $key => $value){
            $clean_attributes[$key] = $database->escape_value($value);
        }
        return $clean_attributes;
        
    }

Could it be because of this:

        $comment->photographid = (int)$photoid; //<====
        $comment->created = strftime("%Y-%m-%d %H:%M:%S", time());
        $comment->author = $author;
        $comment->body = $body;

 

You are defining a variable $comment->photographid, and it may be looking for $comment->photo_id. Not sure how you are building the array of values, but if it is pairing up index names with variable names it seems there is a mismatch.

 

Also, FYI: if the id field is an auto-increment, there is no need to include it in your INSERT query

 

EDIT: Yes, I think that is the problem, look at this

    protected function attributes(){
        $attributes = array();
        foreach(static::$db_fields as $field){
            $attributes[$field] = $this->$field;
        }        return $attributes;
    }

 

The $db_fields includes

array('id', 'photo_id', 'created', 'author', 'body');

 

But, your defined fields don't include a $this->photo_id

 

Change the references for "photographid" to "photo_id"

Beautiful! thank you. Guess i needed someone else to look at it after i stare at it for a while i would miss something like that.

 

For anyone who has similar trouble,

    protected static $db_fields=array('id', 'photo_id', 'created', 'author', 'body');
    
    public $id;
    public $photo_id;
    public $created;
    public $author;
    public $body;
    public $picture;
    
    public static function make($photo_id, $author="Anonymous", $body=""){
        if(!empty($photo_id) && !empty($author) && !empty($body)){
            
        $comment = new Comment();
        
        $comment->photo_id = $photo_id;
        $comment->created = strftime("%Y-%m-%d %H:%M:%S", time());
        $comment->author = $author;
        $comment->body = $body;
        return $comment;

 

Thank you again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.