{"id":1484,"date":"2008-05-04T23:59:15","date_gmt":"2008-05-05T04:59:15","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=1484"},"modified":"2013-12-07T19:39:19","modified_gmt":"2013-12-08T00:39:19","slug":"sql-select-based-on-text-inserted-into-a-text-box","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/photoshop-tutorials\/web-graphics\/sql-select-based-on-text-inserted-into-a-text-box","title":{"rendered":"SQL Select Based on Text Inserted Into a Text Box"},"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 using PHP to select entries from a database based on the text that is entered into a textbox on the site. This guide will also walk you through making the insert script to insert the comments into the database as well as help you make the database. <\/p>\n First we need to create our database that we will be using. The following code will set our table that we will be using up automatically. **NOTE** change DBNAME and tutorial to the database name and table name respectively. As shown, I called my table tutorial, so for the sake of following this tutorial, you may want to do the same.<\/p>\n As you can see, this will create a table called tutorial on our database with 4 fields of data:<\/p>\n Id \u2013 Primary key and auto increment. (Will increase by one for every entry inserted) Now that we have our table prepared, let\u2019s insert a sample entry to it to make sure it is ok. Once you verify that, we are ready to start preparing our scripts to write and to read from the database. Let\u2019s start with our insert script first. I called this file index.php.<\/p>\n The first thing in our file will be the connection string. This connects to our database. Make sure you insert you DBname, username, and password into the appropriate places in the string. You can now save the file and launch it in a browser. If the screen comes up blank the connection stream worked. If it throws an error, it will tell you the error and you will need to troubleshoot it. <\/p>\n Next we declare the variables that we are going to use. We will make a form in the php file later that will store these variables on submit. As you can see, we have a variable for the name, the topic, and the comment.<\/p>\n This line checks to see if all of the variables that we need have indeed been set. This allows the page to see whether the user has already inserted data, or if this is the first time the page has been loaded. <\/p>\n This section runs if the variables are set. Just because they are \u201cset\u201d does not mean they contain information. This snippet checks for there to be valid characters in all the variables. If there is not, it throws an error and ends the PHP so that erroneous data is not entered into the database.<\/p>\n This code inserts the values they entered into the fields into the database. Again, make sure your DBNAME and TABLENAME are properly inserted into those places. The last line returns the primary key (the one that is set to auto increment) so that we can display the comment number to the user.<\/p>\n This code simply displays the values back to the user in formatted text so they can see that there comment went through the way they intended. The submit button simply redirects to the same page so that you can insert another comment if you would like. When you click the button and reload the page it will clear the variables. If you just reload the page it will keep submitting the variables to the database. The trailing end bracket ends the If statement. <\/p>\n This is the else statement that runs if the first IF statement if not true (the ISSET one). It simply shows the form to the user and tells it to reload this page on submit. Note how the names of the input boxes are the same of the variables that we declared in the first sections. Now you should be able to run you file and use it to submit a comment.<\/p>\n <\/a><\/p>\n You should see the form that we created since none of the variables have been set yet. Fill out the form saying whatever you would like it to say and hit submit. <\/p>\n <\/a><\/p>\n When you hit submit, the form should simply reload the page and you should be greeted with the success message. Submit several different types of comments so that when you run a select later you can see that it is functioning the way that it should. <\/p>\n Now we need to make our file that will pull the data back from the database for us. We are going to make it so that we can put the text that we are looking for into a textbox and pull the data back based on that. I called this file lookup.php<\/p>\n This file starts off with the connection string again. It should be identical to the first one as it is pulling from the same database that we were inserting to. <\/p>\n Then we have our variables. This time we only have two to work with; method and text. Method is going to tell the query where to look for our text (name, topic, comment, or id). The text is going to be the text that we are looking for within that column. <\/p>\n This section simply checks for the variables to be set and sets up the table to show our results once the query is ran.<\/p>\n This is the select statement to pull the data from the database. Note that the where clause contains the variable $method. This variable is whatever is picked from the drop-down menu that we will be creating. Also, notice how we are using like instead of the equal sign. This tells the database to look for that text in ANY part of the field. It is not case sensitive either. <\/p>\n This bit of code simply sets the variable to the query that was just ran and echo\u2019s the variables that were returned by the previous query until the variable no longer contains information. The first close bracket ends the while loop and the second ends the IF statement.<\/p>\n This final bit of code simply creates our form to fill out that will allow us to create the selection stream. It sets up the HTML form to go back to this page on submit. Then it creates the drop down with the options of ID, name, topic, and comment. Then it creates an input box for you to type the text that you are trying to locate. Then it gives us the submit button. Finally it shows the query that was run in order to retrieve the results. I did this so that you can see what the query looks like when it is run. This completes this form. You should now be able to launch the form.<\/p>\n\r\nCREATE TABLE `DBNAME`.`tutorial` (\r\n`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,\r\n`name` VARCHAR( 30 ) NOT NULL ,\r\n`topic` VARCHAR( 30 ) NOT NULL ,\r\n`comment` VARCHAR( 255 ) NOT NULL ,\r\nPRIMARY KEY ( `id` ) \r\n) ENGINE = MYISAM \r\n<\/pre>\n
\nName \u2013 name of the user making a comment
\nTopic \u2013 A heading\/title for the comment
\nComment \u2013 The actual comment from the user.<\/p>\n\r\n<?php\r\n$conn = mysql_connect ("localhost","dbname_username","password") or die ('cannot connect to database error: '.mysql_error());\r\nmysql_select_db ("databasename");\r\n<\/pre>\n
\r\n$name = $_POST['Name'];\r\n$topic = $_POST['Topic'];\r\n$comment = $_POST['Comment'];\r\n<\/pre>\n
\r\nif (isset($name) && isset($topic) && isset($comment))\r\n{\r\n<\/pre>\n
\r\nif(($name=="") || ($topic=="") || ($comment=="")) {\r\necho 'One or more field has been left blank. Please use your browser back button and correct this. Thank-you.';\r\ndie ();\r\n}\r\n<\/pre>\n
\r\n$sql = "INSERT INTO\r\n`DBNAME`.`TABLENAME` (`id`,`name`, `topic`, `comment`)\r\nVALUES (NULL,'".$name."','".$topic."','".$comment."');";\r\nmysql_query($sql);\r\n$commentno = mysql_insert_id();\r\n<\/pre>\n
\r\necho 'You comment has been successfully submitted with the following values: <br \/>';\r\necho "<br \/><B>Comment #:<\/B> $commentno";\r\necho "<br \/><B>Name:<\/B> $name";\r\necho "<br \/><B>Topic:<\/B> $topic";\r\necho "<br \/><B>Comment:<\/B> $comment";\r\necho '<form method="post" action = "index.php">';\r\necho '<Input Type="SUBMIT" value="Submit Another?"><\/form>';\r\n\r\n}\r\n<\/pre>\n
\r\nelse{\r\n\r\necho '<h2>Please leave us your comments: (All Fields Are Required)<\/h2>';\r\necho '<form method="post" action="index.php">';\r\necho 'Your Name: <br \/> <INPUT TYPE="TEXT" NAME="Name" size="35" maxlength = "30"><br \/><br \/>';\r\necho 'What is your comment about?: <br \/> <INPUT TYPE="TEXT" NAME="Topic" size="35" maxlength = "30"><br \/><br \/>';\r\necho 'Comment: <br \/> <TEXTAREA NAME = "Comment" rows="7" cols = "35"><\/textarea><br \/><br \/>';\r\necho '<INPUT TYPE="SUBMIT" VALUE="Submit Comment"> <br \/><br \/>';\r\necho '<\/form>';\r\n}\r\n?>\r\n<\/pre>\n
\r\n<?php\r\n$conn = mysql_connect ("localhost","dbname_username","password") or die ('cannot connect to database error: '.mysql_error());\r\nmysql_select_db ("databasename");\r\n<\/pre>\n
\r\n$method = $_POST['method'];\r\n$text = $_POST['string'];\r\n<\/pre>\n
\r\nif (isset($method) && isset($text)){\r\n\r\necho '<table cellspacing="2" cellpadding="6" border = "2">';\r\necho '<tr>';\r\necho '<th style="text-align:left;">ID #<\/th>';\r\necho '<th>Name<\/th>';\r\necho '<th>Topic<\/th>';\r\necho '<th>Comment<\/th>';;\r\necho '<\/tr>';\r\n<\/pre>\n
\r\n$sql ="select * from `tutorial` where $method like '%$text%'";\r\n$query = mysql_query($sql);\r\n<\/pre>\n
\r\nwhile ($row = mysql_fetch_array($query)){\r\necho "\r\n<tr>\r\n<td>".$row['id']."<\/td>\r\n<td>".$row['name']."<\/td>\r\n<td>".$row['topic']."<\/td>\r\n<td>".$row['comment']."<\/td>\r\n<\/tr>";\r\n}\r\n}\r\n<\/pre>\n
\r\necho '<h2>Enter your search criteria<\/h2>';\r\necho '<form method="post" action="lookup.php">';\r\necho 'Search which area? <br \/> <SELECT NAME="method"><br \/><br \/>';\r\necho '<option value="id" style="text-align:left">id <\/option>';\r\necho '<option value="name" style="text-align:left">name <\/option>';\r\necho '<option value="topic" style="text-align:left">topic <\/option>';\r\necho '<option value="comment" style="text-align:left">comment <\/option><\/select> <br \/>';\r\necho 'Search for what? <br \/> <INPUT TYPE="TEXT" NAME="string" size="35" maxlength = "30"><br \/><br \/>';\r\necho '<INPUT TYPE="SUBMIT" VALUE="Search">';\r\necho '<\/form><br \/><br \/>';\r\necho $sql;\r\n\r\n?>\r\n<\/pre>\n