Note that there are some explanatory texts on larger screens.

plurals
  1. POIF statement inside WHILE - PHP, MySQL and HTML dropdown select
    primarykey
    data
    text
    <p>I'll try to be short with this one. Most probably I'm missing something obvious so any input would be greatly appreciated.</p> <p><strong>SQL structure/columns:</strong></p> <ol> <li><code>id_user</code> (primary key)</li> <li><code>income_input</code></li> <li><code>income_select</code></li> </ol> <p><strong>PHP Code:</strong></p> <pre><code>&lt;?php $con = mysql_connect("localhost","dbname","pwd"); if (!$con) { die('Could not connect to the database: ' . mysql_error()); } mysql_select_db("dbname", $con); global $user; // Drupal 7 global $userid = $user-&gt;uid; // Drupal 7 currently logged-in user id $result = mysql_query("SELECT * FROM my_table WHERE id_user='$userid'"); if($result === FALSE) { die(mysql_error()); } if ($user-&gt;uid) { ?&gt; &lt;form&gt; &lt;label for="edit-1"&gt;Label&lt;/label&gt; &lt;input id="edit-1" maxlength="60" size="60" type="text" value="&lt;?php while($row = mysql_fetch_array($result)){ echo $row['income_input']; } ?&gt;"&gt; &lt;select id="select-edit-1"&gt; &lt;option value="daily" &lt;?php while($row = mysql_fetch_array($result)){ $income_select = $row['income_select']; if ($income_select == 'daily') echo 'selected="selected"'; }?&gt;&gt;Daily&lt;/option&gt; &lt;option value="weekly" &lt;?php while($row = mysql_fetch_array($result)){ $income_select = $row['income_select']; if ($income_select == 'weekly') echo 'selected="selected"'; }?&gt;&gt;Weekly&lt;/option&gt; &lt;/select&gt; &lt;/form&gt; &lt;?php } mysql_close($con); ?&gt; </code></pre> <p>The input value (<code>income_select</code>) is fetched properly from the database, but the dropdown (<code>income_select</code>) won't return the selected state. If I include the entire form into the WHILE statement it's working fine and I can echo quickly the DB values and the select dropdown "selected" state is working fine too. I would opt for this method but I have to keep the form as it is and only populate it with data from the DB if there is any entry for the currently logged-in user.</p> <p>I hope the above one makes sense and finally thank you in advance for your help!</p>
    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.
    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