943,186 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1025
  • PHP RSS
Apr 11th, 2010
0

PHP Form Script

Expand Post »
I am a bit of a newbie to PHP and have produced a dynamic form for a uni project, however, I am unsure how to code the PHP script which will insert the data inputted through the form into a SQL database.

The code for the form can be seen below and it can be seen graphically in the file attached: -

PHP Syntax (Toggle Plain Text)
  1. <form name="count" method="post" action="../php/addshopfloorcount.php">
  2. <table id="count" width="80%" border="1" class="table_text">
  3. <tr>
  4. <td colspan="4" class="table_text">Count Information:</td>
  5. </tr>
  6. <tr>
  7. <td width="20%" align="center" >Count ID</td>
  8. <td width="40%" align="center" >Department</td>
  9. <td width="40%" align="center" >Username</td>
  10. </tr>
  11. <tr>
  12. <td colspan="-3" align="center"><input name="id" type="text" id="id" size="10" maxlength="10" /></td>
  13. <td align="center"><select name="department" id="department">
  14. <option value="--- Select Department ---">--- Select Department ---</option>
  15. <?
  16. // Get records from database (table "name_list").
  17. $list=mysql_query("select * from department order by id asc");
  18.  
  19. // Show records by while loop.
  20. while($row_list=mysql_fetch_assoc($list)){
  21. ?>
  22. <option value="<? echo $row_list['id']; ?>" <? if($row_list['id']==$select){ echo "selected"; } ?>><? echo $row_list['department']; ?> </option>
  23. <?
  24. // End while loop.
  25. }
  26. ?>
  27. </select> </td>
  28. <td align="center"><select name="username" id="username">
  29. <option value="--- Select Username ---">--- Select Username ---</option>
  30. <?
  31. // Get records from database (table "name_list").
  32. $list=mysql_query("select * from members order by id asc");
  33.  
  34. // Show records by while loop.
  35. while($row_list=mysql_fetch_assoc($list)){
  36. ?>
  37. <option value="<? echo $row_list['id']; ?>" <? if($row_list['id']==$select){ echo "selected"; } ?>><? echo $row_list['username']; ?> </option>
  38. <?
  39. // End while loop.
  40. }
  41. ?>
  42. </select> </td>
  43. </tr>
  44. </table>
  45.  
  46. <table id="lines" width="80%" border="1" class="table_text">
  47. <tr>
  48. <td colspan="5" class="table_text">Product Information:</td>
  49. </tr>
  50. <tr>
  51. <td width="10%" align="left">&nbsp;</td>
  52. <td width="30%" align="left">Barcode</td>
  53. <td width="30%" align="left" >Product</td>
  54. <td width="30%" align="left" >Quantity</td>
  55. <td width="30%" align="left" >&nbsp;</td>
  56. </tr>
  57. <tr>
  58. <td align="t"><input type="checkbox" name="chk" id="chk" value="0" /></td>
  59. <td colspan="-3" align="left"><input name="barcode" type="text" id="barcode" readonly="readonly" /></td>
  60. <td align="left">
  61. <select name="product" id="product" onchange='javascript:document.getElementByID("barcode").value=this.value;'>
  62. <option value="--- Select Product ---">--- Select Product ---</option>
  63. <?
  64. // Get records from database (table "name_list").
  65. $list=mysql_query("select * from `lines` order by barcode asc");
  66.  
  67. // Show records by while loop.
  68. while($row_list=mysql_fetch_assoc($list)){
  69. ?>
  70. <option value="<? echo $row_list['barcode']; ?>" <? if($row_list['barcode']==$select){ echo "selected"; } ?>><? echo $row_list['product']; ?> </option>
  71. <?
  72. // End while loop.
  73. }
  74. ?>
  75. </select></td>
  76. <td><input type="text" name="quantity" id="quantity" /></td>
  77. <td><input type='button' value='Remove' onclick='removeRow(this);'/></td>
  78. </tr>
  79. <tr>
  80. <td align="left"><input name="chk" type="checkbox" id="chk" value="0" /></td>
  81. <td colspan="-3" align="left">
  82. <input name="barcode" type="text" id="barcode" readonly="readonly" /></td>
  83. <td align="left"><select name="product" id="product">
  84. <option value="--- Select Department ---">--- Select Product ---</option>
  85. <?
  86. // Get records from database (table "name_list").
  87. $list=mysql_query("select * from `lines` order by barcode asc");
  88.  
  89. // Show records by while loop.
  90. while($row_list=mysql_fetch_assoc($list)){
  91. ?>
  92. <option value="<? echo $row_list['barcode']; ?>" <? if($row_list['barcode']==$select){ echo "selected"; } ?>><? echo $row_list['product']; ?> </option>
  93. <?
  94. // End while loop.
  95. }
  96. ?>
  97. </select></td>
  98. <td align="left">
  99. <input type="text" name="quantity" id="quantity" /></td>
  100. <td align="left"><input type='button' value='Remove' onclick='removeRow(this);'/></td>
  101. </tr>
  102. <tr>
  103. <td align="left"><input name="chk2" type="checkbox" id="chk2" value="0" /></td>
  104. <td colspan="-3" align="left">
  105. <input name="barcode" type="text" id="barcode" readonly="readonly" /></td>
  106. <td align="left"><select name="product" id="product">
  107. <option value="--- Select Department ---">--- Select Product ---</option>
  108. <?
  109. // Get records from database (table "name_list").
  110. $list=mysql_query("select * from `lines` order by barcode asc");
  111.  
  112. // Show records by while loop.
  113. while($row_list=mysql_fetch_assoc($list)){
  114. ?>
  115. <option value="<? echo $row_list['barcode']; ?>" <? if($row_list['barcode']==$select){ echo "selected"; } ?>><? echo $row_list['product']; ?> </option>
  116. <?
  117. // End while loop.
  118. }
  119. ?>
  120. </select></td>
  121. <td align="left">
  122. <input type="text" name="quantity" id="quantity" /></td>
  123. <td align="left"><input type='button' value='Remove' onclick='removeRow(this);'/></td>
  124. </tr>
  125. <tr>
  126. <td align="left"><input name="chk" type="checkbox" id="chk" value="0" /></td>
  127. <td colspan="-3" align="left">
  128. <input name="barcode" type="text" id="barcode" readonly="readonly" /></td>
  129. <td align="left"><select name="product" id="product">
  130. <option value="--- Select Department ---">--- Select Product ---</option>
  131. <?
  132. // Get records from database (table "name_list").
  133. $list=mysql_query("select * from `lines` order by barcode asc");
  134.  
  135. // Show records by while loop.
  136. while($row_list=mysql_fetch_assoc($list)){
  137. ?>
  138. <option value="<? echo $row_list['barcode']; ?>" <? if($row_list['barcode']==$select){ echo "selected"; } ?>><? echo $row_list['product']; ?> </option>
  139. <?
  140. // End while loop.
  141. }
  142. ?>
  143. </select></td>
  144. <td align="left">
  145. <input type="text" name="quantity" id="quantity" /></td>
  146. <td align="left"><input type='button' value='Remove' onclick='removeRow(this);'/></td>
  147. </tr>
  148. <tr>
  149. <td align="left"><input name="chk" type="checkbox" id="chk" value="0" /></td>
  150. <td colspan="-3" align="left">
  151. <input name="barcode" type="text" id="barcode" readonly="readonly" /></td>
  152. <td align="left"><select name="product" id="product">
  153. <option value="--- Select Department ---">--- Select Product ---</option>
  154. <?
  155. // Get records from database (table "name_list").
  156. $list=mysql_query("select * from `lines` order by barcode asc");
  157.  
  158. // Show records by while loop.
  159. while($row_list=mysql_fetch_assoc($list)){
  160. ?>
  161. <option value="<? echo $row_list['barcode']; ?>" <? if($row_list['barcode']==$select){ echo "selected"; } ?>><? echo $row_list['product']; ?> </option>
  162. <?
  163. // End while loop.
  164. }
  165. ?>
  166. </select></td>
  167. <td align="left">
  168. <input type="text" name="quantity" id="quantity" /></td>
  169. <td align="left"><input type='button' value='Remove' onclick='removeRow(this);'/></td>
  170. </tr>
  171. </table>
  172.  
  173. <table align="center" width="50%" border="0">
  174. <tr>
  175. <td><input type="button" value="Add New Row" onclick="addRow();" /></td>
  176. <td><input type="submit" name="save_count" id="save_count" value="Save Count" /></td>
  177. </tr>
  178. </table>
  179.  
  180. </form>
Attached Thumbnails
Click image for larger version

Name:	form.jpg
Views:	28
Size:	68.2 KB
ID:	14354  
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nelliott10 is offline Offline
14 posts
since Feb 2010
Apr 11th, 2010
0
Re: PHP Form Script
You need a form handler page (I assume this is addshopfloorcount.php - but it shouldn't be the same page as your form) which will receive the form data. The data will be in the superglobal $_POST variable.

Example:

PHP Syntax (Toggle Plain Text)
  1. $field1 = mysql_real_escape_string($_POST['field1']); //etc for each field passed
  2.  
  3. $q = mysql_query("INSERT INTO mytable SET myfield1 = '$field1',myfield2 = '$field2' ...");

As this is uni work, I think I'll leave it at that.
Sponsor
Featured Poster
Reputation Points: 1041
Solved Threads: 935
Sarcastic Poster
ardav is offline Offline
6,623 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: In want to display PHP query results in a table
Next Thread in PHP Forum Timeline: mysql_fetch_assoc()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC