skurai Posted March 19, 2010 Share Posted March 19, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/ Share on other sites More sharing options...
skurai Posted March 19, 2010 Author Share Posted March 19, 2010 stupid me, i forgot to say which variable is not being stored. The photo_id is not being stored. the id is incrementing, the author, created, and body all work, but photo_id will not store. Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/#findComment-1028628 Share on other sites More sharing options...
PFMaBiSmAd Posted March 19, 2010 Share Posted March 19, 2010 Have you echoed the query in $sql; so that you can see exactly what it is? Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/#findComment-1028643 Share on other sites More sharing options...
skurai Posted March 19, 2010 Author Share Posted March 19, 2010 INSERT INTO comments (id, photo_id, created, author, body) VALUES ('', '', '2010-03-19 13:18:38', 'ttt', 'ttt') that is the result of the $new_comment->create() Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/#findComment-1028689 Share on other sites More sharing options...
skurai Posted March 19, 2010 Author Share Posted March 19, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/#findComment-1028692 Share on other sites More sharing options...
Psycho Posted March 19, 2010 Share Posted March 19, 2010 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" Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/#findComment-1028695 Share on other sites More sharing options...
skurai Posted March 19, 2010 Author Share Posted March 19, 2010 $comment->photographid = 7; I changed the variable to 7 just for testing purposes, and it still does not add it. The query does not show it either. Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/#findComment-1028698 Share on other sites More sharing options...
skurai Posted March 19, 2010 Author Share Posted March 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/#findComment-1028701 Share on other sites More sharing options...
Psycho Posted March 19, 2010 Share Posted March 19, 2010 OK, good thing you got it. I was about to write a post calling you an idiot for not reading my explanation. Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/#findComment-1028708 Share on other sites More sharing options...
skurai Posted March 19, 2010 Author Share Posted March 19, 2010 haha, well right after i posted my response, it refreshed the page and I saw your edit. My bad Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/#findComment-1028735 Share on other sites More sharing options...
Psycho Posted March 19, 2010 Share Posted March 19, 2010 haha, well right after i posted my response, it refreshed the page and I saw your edit. My bad OK, I'll let it slide . . . this time! Quote Link to comment https://forums.phpfreaks.com/topic/195810-all-but-one-variable-being-stored-in-mysql/#findComment-1028741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.