Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Because you're not inserting ALL columns, you need to dynamically build an insert statement that will specify the columns you're inserting into.</p> <p>First, create an array of the columns you want to use. Use this both to generate your form and to retrieve the values</p> <pre><code>$exclude = array("id", "nameandsurname", "departument", "phone", "computer", "date"); $result = mysql_query("SHOW COLUMNS FROM employees") or die(mysql_error()); $columns = array(); while ($row = mysql_fetch_array($result)) { if (!in_array($row[0], $exclude) { $columns[] = $row[0]; } } </code></pre> <p>Render your form from the <code>$columns</code> array:</p> <pre><code>foreach ($columns as $column) { echo '&lt;tr&gt;&lt;td bgcolor="#ddddff"&gt;'.$column.'&lt;br /&gt;'; if (stripos($column, "privileges") !== false) { echo '&lt;p&gt;&lt;a class="hint" href="#"&gt; &lt;input type="text" name="'.$column.'"&gt; &lt;span&gt;Privileges like "occupation" or "like someone"&lt;/span&gt;&lt;/a&gt;'; } else { echo '&lt;select name="'.$column.'"&gt; &lt;option value = "No"&gt;No &lt;option value = "Yes"&gt;Yes &lt;/select&gt;'; } echo '&lt;/td&gt;&lt;/tr&gt;'; } </code></pre> <p>Then, dynamically build your INSERT string from the posted values for those columns. Be sure to protect against SQL injection:</p> <pre><code>$keys = array(); $values = array(); foreach ($columns as $column) { $value = trim($_POST[$column]); $value = mysql_real_escape_string($value); $keys[] = "`{$column}`"; $values[] = "'{$value}'"; } $query = "INSERT INTO 'employees' (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ");"; </code></pre> <p>Note: this will work better if you select from <code>INFORMATION_SCHEMA.COLUMNS</code> so that you can know the type of column you're inserting into. That way, you won't have to quote everything.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload