PHP OOP Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zubair1
    New Member
    • Sep 2008
    • 79

    PHP OOP Help

    Hello,

    I just recently started to switch to OOP in PHP, but am getting confused on almost every step :(

    Right now, my current issue is i have this function:

    Code:
    	public function create() {
    		global $database;
    		$attributes = $this->sanitized_attributes();
    
    		$sql = "INSERT INTO ".self::$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 am using a another function (method) to grab all the attributes i have set for the database fields, but when i use the above code - it tells me the first and second values are not valid integers.

    I know that maybe because its adding those '' (single quotes) there but i am not sure how i can make this function work without modifying anything outside of this function?

    also, if possible could some one provide to me or point me to a place that explains about instantiate?

    I am instantiating my array to objects from within the class but wanting to know if there is an easier method to do so my current code is below:

    Code:
    	private static function instantiate($record) {
    		$object = new self;
    
    		foreach($record as $attribute=>$value){
    		  if($object->has_attribute($attribute)) {
    		    $object->$attribute = $value;
    		  }
    		}
    
    		return $object;
    	}
    Thank you,
    Regards
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    could you post the full error message?

    your code is confusing me (i.e. I don’t have enough information to knock sense in the problem, it would be great, if you could elaborate it)

    Comment

    • zubair1
      New Member
      • Sep 2008
      • 79

      #3
      Originally posted by Dormilich
      could you post the full error message?

      your code is confusing me (i.e. I don’t have enough information to knock sense in the problem, it would be great, if you could elaborate it)
      Sorry,

      The above Create() method is within my my class it submits data into the database.

      The error i received is "Invalid Integer Value for column name userid" - PHP doesn't send that error MySQL does from query.

      I need a better way to write the query, currently right now if you look at my above function Create(), it is joining them but also adding ' (single quotes) when it joins them.

      The first db field is userid and that field needs an integer value but the join i am using above is adding those single quotes even to the integer value.

      I want it to write the query as it does but ignore adding '' single quotes for integer values since that makes it invalid for the SQL.

      P.S :- you can ignore the other function Instantiate (i'll ask about it later after i resolved this other issue).

      Hope that makes better sense now.

      Comment

      Working...