How to link php array to database so it submits data

OK, good. You have the correct form field name instead now, right?

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

<?php
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"<select name='form_companycode'>";
echo"<option value = '10'> 10 </option>";
echo"<option value = '20'> 20 </option>";
echo"<option value = '30'> 30 </option>";
echo"<option value = '40'> 40 </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>" ;
?>










  









</body>";

this is what i have currently

before even attempting option field dropdown etc. these worked and submitted

Can’t read it, remember you need to surround it with three backticks to display properly, or use the “formatted text” option on the reply box.

(just applied it for him)

1 Like

mathcing name to the post is not working for the drop down

You’ve still got the select stuff in no-mans land, not inside the <td> tags. I’d far rather you fixed it, but in the interests of moving forwards, here is what that section should look like:

echo"<tr><td>*Company Code";

echo"<select name='form_companycode'>";
echo"<option value = '10'> 10 </option>";
echo"<option value = '20'> 20 </option>";
echo"<option value = '30'> 30 </option>";
echo"<option value = '40'> 40 </option>";
echo"</select></td></tr>";

Also need to see your PHP code.

<?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['$x'];


//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!";
}
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>";
?>

A minute ago you said

And yet in your current code, you have:

$companycode=$_POST['$x'];

You also have not removed the two lines that I specifically outlined in post 33 should be removed, so it won’t process the form.

I’m not sure I can offer any more to this problem, I can’t think of any other ways to explain what you need to do without just doing it and posting the code, which won’t help you understand what you’re doing.

3 Likes

apologies i am very stressed and have not realised

as expected i changed the x inside [ ] to ‘from_companycode’ and it still does not work.

form sos

but i do not have the lines on 33 in my code anyway?

you know what i am very sorry sir/madam i have just realised what you said

Yes you do.

it is now working thank you

thank you for all who have helped and spent their time explaining once apolgies for wasting your time.

2 Likes

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