Page 1 of 1

insert function

Posted: Tue Dec 02, 2014 12:05 am
by gautamz07
I donot understand this insert function at all , can somebody really please explain this to me ??

Code: Select all

	public function insert($data, $table) {
		
		$columns = "";
		$values = "";
		
		foreach ($data as $column => $value) {
			$columns .= ($columns == "") ? "" : ", ";
			$columns .= $column;
			$values .= ($values == "") ? "" : ", ";
			$values .= $value;
		}
		
		$sql = "insert into $table ($columns) values ($values)";
				
		mysql_query($sql) or die(mysql_error());
		
		//return the ID of the user in the database.
		return mysql_insert_id();
		
	}
:banghead: .

Thanks .

Re: insert function

Posted: Tue Dec 02, 2014 12:45 am
by pbs
this insert function is used to insert data in database table, it accepts two parameters,
first the data, in array format with array key as table column name and value as column value and
second parameter is table name to which you want to insert data