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: -

<form name="count" method="post" action="../php/addshopfloorcount.php">
          <table id="count" width="80%" border="1" class="table_text">
			  <tr>
			    <td colspan="4" class="table_text">Count Information:</td>
			  </tr>
			  <tr>
			    <td width="20%" align="center" >Count ID</td>
			    <td width="40%" align="center" >Department</td>
			    <td width="40%" align="center" >Username</td>
			  </tr>
		  <tr>
		    <td colspan="-3" align="center"><input name="id" type="text" id="id" size="10" maxlength="10" /></td>
		    <td align="center"><select name="department" id="department">
              <option value="--- Select Department ---">--- Select Department ---</option>
              <?
					// Get records from database (table "name_list").
					$list=mysql_query("select * from department order by id asc");
		
					// Show records by while loop.
					while($row_list=mysql_fetch_assoc($list)){
				  ?>
              <option value="<? echo $row_list['id']; ?>" <? if($row_list['id']==$select){ echo "selected"; } ?>><? echo $row_list['department']; ?> </option>
              <?
					// End while loop.
					}
				  ?>
            </select>            </td>
		    <td align="center"><select name="username" id="username">
              <option value="--- Select Username ---">--- Select Username ---</option>
              <?
					// Get records from database (table "name_list").
					$list=mysql_query("select * from members order by id asc");
		
					// Show records by while loop.
					while($row_list=mysql_fetch_assoc($list)){
				  ?>
              <option value="<? echo $row_list['id']; ?>" <? if($row_list['id']==$select){ echo "selected"; } ?>><? echo $row_list['username']; ?> </option>
              <?
					// End while loop.
					}
				  ?>
            </select>            </td>
		    </tr>
		    </table>

          <table id="lines" width="80%" border="1" class="table_text">
            <tr>
              <td colspan="5" class="table_text">Product Information:</td>
            </tr>
            <tr>
              <td width="10%" align="left">&nbsp;</td>
              <td width="30%" align="left">Barcode</td>
              <td width="30%" align="left" >Product</td>
              <td width="30%" align="left" >Quantity</td>
              <td width="30%" align="left" >&nbsp;</td>
            </tr>
            <tr>
              <td align="t"><input type="checkbox" name="chk" id="chk" value="0" /></td>
              <td colspan="-3" align="left"><input name="barcode" type="text" id="barcode" readonly="readonly" /></td>
              <td align="left">
              <select name="product" id="product" onchange='javascript:document.getElementByID("barcode").value=this.value;'>
                <option value="--- Select Product ---">--- Select Product ---</option>
                <?
					// Get records from database (table "name_list").
					$list=mysql_query("select * from `lines` order by barcode asc");
		
					// Show records by while loop.
					while($row_list=mysql_fetch_assoc($list)){
				  ?>
                <option value="<? echo $row_list['barcode']; ?>" <? if($row_list['barcode']==$select){ echo "selected"; } ?>><? echo $row_list['product']; ?> </option>
                <?
					// End while loop.
					}
				  ?>
              </select></td>
              <td><input type="text" name="quantity" id="quantity" /></td>
              <td><input type='button' value='Remove' onclick='removeRow(this);'/></td>
            </tr>
            <tr>
              <td align="left"><input name="chk" type="checkbox" id="chk" value="0" /></td>
              <td colspan="-3" align="left">
			  <input name="barcode" type="text" id="barcode" readonly="readonly" /></td>
              <td align="left"><select name="product" id="product">
                <option value="--- Select Department ---">--- Select Product ---</option>
                <?
					// Get records from database (table "name_list").
					$list=mysql_query("select * from `lines` order by barcode asc");
		
					// Show records by while loop.
					while($row_list=mysql_fetch_assoc($list)){
				  ?>
                <option value="<? echo $row_list['barcode']; ?>" <? if($row_list['barcode']==$select){ echo "selected"; } ?>><? echo $row_list['product']; ?> </option>
                <?
					// End while loop.
					}
				  ?>
              </select></td>
              <td align="left">
			  <input type="text" name="quantity" id="quantity" /></td>
              <td align="left"><input type='button' value='Remove' onclick='removeRow(this);'/></td>
            </tr>
            <tr>
              <td align="left"><input name="chk2" type="checkbox" id="chk2" value="0" /></td>
              <td colspan="-3" align="left">
			  <input name="barcode" type="text" id="barcode" readonly="readonly" /></td>
              <td align="left"><select name="product" id="product">
                <option value="--- Select Department ---">--- Select Product ---</option>
                <?
					// Get records from database (table "name_list").
					$list=mysql_query("select * from `lines` order by barcode asc");
		
					// Show records by while loop.
					while($row_list=mysql_fetch_assoc($list)){
				  ?>
                <option value="<? echo $row_list['barcode']; ?>" <? if($row_list['barcode']==$select){ echo "selected"; } ?>><? echo $row_list['product']; ?> </option>
                <?
					// End while loop.
					}
				  ?>
              </select></td>
              <td align="left">
			  <input type="text" name="quantity" id="quantity" /></td>
              <td align="left"><input type='button' value='Remove' onclick='removeRow(this);'/></td>
            </tr>
            <tr>
              <td align="left"><input name="chk" type="checkbox" id="chk" value="0" /></td>
              <td colspan="-3" align="left">
			  <input name="barcode" type="text" id="barcode" readonly="readonly" /></td>
              <td align="left"><select name="product" id="product">
                <option value="--- Select Department ---">--- Select Product ---</option>
                <?
					// Get records from database (table "name_list").
					$list=mysql_query("select * from `lines` order by barcode asc");
		
					// Show records by while loop.
					while($row_list=mysql_fetch_assoc($list)){
				  ?>
                <option value="<? echo $row_list['barcode']; ?>" <? if($row_list['barcode']==$select){ echo "selected"; } ?>><? echo $row_list['product']; ?> </option>
                <?
					// End while loop.
					}
				  ?>
              </select></td>
              <td align="left">
			  <input type="text" name="quantity" id="quantity" /></td>
              <td align="left"><input type='button' value='Remove' onclick='removeRow(this);'/></td>
            </tr>
            <tr>
              <td align="left"><input name="chk" type="checkbox" id="chk" value="0" /></td>
              <td colspan="-3" align="left">
			  <input name="barcode" type="text" id="barcode" readonly="readonly" /></td>
              <td align="left"><select name="product" id="product">
                <option value="--- Select Department ---">--- Select Product ---</option>
                <?
					// Get records from database (table "name_list").
					$list=mysql_query("select * from `lines` order by barcode asc");
		
					// Show records by while loop.
					while($row_list=mysql_fetch_assoc($list)){
				  ?>
                <option value="<? echo $row_list['barcode']; ?>" <? if($row_list['barcode']==$select){ echo "selected"; } ?>><? echo $row_list['product']; ?> </option>
                <?
					// End while loop.
					}
				  ?>
              </select></td>
              <td align="left">
			  <input type="text" name="quantity" id="quantity" /></td>
              <td align="left"><input type='button' value='Remove' onclick='removeRow(this);'/></td>
            </tr>
          </table>
          
	    <table align="center" width="50%" border="0">
			  <tr>
			    <td><input type="button" value="Add New Row" onclick="addRow();" /></td>
			    <td><input type="submit" name="save_count" id="save_count" value="Save Count" /></td>
			  </tr>
		  </table>
                 
          </form>
Member Avatar for diafol

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:

$field1 = mysql_real_escape_string($_POST['field1']); //etc for each field passed

$q = mysql_query("INSERT INTO mytable SET myfield1 = '$field1',myfield2 = '$field2' ...");

As this is uni work, I think I'll leave it at that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.