PHP embed drop down list that submits data into sql

hi, i have basically created a registration form using php and linking with mysql database. The only problem is i’m finding it difficult to embed a quick drop down list for where a use can select their company code. i have managed to code a list with an array but it does not submit my form and insert the data into my sql table. below is my addemployee_advanced.php which displays the form


<?php
include ("db.php");
$pagename="Add Employee";
echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";
echo "<body>";
echo "<hr><b>".date('d F Y H:i:s')."</b>";
echo "<hr>Fill the form below to add a new Employee";

//create a html form to capture user input
echo "<form method=post action=getemployee.php>" ;
echo "<table border=0 cellpadding=10>";
echo "<tr><td>*Employee ID </td>";
echo "<td><input type=text name=form_employeeid size=35></td></tr>";
echo "<tr><td>*Full Name </td>";
echo "<td><input type=text name=form_fullname size=35></td></tr>";
echo "<tr><td>*Position </td>";
echo "<td><input type=text name=form_position size=35></td></tr>";
echo "<tr><td>*Email </td>";
echo "<td><input type=text name=form_email size=35></td></tr>";
echo "<tr><td>*Company Code </td>"; 
$companycode = array(10, 20, 30, 40);
echo '<select name="company">';
for($i = 0; $i < count($companycode); $i++)
{
		echo '<option value=" '. ($i + 1) . ' ">' . $companycode[$i] . '</option>';
}	
echo '</select>';


echo "<tr><td><input type=submit value='Add Employee'></td>";
echo "<td><input type=reset value='Clear Form'></td></tr>";
echo "</table>";
echo "</form>" ;

echo "</body>";
?>

This is the second confirmation page which records the data and provides confirmation that the data has been inserted to sql.

<?php
include("db.php");
$pagename="New Employee Confirmation";
echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";
echo "<body>";
echo "<hr><b>".date('d F Y H:i:s')."</b>";


$employeeid=$_POST['form_employeeid'];
$fullname=$_POST['form_fullname'];
$position=$_POST['form_position'];
$email=$_POST['form_email'];
$companycode=$_POST['form_companycode'];


//check if any of the mandatory fields were not filled in
if (empty($employeeid) or empty($fullname) or empty($position) or empty($email) or empty($companycode))
{
	echo "<p>Please ensure all fields with a * are filled in!";
}
else
{
	//if an optional field was not filled in then write SQL query without the column
	{
	
	
		$addEmployeeSQL=
		"insert into 
		w1668315_Employee (w1668315_empID, w1668315_empFullName, w1668315_empPosition, w1668315_empEmail, w1668315_compCode)
		values (".$employeeid.", '".$fullname."','".$position."','".$email."',".$companycode.")";
	}
	//if optional was filled in, include column in SQL query
		
	//run SQL query
	$exeaddEmployeeSQL=mysqli_query($conn, $addEmployeeSQL);
	
	if (mysqli_errno($conn)==0)
	{ 
		echo "<p>A new Employee has been added successfully";
		echo "<p>Added Employee id: ".$employeeid;
		echo "<br>Added Full Name: ".$fullname;
		echo "<br>Added Position: ".$position;
		echo "<br>Added Email: ".$email;
		echo "<br>Added Company Code: ".$companycode;
		
	}
	//else check individual error codes and display appropriate message
	else
	{
		echo "<p>There is an error with the Employee you entered.";
		//error code for breach of PK or unique constraint
		if (mysqli_errno($conn)==1062)
		{
			echo "<br>The value entered for the new Employee id is not valid as it already exists.";			
		}
		//error code for inserting character that is problematic for SQL query
		if (mysqli_errno($conn)==1064)
		{
			echo "<br>Values entered for the Employee details are not valid.";			
		}
	}
}



echo "</body>";
?>

Those names must match. So either change the select name to form_companycode or use $_POST['company'] to read data from the form.

Sorry the box still appears blank with no numbers.

That’s not what I meant. What I meant was

<select name="form_companycode">

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.