Note that there are some explanatory texts on larger screens.

plurals
  1. POdropdown list php
    primarykey
    data
    text
    <p>I have created a profile page in php where a user using an html drop down list chooses gender. After the user chooses gender the form dispatches and saves the data into profile table with the help of a function. All I want is the dropdown list to keep the value selected by user the previous time. For example lets say that user making his profile and selected gender=male. If he wants to update his profile next time visiting the profile page the dropdown list to hold as selected value the "male".</p> <p>Here is my code:</p> <pre><code>&lt;?php if ( isset($_GET['success']) === true &amp;&amp; empty($_GET['success'])===true ){ echo'Profile Updated Sucessfuly'; }else{ if( empty($_POST) === false &amp;&amp; empty($errors) === true ){ $update_data_profile = array('gender' =&gt; $_POST['gender'], 'zip' =&gt; $_POST['zip']); update_user_profile($session_user_id, $update_data_profile); header('Location: profile_update.php?success'); exit(); }else if ( empty($errors) === false ){ echo output_errors($errors); } ?&gt; Gender&amp;nbsp;&lt;select name="gender" id="gender"&gt; &lt;option value=" "&gt; EMPTY &lt;/option&gt; &lt;option value="Male"&gt;Male&lt;/option&gt; &lt;option value="Female"&gt;Female&lt;/option&gt; &lt;/select&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ZIP&lt;input name="zip" type="text" size="15" placeholder="type your ZIP code"/&gt; </code></pre> <p>and this is my function:</p> <pre><code>&lt;?php function update_user_profile($user_id, $update_data_profile){ $result = mysql_query("select user_id from profile where user_id = $user_id limit 1"); if(mysql_num_rows($result) === 1) { $update = array(); array_walk($update_data_profile, 'array_sanitize'); foreach($update_data_profile as $field =&gt; $data ){ if(!empty($data)){ $update[]='`' . $field . '` = \'' . $data . '\''; } } if(isset($update) &amp;&amp; !empty($update)) { mysql_query(" UPDATE `profile` SET " . implode(', ', $update) . " WHERE `user_id` = $user_id ") or die(mysql_error()); } } else { $user_id = $update_data_profile['user_id']; if(count($update_data_profile)) { $columns = array(); $values = array(); foreach($update_data_profile as $field =&gt; $data) { $columns[] = $field; $values[] = $data; } } mysql_query(" INSERT INTO `profile` (" . implode(",", $columns) .") values ('" . implode("','", $values) . "')" ) or die (mysql_error()); } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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