Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to have a HTML SELECT/OPTION value as NULL using PHP?
    primarykey
    data
    text
    <p>The following is a snippet from my HTML form which pulls the options from the table rows accordingly.</p> <p>What I want to do is have the first option value to be NULL so when no choice is made, NULL is entered when the form is submitted to the database.</p> <pre><code> &lt;td&gt;Type&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;select name="type_id" id="type_id" class="form"&gt; &lt;option value=""&gt;&lt;/option&gt; &lt;?php $sql = mysql_query("SELECT type_id, type FROM $tbl_add_type") or die(mysql_error()); while ($row = mysql_fetch_array($sql)) { echo "&lt;option value=".$row['type_id']."&gt;" . $row['type'] . "&lt;/option&gt;"; } ?&gt; &lt;/select&gt;*&lt;/td&gt; </code></pre> <p>Is this possible? Or could someone suggest an easier/better way of doing this?</p> <p>Thanks</p> <p>Update: Thanks for the answer, I'm using the method detailed below to convert it to NULL.</p> <pre><code>if ($_POST['location_id'] === '') { $_POST['location_id'] = 'NULL'; } </code></pre> <p>However when trying to use this NULL value with the following query it does not work.</p> <pre><code>UPDATE addresses SET location_id='NULL' WHERE ipid = '4791' </code></pre> <p>I know it needs to be <code>location_id=NULL</code> instead but don't know how to do this...</p> <p>Update 2: this is how my queries work:</p> <pre><code>if ($_POST['location_id'] === '') { $_POST['location_id'] = 'NULL'; // or 'NULL' for SQL } $notes=isset($_POST['notes']) ? mysql_real_escape_string($_POST['notes']) : ''; //$location_id=isset($_POST['location_id']) ? mysql_real_escape_string($_POST['location_id']) : ''; $ipid=isset($_POST['ipid']) ? mysql_real_escape_string($_POST['ipid']) : ''; $sql="UPDATE addresses SET notes='$notes', location_id='$location_id' WHERE ipid = '$ipid'"; mysql_query($sql) or die(mysql_error()); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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