Jump to content


putIntTableRow


  • You cannot reply to this topic
1 reply to this topic

#1 PaulD

    Likes Etomite Forums!

  • Developers
  • PipPip
  • 389 posts
  • Gender:Male

Posted 16 February 2009 - 05:22 AM

This function is used to write a row into any table on the etomite database including tables you have added yourself. For tables on other databases you need putExtTableRow.

From the documentation

Quote

putIntTableRow($fields="", $into="")

// function to put a row into ANY internal database table
// INSERT's a new table row into ANY internal Etomite database table. No data validation is performed.
// $fields = a $key=>$value array: $fields=("name"=>$name,"email"=$email,"age"=>$age)
// $into = name of the internal Etomite table which will receive the new data row without database name or table prefix: $into="user_messages"
// Returns FALSE on failure.

Example usage
Here I am writing a fictional row of two fields to a table I created on the database etomite is installed on, called etomite_my_table_name (etomite_ being the default prefix for etomite installs. Notice in the function call it is not used).

// example fields I recieved from, say, a form.
$example = 'hello';
$test = 4;

// write my fields to an array called params, 
// so it can be used in the function call
$params = compact("example","test");

// write fields to database
// if putIntTableRow fails it will return false. 
// so 'if not false', writes error message.
// my_table_name is the name of my 
// internal table without any prefix (if used)

if (!$etomite->putIntTableRow($fields=$params, $into="my_table_name")){

	// fail
	// didnt write it to database for some reason
	$error .= 'Error message';
	return $error;
	die;
}
else	{
	// success
	// do something here
}

Needless to say, but I should say, that fields should be well validated before writing to the database.

Paul.

PS It is possible to use the extra field $addPrefix=true or $addPrefix=false in this function call, to add (or not add) the table prefix.

Edited by PaulD, 26 February 2009 - 12:51 AM.


#2 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 16 February 2009 - 11:17 AM

From the index.php (etomite v1.1):
function putIntTableRow($fields="", $into="", $addPrefix=true) {
  // function to put a row into ANY internal database table
  // INSERT's a new table row into ANY internal Etomite database table. No data validation is performed.
  // $fields = a $key=>$value array: $fields=("name"=>$name,"email"=$email,"age"=>$age)
  // $into = name of the internal Etomite table which will receive the new data row without database name or table prefix: $into="user_messages"
  // $addPrefix = whether to check for and/or add $this->dbConfig['table_prefix'] to the table name
  // Returns FALSE on failure.
	if(($fields=="") || ($into=="")){
	  return false;
	} else {
	  $tbl = ($this->dbConfig['table_prefix'] != ''
			  && strpos($from,$this->dbConfig['table_prefix']) === 0
			  || !$addPrefix)
			  ? $this->dbConfig['dbase'].".".$into
			  : $this->db.$into;
	  $sql = "INSERT INTO $tbl SET ";
	  foreach($fields as $key=>$value) {
		$sql .= "`".$key."`=";
		if (is_numeric($value)) $sql .= $value.",";
		else $sql .= "'".$value."',";
	  }
	  $sql = rtrim($sql,",");
	  $sql .= ";";
	  $result = $this->dbQuery($sql);
	  return $result;
	}
  }

Therefore, yes, Paul, you can add the $addPrefix to this function in the latest version.

Note also that if $fields or $into are blank, it returns false. However, if you try to insert a record that makes the INSERT INTO ($sql) fail, you will get the ugly red error message that is difficult to suppress, not "false".

Also note that as the "key" is passed in the query, it is automatically enclosed in `backticks`, and if the value is numeric, it is passed as is (as it should be) and non-numeric values are enclosed in "quotes".

Edited by Cris D., 16 February 2009 - 11:19 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users