<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.pgsql.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.pg-insert.php',
    1 => 'pg_insert',
    2 => 'Insert array into table',
  ),
  'up' => 
  array (
    0 => 'ref.pgsql.php',
    1 => 'PostgreSQL Functions',
  ),
  'prev' => 
  array (
    0 => 'function.pg-host.php',
    1 => 'pg_host',
  ),
  'next' => 
  array (
    0 => 'function.pg-last-error.php',
    1 => 'pg_last_error',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pgsql/functions/pg-insert.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.pg-insert" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pg_insert</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pg_insert</span> &mdash; <span class="dc-title">
   Insert array into table
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.pg-insert-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>pg_insert</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="class.pgsql-connection.php" class="type PgSql\Connection">PgSql\Connection</a></span> <code class="parameter">$connection</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$table_name</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$values</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-exec">PGSQL_DML_EXEC</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="class.pgsql-result.php" class="type PgSql\Result">PgSql\Result</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>pg_insert()</strong></span> inserts the values
   of <code class="parameter">values</code> into the table specified
   by <code class="parameter">table_name</code>.
  </p>
  <p class="para">
   If <code class="parameter">flags</code> is specified,
   <span class="function"><a href="function.pg-convert.php" class="function">pg_convert()</a></span> is applied to
   <code class="parameter">values</code> with the specified flags.
  </p>
  <p class="para">
   By default <span class="function"><strong>pg_insert()</strong></span> passes raw values.
   Values must be escaped or the <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-escape">PGSQL_DML_ESCAPE</a></code></strong> flag
   must be specified in <code class="parameter">flags</code>.
   <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-escape">PGSQL_DML_ESCAPE</a></code></strong> quotes and escapes parameters/identifiers.
   Therefore, table/column names become case sensitive.
  </p>
  <p class="para">
   Note that neither escape nor prepared query can protect LIKE query,
   JSON, Array, Regex, etc. These parameters should be handled
   according to their contexts. i.e. Escape/validate values.
  </p>
 </div>


<div class="refsect1 parameters" id="refsect1-function.pg-insert-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">connection</code></dt>
     <dd>
      <p class="para">An <span class="classname"><a href="class.pgsql-connection.php" class="classname">PgSql\Connection</a></span> instance.</p>
     </dd>
    
    
     <dt><code class="parameter">table_name</code></dt>
     <dd>
      <p class="para">
       Name of the table into which to insert rows.  The table <code class="parameter">table_name</code> must at least
       have as many columns as <code class="parameter">values</code> has elements.
      </p>
     </dd>
    
    
     <dt><code class="parameter">values</code></dt>
     <dd>
      <p class="para">
       An <span class="type"><a href="language.types.array.php" class="type array">array</a></span> whose keys are field names in the table <code class="parameter">table_name</code>,
       and whose values are the values of those fields that are to be inserted.
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       Any number of <strong><code>PGSQL_CONV_OPTS</code></strong>,
       <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-no-conv">PGSQL_DML_NO_CONV</a></code></strong>,
       <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-escape">PGSQL_DML_ESCAPE</a></code></strong>,
       <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-exec">PGSQL_DML_EXEC</a></code></strong>,
       <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-async">PGSQL_DML_ASYNC</a></code></strong> or
       <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-string">PGSQL_DML_STRING</a></code></strong> combined. If <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-string">PGSQL_DML_STRING</a></code></strong> is part of the
       <code class="parameter">flags</code> then query string is returned. When <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-no-conv">PGSQL_DML_NO_CONV</a></code></strong>
       or <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-escape">PGSQL_DML_ESCAPE</a></code></strong> is set, it does not call <span class="function"><a href="function.pg-convert.php" class="function">pg_convert()</a></span> internally.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.pg-insert-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> on success or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.. Or returns a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> on success if <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-string">PGSQL_DML_STRING</a></code></strong> is passed
   via <code class="parameter">flags</code>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.pg-insert-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   A <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is thrown when the specified table is invalid.
  </p>
  <p class="para">
   A <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> or <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> is thrown
   when the value or type of field does not match properly with a PostgreSQL&#039;s type.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.pg-insert-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.3.0</td>
      <td>
       Now throws a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> error when the specified table is invalid;
       previously an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was emitted.
      </td>
     </tr>

     <tr>
      <td>8.3.0</td>
      <td>
       Now throws a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> or <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> error
       when the value or type of field does not match properly with a PostgreSQL&#039;s type;
       previously an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was emitted.
      </td>
     </tr>

     <tr>
 <td>8.1.0</td>
 <td>
  Returns an <span class="classname"><a href="class.pgsql-result.php" class="classname">PgSql\Result</a></span> instance now;
  previously, a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> was returned.
 </td>
</tr>

     <tr>
 <td>8.1.0</td>
 <td>
  The <code class="parameter">connection</code> parameter expects an <span class="classname"><a href="class.pgsql-connection.php" class="classname">PgSql\Connection</a></span>
  instance now; previously, a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> was expected.
 </td>
</tr>

    </tbody>
   
  </table>

 </div>

 
 <div class="refsect1 examples" id="refsect1-function.pg-insert-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>pg_insert()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php <br />  $dbconn </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'dbname=foo'</span><span style="color: #007700">);<br />  </span><span style="color: #FF8000">// This is safe somewhat, since all values are escaped.<br />  // However PostgreSQL supports JSON/Array. These are not<br />  // safe by neither escape nor prepared query.<br />  </span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbconn</span><span style="color: #007700">, </span><span style="color: #DD0000">'post_log'</span><span style="color: #007700">, </span><span style="color: #0000BB">$_POST</span><span style="color: #007700">, </span><span style="color: #0000BB">PGSQL_DML_ESCAPE</span><span style="color: #007700">);<br />  if (</span><span style="color: #0000BB">$res</span><span style="color: #007700">) {<br />      echo </span><span style="color: #DD0000">"POST data is successfully logged\n"</span><span style="color: #007700">;<br />  } else {<br />      echo </span><span style="color: #DD0000">"User must have sent wrong inputs\n"</span><span style="color: #007700">;<br />  }<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.pg-insert-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.pg-convert.php" class="function" rel="rdfs-seeAlso">pg_convert()</a> - Convert associative array values into forms suitable for SQL statements</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>