{"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":"
\n

You should not use this code on a production website.<\/h4>\n

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

\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

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

\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

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

\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

Next, we set up our form. Then we create a table so that the items can be aligned properly. <\/p>\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

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

\r\n<\/tr><\/table><br \/><br \/>\r\n\r\n<input type='submit' value='Insert Comment'>\r\n<\/form>\r\n<\/pre>\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

\"creating_checkboxes_from_sql_results_01\"<\/a><\/p>\n

If you go into the database and add more items, or change the items in there the page will automatically show your changes without the need to change the code like below:<\/p>\n

\"\"<\/a><\/p>\n

This concludes this tutorial. We will continue to take this a step further in the next tutorial. If you have any questions, please leave a comment. I hope this was easy to follow and thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"

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.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,17],"tags":[50,51,52,47],"yoast_head":"\nCreating Checkboxes Based on SQL Results<\/title>\n<meta name=\"description\" content=\"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.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Maguire\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/09\/creating_checkboxes_from_sql_results_01.jpg\",\"contentUrl\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/09\/creating_checkboxes_from_sql_results_01.jpg\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results#webpage\",\"url\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results\",\"name\":\"Creating Checkboxes Based on SQL Results\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results#primaryimage\"},\"datePublished\":\"2008-09-22T02:14:36+00:00\",\"dateModified\":\"2013-12-08T00:38:40+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\"},\"description\":\"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.\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating Checkboxes Based on SQL Results\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\",\"name\":\"Mike Maguire\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r\",\"caption\":\"Mike Maguire\"},\"sameAs\":[\"http:\/\/mikemaguire.me\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating Checkboxes Based on SQL Results","description":"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.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results","twitter_misc":{"Written by":"Mike Maguire","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results#primaryimage","inLanguage":"en-US","url":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/09\/creating_checkboxes_from_sql_results_01.jpg","contentUrl":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/09\/creating_checkboxes_from_sql_results_01.jpg"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results#webpage","url":"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results","name":"Creating Checkboxes Based on SQL Results","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results#primaryimage"},"datePublished":"2008-09-22T02:14:36+00:00","dateModified":"2013-12-08T00:38:40+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b"},"description":"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.","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/creating-checkboxes-based-on-sql-results#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Creating Checkboxes Based on SQL Results"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b","name":"Mike Maguire","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/36f2aa9a11241ca79ed05e758e36f3cb?s=96&d=mm&r=r","caption":"Mike Maguire"},"sameAs":["http:\/\/mikemaguire.me"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1664"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=1664"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1664\/revisions"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=1664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=1664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=1664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}