{"id":1664,"date":"2008-09-21T22:14:36","date_gmt":"2008-09-22T02:14:36","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=1664"},"modified":"2013-12-07T19:38:40","modified_gmt":"2013-12-08T00:38:40","slug":"creating-checkboxes-based-on-sql-results","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results","title":{"rendered":"Creating Checkboxes Based on SQL Results"},"content":{"rendered":"
Warning:<\/strong> This tutorial uses old techniques. It is insecure and will leave your server vulnerable to SQL Injection attacks<\/strong>.This tutorials also uses mysql_ functions that are no longer support. For updated tutorials look for a PDO<\/a> or MySQLi tutorial.This post will be delete or revised in the future.<\/p>\n<\/div>\n This tutorial will walk you through creating a database of categories and then using the categories in the database to create a series of checkboxes. This could be used for forum or board that will allow them to select a category of their story\/comment. Let\u2019s begin.<\/p>\n I will be doing all of this code and execution on my local PC thanks to WAMP Server which can be installed by following Installing a WAMP Server<\/a>. To get started we will need to create our database. To do this, run the following in your SQL editor:<\/p>\n This will create our table that we will be using for this tutorial. Now we need to create the file that will pull these values to our site and show them in an orderly fashion. Create an index.php in your www root of your WAMP server and open it in your favorite text editor. <\/p>\n This first section of the file simply creates the connection to the database and select all the categories from the table named tutorial (which we just created). It sets all the results to the variable of results. Make sure you change the code to have your databases user name and password in the proper locations. If it receives no results it throws an error and displays it to the browser window. The final thing you see is that we created a variable of cats so that we can keep track of how many items have been inserted.<\/p>\n Next, we set up our form. Then we create a table so that the items can be aligned properly. <\/p>\n This section so to process everything in the brackets of the \u201cwhile\u201d clause to be executed until $row (the results returned from our query) contains no information. The first line of the while clause checks to see if this is the third category we have entered since we declared cats to be 1. If it is, it echoes the line after it which shows the category in a column and then ends the row and starts a new one. Otherwise, it just puts the value in a new column and leaves the row tag open and increments the variable by one. This way every third category will be placed on a new row. The final line clears the results from the variable.<\/p>\n Then we close the final row and end the table. Then we create a couple of breaks and make the submit button to allow the user to submit the values and finally close the form. If you have followed these steps you should get a page similar to this:<\/p>\n\r\nSET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";\r\n\r\nCREATE TABLE `tutorial` (\r\n `CatID` int(3) NOT NULL auto_increment,\r\n `Category` varchar(100) NOT NULL,\r\n PRIMARY KEY (`CatID`)\r\n) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;\r\n\r\nINSERT INTO `tutorial` (`CatID`, `Category`) VALUES\r\n(1, 'Cars'),\r\n(2, 'Trucks'),\r\n(3, 'Baseball'),\r\n(4, 'Basketball'),\r\n(5, 'Football'),\r\n(6, 'Houses'),\r\n(7, 'Root Beer'),\r\n(8, 'Gatorade'),\r\n(9, 'Speakers'),\r\n(10, 'Computers'),\r\n(11, 'Databases'),\r\n(12, 'Software');\r\n<\/pre>\n
\r\n<?php\r\n\r\n$conn = mysql_connect ("localhost","user_name","password") or die ('cannot connect to database error: '.mysql_error());\r\nmysql_select_db ("tutorial");\r\n\r\n$sql = 'select * from `tutorial` order by Category';\r\n$result = mysql_query($sql);\r\n\r\nif (!$result) {\r\n echo "DB Error, could not list customers\\n";\r\n echo 'MySQL Error: ' . mysql_error();\r\n exit;\r\n}\r\n$cats = 1\r\n?>\r\n<\/pre>\n
\r\n<form name="category" method="post" action="insert.php">\r\n\r\nSelect your story topic(s)<br \/><br \/>\r\n<table border = "1" cellpadding = "5">\r\n<tr>\r\n<\/pre>\n
\r\n<?php\r\n\r\nwhile ($row = mysql_fetch_row($result)) \r\n{\r\nif (($cats \/ 3) == 1){\r\n\techo "<td align = 'right'>$row[1]:<input type='checkbox' name=$row[1] value=$row[1] \/> <br \/><\/td><\/tr><tr>";\r\n\t$cats = 1;\r\n}\r\nelse\r\n{\r\n\techo "<td align = 'right'>$row[1]:<input type='checkbox' name=$row[1] value=$row[1] \/> <br \/><\/td>";\r\n\t$cats++;\r\n}\r\n}\r\n\r\nmysql_free_result($result);\r\n\r\n?>\r\n<\/pre>\n
\r\n<\/tr><\/table><br \/><br \/>\r\n\r\n<input type='submit' value='Insert Comment'>\r\n<\/form>\r\n<\/pre>\n