{"id":1590,"date":"2008-06-30T00:01:57","date_gmt":"2008-06-30T05:01:57","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=1590"},"modified":"2013-12-07T19:40:18","modified_gmt":"2013-12-08T00:40:18","slug":"receiving-dynamic-textbox-data","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data","title":{"rendered":"Receiving Dynamic Textbox Data"},"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

In this tutorial we will be working with PHP and MySQL to insert the data that was inserted into the text boxes on the Dynamically Add Textbox to Site tutorial<\/a>. We will collect the data and insert the values into a database. To start we need to make sure our file that we are using the make this data is called insert.php (as we named it in the last tutorial.) This file will be called when you click the \u201cAdd Invoice\u201d button. <\/p>\n

\r\n<?php\r\n\r\n$conn = mysql_connect ("localhost","username","password") or die \r\n('cannot connect to database error: '.mysql_error());\r\nmysql_select_db ('test');\r\n<\/pre>\n

This starts the php and makes the connection to the database so that we can insert the values. <\/p>\n

\r\n$quantity = $_POST['quantity'];\r\n$desc = $_POST['desc'];\r\n$itemno = $_POST['itemno'];\r\n$ourcost = $_POST['ourcost'];\r\n$cost = $_POST['cost'];\r\n$distid = $_POST['distid'];\r\n$invoice = $_POST['invoice'];\r\n$integer = 0;\r\n<\/pre>\n

This declares all of our POST variables (the names of the textboxes in the form from the last page). Notice the additioni variable that we called integer. We are going to use this to go through all the results. This sets each php variable equal to the array of values we set on the last page. We can set which variable we are trying to use by defining the number of the array we would like to grab. For example:<\/p>\n

$quantity[0] \u2013 Quantity of first item.
\n$quantity[1] \u2013 Quantity of second item.
\nEct\u2026.<\/p>\n

\r\nwhile (count($quantity)>$integer) {\r\nif (($distid[$integer] <> "") && ($desc[$integer] <> "") && ($quantity[$integer] <> "")&& ($cost[$integer] <> "") && ($itemno[$integer] <> "") && ($ourcost[$integer] <> "")){\r\n$sql =  "INSERT INTO `test`.`item` (`InvoiceNo`, `DistID`, `ItemDesc`, `ItemQuantity`, `ItemCost`, `ItemNo`, `OurCost`) \r\n\tVALUES ('".$invoice."', '".$distid[$integer]."', '".$desc[$integer]."', '".$quantity[$integer]."', '".$cost[$integer]."', '".$itemno[$integer]."', '".$ourcost[$integer]."')";\r\nmysql_query($sql);\r\n<\/pre>\n

This section of code looks worse than it actually is. The function it is doing is actually relatively simple. The first line means that the system will do every thing after it until the amount of values in the array $quantity is greater than the value we set ($integer). The count function simply counts the number of values in an array. So if we insert 2 items this section will run until the value of $integer passes the count of $quantity. The second line checks all the variables for values and runs the insert query if none are blank. Notice that we are inserting $integer instead of a number so that this number can be incremented every time it is run. So on the first pass of the code:<\/p>\n

$quantity[$integer] = $quantity[0]<\/p>\n

\r\necho "$quantity[$integer] $desc[$integer](s) bought from Distribution Center $distid[$integer] \r\nhave\/has been added to the database with an Item Number of $itemno[$integer] \r\nand a customer cost of $cost[$integer] and a company cost of $ourcost[$integer]<br \/>";\r\n<\/pre>\n

This line simply echoes the values of all the variables into a sentence so we can see it was inserted into the database. <\/p>\n

\r\n}\r\nelse{\r\necho "Item number ".($integer+1)." is missing values and cannot be inserted.";\r\n}\r\n<\/pre>\n

The first bracket end the if statement. Everything up until that point would have been run if the statement were true. The next line starts the else statement. This will be run if one of the fields on the last page was blank. It will echo the statement above. Notice that we echoes the value of integer plus one since the integer starts on 0 instead of 1.<\/p>\n

\r\n$integer = ($integer + 1);\r\n}\r\n?>\r\n<\/pre>\n

Finally we increment our variable and then end the while statement. Once the variable is incremented it will try the while statement again. If it passes, the section of code will be run again but this time the value of our integer will be higher by one number. Finally we end our PHP.<\/p>\n

This concludes writing the receiving file. <\/p>\n

\"dynamic-data-retrieval_01\"<\/a><\/p>\n

Let\u2019s open our original file and insert two items with the above information. Notice the blank item number.<\/p>\n

\"dynamic-data-retrieval_02\"<\/a><\/p>\n

Your screen should look like this if you did it exactly the way I did. This means that the first item we inserted was successfully put into the database, but the second one was not since there was no item number. <\/p>\n

This concludes this tutorial. I hope it is helpful and you were able to follow it. Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"

In this tutorial we will be working with PHP and MySQL to insert the data that was inserted into the text boxes on the Dynamically Add Textbox to Site tutorial<\/a>. We will collect the data and insert the values into a database<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30,1,24],"tags":[50,61,62,49,57],"yoast_head":"\nReceiving Dynamic Textbox Data<\/title>\n<meta name=\"description\" content=\"In this tutorial we will be working with PHP and MySQL to insert the data that was inserted into the text boxes on the Dynamically Add Textbox to Site tutorial. We will collect the data and insert the values into a database\" \/>\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\/other-tutorials\/receiving-dynamic-textbox-data\" \/>\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\/other-tutorials\/receiving-dynamic-textbox-data#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/06\/dynamic-data-retrieval_01-699x407.jpg\",\"contentUrl\":\"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/06\/dynamic-data-retrieval_01-699x407.jpg\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data#webpage\",\"url\":\"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data\",\"name\":\"Receiving Dynamic Textbox Data\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data#primaryimage\"},\"datePublished\":\"2008-06-30T05:01:57+00:00\",\"dateModified\":\"2013-12-08T00:40:18+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b\"},\"description\":\"In this tutorial we will be working with PHP and MySQL to insert the data that was inserted into the text boxes on the Dynamically Add Textbox to Site tutorial. We will collect the data and insert the values into a database\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Receiving Dynamic Textbox Data\"}]},{\"@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":"Receiving Dynamic Textbox Data","description":"In this tutorial we will be working with PHP and MySQL to insert the data that was inserted into the text boxes on the Dynamically Add Textbox to Site tutorial. We will collect the data and insert the values into a database","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\/other-tutorials\/receiving-dynamic-textbox-data","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\/other-tutorials\/receiving-dynamic-textbox-data#primaryimage","inLanguage":"en-US","url":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/06\/dynamic-data-retrieval_01-699x407.jpg","contentUrl":"http:\/\/teamtutorials.com\/wp-content\/uploads\/2008\/06\/dynamic-data-retrieval_01-699x407.jpg"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data#webpage","url":"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data","name":"Receiving Dynamic Textbox Data","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data#primaryimage"},"datePublished":"2008-06-30T05:01:57+00:00","dateModified":"2013-12-08T00:40:18+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/eb38d967529dbe49f7cbe082fd39105b"},"description":"In this tutorial we will be working with PHP and MySQL to insert the data that was inserted into the text boxes on the Dynamically Add Textbox to Site tutorial. We will collect the data and insert the values into a database","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/other-tutorials\/receiving-dynamic-textbox-data#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Receiving Dynamic Textbox Data"}]},{"@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\/1590"}],"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=1590"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/1590\/revisions"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=1590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=1590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=1590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}