I'll try to be short with this one. Most probably I'm missing something obvious so any input would be greatly appreciated.
SQL structure/columns:
-
id_user
(primary key) income_input
income_select
PHP Code:
<?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->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->uid) {
?>
<form>
<label for="edit-1">Label</label>
<input id="edit-1" maxlength="60" size="60" type="text" value="<?php while($row = mysql_fetch_array($result)){ echo $row['income_input']; } ?>">
<select id="select-edit-1">
<option value="daily" <?php while($row = mysql_fetch_array($result)){ $income_select = $row['income_select']; if ($income_select == 'daily') echo 'selected="selected"'; }?>>Daily</option>
<option value="weekly" <?php while($row = mysql_fetch_array($result)){ $income_select = $row['income_select']; if ($income_select == 'weekly') echo 'selected="selected"'; }?>>Weekly</option>
</select>
</form>
<?php
}
mysql_close($con);
?>
The input value (income_select
) is fetched properly from the database, but the dropdown (income_select
) 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.
I hope the above one makes sense and finally thank you in advance for your help!