Create a simple php page that loads the list of users information with name and sex.if value exist in the database with ‘1’ as Male and ‘0’ as Female.
Please follow the following steps
Step1:
create a database called “test”. create a table called “tbl_user_information” using the below query
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
CREATE TABLE IF NOT EXISTS `tbl_user_information` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(250) NOT NULL, `sex` tinyint(2) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; -- -- Dumping data for table `tbl_user_information` -- INSERT INTO `tbl_user_information` (`id`, `name`, `sex`) VALUES (1, 'vasanthan', 1), (2, 'sudhina', 0); |
Step 2: create file called radio.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<!DOCTYPE html> <head> <body> </body> </head> <html> <?php // database connection $linkfirst = mysql_connect('localhost', 'root', ''); if (!$linkfirst) { die('Could not connect: ' . mysql_error()); } mysql_select_db('test', $linkfirst) or die('Could not select database.'); //query for selecting user information $query = "SELECT * FROM tbl_user_information"; $results = mysql_query($query,$linkfirst) or die(mysql_error()); ?> <table> <tr> <td>user name</td> <td>Gender</td> </tr> <?php while($row=mysql_fetch_array($results)) { ?> <tr> <td><?php echo $row['name']; ?></td> <td><?php echo $row['sex']; ?><input type="radio" id="male" name="male" <?php if($row['sex']=="1") { ?> checked="checked" <?php }?>>Male<input type="radio" id="female" name="female" <?php if ($row['sex']=="0") { ?> checked="checked" <?php }?>>Female</td> </tr> <?php } ?> </table> |
Step3: execute the file in your browser.