jaydeesmalls Posted June 28, 2008 Share Posted June 28, 2008 Hi everyone, I have this form that will populate the field based on what's in the db. My problem is, some of the fields will populate from the db, others will not. I have no clue why. These work. The field will populate based on what's already in the db table. And if I make changes, the changes will also populate once submitted. <?php $host="xxxxxx"; // Host name $username="xxxx"; // Mysql username $password="xxxxx1"; // Mysql password $db_name="xxxx"; // Database name $tbl_name="xxxx"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); ?> <form action="proluc.php" method="post"> <h2>Coverage Date</h2> <?php $id ='1'; //Day of week $results = mysql_query("SELECT up1 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <select name="dayofweek"> <option value="Monday"<?php if ($values['up1'] == "Monday") echo 'selected="selected"'; ?>>Monday</option> <option value="Tuesday"<?php if ($values['up1'] == "Tuesday") echo 'selected="selected"'; ?>>Tuesday</option> <option value="Wednesday"<?php if ($values['up1'] == "Wednesday") echo 'selected="selected"'; ?>>Wednesday</option> <option value="Thursday"<?php if ($values['up1'] == "Thursday") echo 'selected="selected"'; ?>>Thursday</option> <option value="Friday"<?php if ($values['up1'] == "Friday") echo 'selected="selected"'; ?>>Friday</option> </select> <?php $id ='1'; //Month $results = mysql_query("SELECT up2 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <select name="month"> <option value="January"<?php if ($values['up2'] == "January") echo 'selected="selected"'; ?>>January</option> <option value="February"<?php if ($values['up2'] == "February") echo 'selected="selected"'; ?>>February</option> <option value="March"<?php if ($values['up2'] == "March") echo 'selected="selected"'; ?>>March</option> <option value="April"<?php if ($values['up2'] == "April") echo 'selected="selected"'; ?>>April</option> <option value="May"<?php if ($values['up2'] == "May") echo 'selected="selected"'; ?>>May</option> <option value="June"<?php if ($values['up2'] == "June") echo 'selected="selected"'; ?>>June</option> <option value="July"<?php if ($values['up2'] == "July") echo 'selected="selected"'; ?>>July</option> <option value="August"<?php if ($values['up2'] == "August") echo 'selected="selected"'; ?>>August</option> <option value="September"<?php if ($values['up2'] == "September") echo 'selected="selected"'; ?>>September</option> <option value="October"<?php if ($values['up2'] == "October") echo 'selected="selected"'; ?>>October</option> <option value="November"<?php if ($values['up2'] == "November") echo 'selected="selected"'; ?>>November</option> <option value="December"<?php if ($values['up2'] == "December") echo 'selected="selected"'; ?>>December</option> </select> <?php $id ='1'; //Date $results = mysql_query("SELECT up3 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <select name="date"> <option value="1"<?php if ($values['up3'] == "1") echo 'selected="selected"'; ?>>1</option> <option value="2"<?php if ($values['up3'] == "2") echo 'selected="selected"'; ?>>2</option> <option value="3"<?php if ($values['up3'] == "3") echo 'selected="selected"'; ?>>3</option> <option value="4"<?php if ($values['up3'] == "4") echo 'selected="selected"'; ?>>4</option> <option value="5"<?php if ($values['up3'] == "5") echo 'selected="selected"'; ?>>5</option> <option value="6"<?php if ($values['up3'] == "6") echo 'selected="selected"'; ?>>6</option> <option value="7"<?php if ($values['up3'] == "7") echo 'selected="selected"'; ?>>7</option> <option value="8"<?php if ($values['up3'] == "8") echo 'selected="selected"'; ?>>8</option> <option value="9"<?php if ($values['up3'] == "9") echo 'selected="selected"'; ?>>9</option> <option value="10"<?php if ($values['up3'] == "10") echo 'selected="selected"'; ?>>10</option> <option value="11"<?php if ($values['up3'] == "11") echo 'selected="selected"'; ?>>11</option> <option value="12"<?php if ($values['up3'] == "12") echo 'selected="selected"'; ?>>12</option> <option value="13"<?php if ($values['up3'] == "13") echo 'selected="selected"'; ?>>13</option> <option value="14"<?php if ($values['up3'] == "14") echo 'selected="selected"'; ?>>14</option> <option value="15"<?php if ($values['up3'] == "15") echo 'selected="selected"'; ?>>15</option> <option value="16"<?php if ($values['up3'] == "16") echo 'selected="selected"'; ?>>16</option> <option value="17"<?php if ($values['up3'] == "17") echo 'selected="selected"'; ?>>17</option> <option value="18"<?php if ($values['up3'] == "18") echo 'selected="selected"'; ?>>18</option> <option value="19"<?php if ($values['up3'] == "19") echo 'selected="selected"'; ?>>19</option> <option value="20"<?php if ($values['up3'] == "20") echo 'selected="selected"'; ?>>20</option> <option value="21"<?php if ($values['up3'] == "21") echo 'selected="selected"'; ?>>21</option> <option value="22"<?php if ($values['up3'] == "22") echo 'selected="selected"'; ?>>22</option> <option value="23"<?php if ($values['up3'] == "23") echo 'selected="selected"'; ?>>23</option> <option value="24"<?php if ($values['up3'] == "24") echo 'selected="selected"'; ?>>24</option> <option value="25"<?php if ($values['up3'] == "25") echo 'selected="selected"'; ?>>25</option> <option value="26"<?php if ($values['up3'] == "26") echo 'selected="selected"'; ?>>26</option> <option value="27"<?php if ($values['up3'] == "27") echo 'selected="selected"'; ?>>27</option> <option value="28"<?php if ($values['up3'] == "28") echo 'selected="selected"'; ?>>28</option> <option value="29"<?php if ($values['up3'] == "29") echo 'selected="selected"'; ?>>29</option> <option value="30"<?php if ($values['up3'] == "30") echo 'selected="selected"'; ?>>30</option> <option value="31"<?php if ($values['up3'] == "31") echo 'selected="selected"'; ?>>31</option> </select> <?php $id ='1'; //Year $results = mysql_query("SELECT up4 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <select name="year"> <option value="2008"<?php if ($values['up4'] == "2008") echo 'selected="selected"'; ?>>2008</option> <option value="2009"<?php if ($values['up4'] == "2009") echo 'selected="selected"'; ?>>2009</option> <option value="2010"<?php if ($values['up4'] == "2010") echo 'selected="selected"'; ?>>2010</option> <option value="2011"<?php if ($values['up4'] == "2011") echo 'selected="selected"'; ?>>2011</option> <option value="2012"<?php if ($values['up4'] == "2012") echo 'selected="selected"'; ?>>2012</option> </select><br /> <br /> <!--Begin Reminders--> <h2>Reminders</h2> <?php $id ='2'; //Reminder 1 $results = mysql_query("SELECT rem1 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <input type="text" name="rem1" size="100" maxlength="100" value="<?php echo $values['rem1']; ?>"/><input type="hidden" name="textid" value="<?php echo $id; ?>" /><br /> <?php $id ='2'; //Reminder 2 $results = mysql_query("SELECT rem2 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <input type="text" name="rem2" size="100" maxlength="100" value="<?php echo $values['rem2']; ?>"/><input type="hidden" name="textid" value="<?php echo $id; ?>" /><br /> <?php $id ='2'; //Reminder 3 $results = mysql_query("SELECT rem3 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <input type="text" name="rem3" size="100" maxlength="100" value="<?php echo $values['rem3']; ?>"/><input type="hidden" name="textid" value="<?php echo $id; ?>" /><br /> <?php $id ='2'; //Reminder 4 $results = mysql_query("SELECT rem4 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <input type="text" name="rem4" size="100" maxlength="100" value="<?php echo $values['rem4']; ?>"/><input type="hidden" name="textid" value="<?php echo $id; ?>" /><br /> <?php $id ='3'; //Reminder 5 $results = mysql_query("SELECT rem1 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <input type="text" name="rem5" size="100" maxlength="100" value="<?php echo $values['rem1']; ?>"/><input type="hidden" name="textid" value="<?php echo $id; ?>" /><br /> <?php $id ='3'; //Reminder 6 $results = mysql_query("SELECT rem2 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <input type="text" name="rem6" size="100" maxlength="100" value="<?php echo $values['rem2']; ?>"/><input type="hidden" name="textid" value="<?php echo $id; ?>" /> <font size="-1" color="#6633CC">Update 1</font><br /> <?php $id ='3'; //Reminder 7 $results = mysql_query("SELECT rem3 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <input type="text" name="rem7" size="100" maxlength="100" value="<?php echo $values['rem3']; ?>"/><input type="hidden" name="textid" value="<?php echo $id; ?>" /> <font size="-1" color="#FF0000">Update 2</font><br /> <!--End Reminder --> And this part does not work. I can submit the changes, and the answers get submitted to the database, but if I refresh the page after I've submitted the data, the fields appear blank. <?php $id ='10'; //Day of week $results = mysql_query("SELECT p1 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <select name="cm1a"> <option value="Monday"<?php if ($values['p1'] == "Monday") echo $values['p1']; ?>>Monday</option> <option value="Tuesday"<?php if ($values['p1'] == "Tuesday") echo $values['p1']; ?>>Tuesday</option> <option value="Wednesday"<?php if ($values['p1'] == "Wednesday") echo $values['p1']; ?>>Wednesday</option> <option value="Thursday"<?php if ($values['p1'] == "Thursday") echo $values['p1']; ?>>Thursday</option> <option value="Friday"<?php if ($values['p1'] == "Friday") echo $values['p1']; ?>>Friday</option> </select> <?php $id ='10'; //Case Manager 1 $results = mysql_query("SELECT act FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <select name="cm1act"> <option value=" "<?php if ($values['act'] == " ") echo 'selected="selected"'; ?>> </option> <option value="(C)"<?php if ($values['act'] == "(C)") echo 'selected="selected"'; ?>>(C)</option> <option value="(H)"<?php if ($values['act'] == "(H)") echo 'selected="selected"'; ?>>(H)</option> <option value="(T)"<?php if ($values['act'] == "(T)") echo 'selected="selected"'; ?>>(T)</option> </select> <?php $id ='10'; //Case Manager 1 $results = mysql_query("SELECT p2 FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> <select name="cm1b"> <option value=" "<?php if ($values['p2'] == " ") echo 'selected="selected"'; ?>> </option> <option value="Annie"<?php if ($values['p2'] == "Annie") echo 'selected="selected"'; ?>>Annie</option> <option value="Antonio"<?php if ($values['p2'] == "Antonio") echo 'selected="selected"'; ?>>Antonio</option> <option value="Beverly"<?php if ($values['p2'] == "Beverly") echo 'selected="selected"'; ?>>Beverly</option> <option value="Celia"<?php if ($values['p2'] == "Celia") echo 'selected="selected"'; ?>>Celia</option> <option value="Charlene"<?php if ($values['p2'] == "Charlene") echo 'selected="selected"'; ?>>Charlene</option> <option value="Dennis"<?php if ($values['p2'] == "Dennis") echo 'selected="selected"'; ?>>Dennis</option> <option value="Elaine"<?php if ($values['p2'] == "Elaine") echo 'selected="selected"'; ?>>Elaine</option> <option value="Elizabeth"<?php if ($values['p2'] == "Elizabeth") echo 'selected="selected"'; ?>>Elizabeth</option> <option value="Erica"<?php if ($values['p2'] == "Erica") echo 'selected="selected"'; ?>>Erica</option> <option value="Faye"<?php if ($values['p2'] == "Faye") echo 'selected="selected"'; ?>>Faye</option> <option value="Gladys"<?php if ($values['p2'] == "Gladys") echo 'selected="selected"'; ?>>Gladys</option> <option value="Grace"<?php if ($values['p2'] == "Grace") echo 'selected="selected"'; ?>>Grace</option> <option value="Jennifer"<?php if ($values['p2'] == "Jennifer") echo 'selected="selected"'; ?>>Jennifer</option> <option value="Karl"<?php if ($values['p2'] == "Karl") echo 'selected="selected"'; ?>>Karl</option> <option value="Kimberly"<?php if ($values['p2'] == "Kimberly") echo 'selected="selected"'; ?>>Kimberly</option> <option value="Miriam"<?php if ($values['p2'] == "Miriam") echo 'selected="selected"';?>>Miriam </option> <option value="Ross"<?php if ($values['p2'] == "Ross") echo 'selected="selected"'; ?>>Ross</option> <option value="Seth"<?php if ($values['p2'] == "Seth") echo 'selected="selected"'; ?>>Seth</option> <option value="Shana"<?php if ($values['p2'] == "Shana") echo 'selected="selected"'; ?>>Shana</option> </select><br> <?php $id ='10'; //Case Manager 1 Extra $results = mysql_query("SELECT extra FROM $tbl_name WHERE id='$id'"); $values = mysql_fetch_array($results); ?> I know the coding isn't pretty - I'm really new. But my concern for now is the functionality and making this work. Quote Link to comment https://forums.phpfreaks.com/topic/112379-solved-fields-sometimes-populating-from-db-but-not-always/ Share on other sites More sharing options...
.josh Posted June 28, 2008 Share Posted June 28, 2008 Not really the issue here, but why do you have: <select name="cm1a"> <option value="Monday"<?php if ($values['p1'] == "Monday") echo $values['p1']; ?>>Monday</option> <option value="Tuesday"<?php if ($values['p1'] == "Tuesday") echo $values['p1']; ?>>Tuesday</option> <option value="Wednesday"<?php if ($values['p1'] == "Wednesday") echo $values['p1']; ?>>Wednesday</option> <option value="Thursday"<?php if ($values['p1'] == "Thursday") echo $values['p1']; ?>>Thursday</option> <option value="Friday"<?php if ($values['p1'] == "Friday") echo $values['p1']; ?>>Friday</option> </select> You echo the vars instead of echoing 'selected="selected" like on your other ones. Quote Link to comment https://forums.phpfreaks.com/topic/112379-solved-fields-sometimes-populating-from-db-but-not-always/#findComment-576969 Share on other sites More sharing options...
papaface Posted June 28, 2008 Share Posted June 28, 2008 Also you may want to look into duplicate code that is not required: e.g <select name="date"> <option value="1"<?php if ($values['up3'] == "1") echo 'selected="selected"'; ?>>1</option> <option value="2"<?php if ($values['up3'] == "2") echo 'selected="selected"'; ?>>2</option> <option value="3"<?php if ($values['up3'] == "3") echo 'selected="selected"'; ?>>3</option> <option value="4"<?php if ($values['up3'] == "4") echo 'selected="selected"'; ?>>4</option> <option value="5"<?php if ($values['up3'] == "5") echo 'selected="selected"'; ?>>5</option> <option value="6"<?php if ($values['up3'] == "6") echo 'selected="selected"'; ?>>6</option> <option value="7"<?php if ($values['up3'] == "7") echo 'selected="selected"'; ?>>7</option> <option value="8"<?php if ($values['up3'] == "8") echo 'selected="selected"'; ?>>8</option> <option value="9"<?php if ($values['up3'] == "9") echo 'selected="selected"'; ?>>9</option> <option value="10"<?php if ($values['up3'] == "10") echo 'selected="selected"'; ?>>10</option> <option value="11"<?php if ($values['up3'] == "11") echo 'selected="selected"'; ?>>11</option> <option value="12"<?php if ($values['up3'] == "12") echo 'selected="selected"'; ?>>12</option> <option value="13"<?php if ($values['up3'] == "13") echo 'selected="selected"'; ?>>13</option> <option value="14"<?php if ($values['up3'] == "14") echo 'selected="selected"'; ?>>14</option> <option value="15"<?php if ($values['up3'] == "15") echo 'selected="selected"'; ?>>15</option> <option value="16"<?php if ($values['up3'] == "16") echo 'selected="selected"'; ?>>16</option> <option value="17"<?php if ($values['up3'] == "17") echo 'selected="selected"'; ?>>17</option> <option value="18"<?php if ($values['up3'] == "18") echo 'selected="selected"'; ?>>18</option> <option value="19"<?php if ($values['up3'] == "19") echo 'selected="selected"'; ?>>19</option> <option value="20"<?php if ($values['up3'] == "20") echo 'selected="selected"'; ?>>20</option> <option value="21"<?php if ($values['up3'] == "21") echo 'selected="selected"'; ?>>21</option> <option value="22"<?php if ($values['up3'] == "22") echo 'selected="selected"'; ?>>22</option> <option value="23"<?php if ($values['up3'] == "23") echo 'selected="selected"'; ?>>23</option> <option value="24"<?php if ($values['up3'] == "24") echo 'selected="selected"'; ?>>24</option> <option value="25"<?php if ($values['up3'] == "25") echo 'selected="selected"'; ?>>25</option> <option value="26"<?php if ($values['up3'] == "26") echo 'selected="selected"'; ?>>26</option> <option value="27"<?php if ($values['up3'] == "27") echo 'selected="selected"'; ?>>27</option> <option value="28"<?php if ($values['up3'] == "28") echo 'selected="selected"'; ?>>28</option> <option value="29"<?php if ($values['up3'] == "29") echo 'selected="selected"'; ?>>29</option> <option value="30"<?php if ($values['up3'] == "30") echo 'selected="selected"'; ?>>30</option> <option value="31"<?php if ($values['up3'] == "31") echo 'selected="selected"'; ?>>31</option> </select> Could be changed to: <select name="date"> <?php $i = 0; while ($i <= 31) {?> <option value="<?php echo $i; ?>"<?php if ($values['up3'] == $i) echo 'selected="selected"'; ?>><?php echo $i; ?></option> <?php $i++; } ?> </select> See how much cleaner it is in comparison? Quote Link to comment https://forums.phpfreaks.com/topic/112379-solved-fields-sometimes-populating-from-db-but-not-always/#findComment-576973 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.