{"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":"
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 This starts the php and makes the connection to the database so that we can insert the values. <\/p>\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. 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 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 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 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 <\/a><\/p>\n Let\u2019s open our original file and insert two items with the above information. Notice the blank item number.<\/p>\n <\/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":"\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
\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
\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
\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
\r\n}\r\nelse{\r\necho "Item number ".($integer+1)." is missing values and cannot be inserted.";\r\n}\r\n<\/pre>\n
\r\n$integer = ($integer + 1);\r\n}\r\n?>\r\n<\/pre>\n