How to link php array to database so it submits data


essentially i have two pages one for a user to submit out a form and another which is a confirmation form which then enters the data to populate my database. i can only use php embedded into html, I have managed to create a dropdown menu using an array and loop now i am struggling to get any response from it to connect with my confirmation page and enter data to my database thank you for your help. below is the form registration code

include ("db.php");
$pagename="Add Employee";

<?php $coCodes = array('10', '20', '30', '40');
echo "<select name ='form_companycode' >";
echo"<option value = ''></option>";

foreach($coCodes as $x)
	echo"<option value ='$x'>$x</option>";

//for ($x=0; $x<sizeof($coCodes); $x++)
	//echo '<option>'. $coCodes[$x] . '</option>';

<h2>Add employee below</h2>";
<?'d F Y H:i:s');?>
<hr>Fill the form below to add a new Employee;</hr>

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>";
echo"<td><name='form_companycode' size=35></td></tr>";
echo"<tr><td><input type=submit value='Add Employee'></td>";
echo"<td><input type=reset value='Clear Form'></td></tr>";
echo"</form>" ;

this is the confirmation code

$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>";


//check if any of the mandatory fields were not filled in
if($_SERVER['REQUEST_METHOD'] === 'POST'){ // Check for form submission
        var_dump($_POST);   // Temporary to check form input is as expected
        exit; // and stop there for now
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!";
	//if an optional field was not filled in then write SQL query without the column
		"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
		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>";


Hi @moksuhdkhan

When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

sorry i have done so

do you know how i would fix this issue i have the drop down box appearing on my employee page but it does not actually do anything once the document is submitted i can’t seem to connect it to my confirmation page to submit data to the DB

This bit:

echo"<td><name='form_companycode' size=35></td></tr>";

isn’t valid, surely? You’ve specified the name, but there’s no input tag.

And in the PHP code where you process the form variables


what is the value of $x? I can’t see that referenced anywhere in the PHP code, unless it’s in your include file.

ETA - I’m a bit confused now. Is all that code in the same physical file on your server, or is the bit after where it says “this is the confirmation code” a different file that is called by the first bit of code? You use $x in the first bit of code where the form is drawn, but it’s only valid inside the foreach() loop.

x represents the numbers 10-40 in my add employee page and i can’t put input type text when i need a drop box and it has to be done with php embedded in html

I get that you can’t put input type="text" for a drop-down, but you have to put something, like the code you have in the other thread on this subject.

Is this two separate files, or one big file?

so what do i put instead of text

two seperate files one for the users to see a add employee form they fill out and the second is a confirmation page file which then executes the data into my sql table

The code you had in the other thread.

which part

That will look for the literal string $x, as single quoted strings do not parse for variables.

OK, in that case $x is not specified anywhere in the second file, so it has no value. Even if it did, you want to use the form field name, not that variable, to get the value of the form field, just as you have with the three fields above it.

Oh yes, my mistake. So it won’t work even if there was a value for $x in the second bit of code.

i am confused as to what i need to change now

The part pertaining to that field. It starts with your <select> tag, contains the foreach() loop to run through your array of company names, and ends with the closing tag.

Yep, a good reason to never cast variables to string using quotes (such as "$var").

And me.

Why have you started a separate thread, with different code, when it seemed to be almost sorted out in the other one?

i did not understand it this code i understand in my own words