How to link php array to database so it submits data

so far i understand that i made an array called coCodes and then i looped it around another variable x then outputed x to get a drop down list now i just need to know how to link the list to my database and confirmation page.

Yes, that’s correct. But you don’t have that code in the stuff you posted at the top of this thread, for some reason. That needs to be put back.

Form processing is fairly straightforward*. You have a html form that looks like this example (this is not your form, it’s just a sample one) :

<form method="post" action="confirmation.php">
<input type="text" name="firstname" size="35">
<input type="password" name="secret" size="10">
<select name="age">
<option value="1">up to 18</option>
<option value="2">19-30</option>
<option value="3">31 or older</option>
<input type="submit">

This has three fields - the first is called “firstname”, and it’s a plain text field. The second is called “secret”, and it’s a password field, which is basically a text field but it doesn’t show the text as you type. The third is a drop-down list, and it’s called “age”. It will display three age ranges, and when the user selects one, the value specified for that age range (1, 2 or 3) will be submitted with the form, and the text shown in the drop-down will not. If you just want the drop-down text submitted as-is, just don’t specify a value= parameter for the option.

The last input in the form is a submit button. When the user presses the button, the browser will have a look in the opening form tag, see the “action” and “method” values, and submit to the script specified in “action”, using the chosen method.

Here we use “post”, which means that when the PHP script (in the example, I’ve called in “confirmation.php”) is called, the form variables are in an array called “$_POST”. The entries in that array are named using the field names from the form, so in our case we will have:


The first two will contain whatever text was typed in those two form fields, and the third will contain either 1, 2 or 3. The PHP handling of them does not change, PHP has no way (I think) to know whether each field came from a text box, or a drop-down, or a radio button or any other type of html form field.

If you’re not sure about what form variables are being passed through from your html, an easy way to check is to add the following lines to the start of your PHP processing code, after the opening PHP tag:


which will dump the entire contents of the array to the screen, and go no further. Once you’re sure what is coming from the form, then you can go on from that in your PHP to store the information.

( * - this is fairly old-style html code, lots of things have changed and there are many improvements, but I believe this is still valid and an easy example. If anything has become invalid html code I am sure someone will point it out).

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

i have now changed the employee page like this and commented out the loop i got the drop down list but no numbers inside

oh wait the numbers now appear at the top of the page instead of being inside the drop list ??

Yes, look at where you’ve closed the td and tr tags in the wrong place. All the select code needs to be inside them. You’ve got all the options and the closing select tag in no-mans land as far as your table is concerned.

i’m not following sorry what about the td arent they supposed to start first and end last why does the select code have to go inside the same tag?

done it sorry but the form still does not submit unless i am missing something else the drop list is fine now but i feel it dosent actually work at all

OK, so that’s the output from var_dump(), that shows you what form variables are present, what they’re called and what they contain. Now you need to remove those two lines, and you can get back to getting your processing code working.

1 Like

but i need them dont i to link the form to confirmation to database

i’m confused as to what i should delete

Yes, but var_dump() only displays them. Read the comments in your own code:

        var_dump($_POST); // Temporary to check form input is as expected
        exit; // and stop there for now

You have code that displays the form variables and exits. Now you need to remove these lines and get on with processing the form data.


i’m sorry i am stuck on what to delete

The lines I showed you above.

I don’t mean to sound rude, but has none of this been covered in your course work to date? It seems unreasonble that you would be set an assignment to do something that you haven’t been shown how to do.

1 Like

i know they only showed how to make a simple database and only 2 lessons on php and that was it so apologies for silly Q’s

The trouble is, I’m not an instructor so I don’t have the skill of being able to explain things to people - which is a very different skill to being able to write code.

so what should i do i just needed the form to submit that is all

Remove the two lines I showed you in the last post that I put code in. Those two lines are displaying the contents of the form, and then exiting. When you have removed them, the code will continue to the next part and you can debug it from there.

But change this line to use the correct form field name:


which was discussed in more detail further up, and you can see what it should be called from the var_dump() display, your code and my detailed explanation of form variables above.

i dont have the variable x anymore