Sending Form values to PHP as an array

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
sambeckham
Forum Newbie
Posts: 3
Joined: Wed Jan 26, 2011 4:14 am

Sending Form values to PHP as an array

Post by sambeckham »

Hi there,

I have been asked to take the values from a form and submit them to a mySQL database. Normally this wouldn't be a problem but I can't edit the form and the names are all "form[surname]" or "form[address]" etc.

I have been informed that the form is passing the data through as an array called form[] However when I tried to serialize the array using

Code: Select all

$form=serialize($_POST['form']);
it returns the value "N;" which makes little to no sense to me.

Here's the code below

Code: Select all

<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("hpoolSFC", $con);

$form=serialize($_POST['form']);

$sql="INSERT INTO enrollment (title, forename, surname, dob, gender, housenum, street1, street2, town, county, postcode, email, hometel, mobtel, pginitials, pgsurname, pgaddress, pgphone, schoolattended, careerinfo, courses, primary, id) VALUES('$form')"; 


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>

Code: Select all

<form method="post" id="userForm" enctype="multipart/form-data" action="http://www.hpoolsfc.ac.uk/enrol-online.html">
<table border="0" class="enrol">
<td colspan="3"><hr>
<span style="color:#000;font-weight:bold;">Yoooooooooour Details</span></td>
	<tr>
		<td width=22%">Title:</td>
		<td><select  name="form[Title][]" size="1" id="Title"  ><option  value="Mr">Mr</option><option  value="Miss">Miss</option><option  value="Ms">Ms</option><option  value="Mrs">Mrs</option></select><div class="formClr"></div><span id="component87" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Forenames: (*)</td>
		<td><input type="text" value="" size="30"  name="form[Forenames]" id="Forenames" /><div class="formClr"></div><span id="component91" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Surname: (*)</td>
		<td><input type="text" value="" size="30"  name="form[Surname]" id="Surname" /><div class="formClr"></div><span id="component86" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Date of Birth: (*)</td>
		<td><input id="txtcal0" name="form[DOB]" type="text"   value="" /><input id="btn0" type="button" value="..." onclick="showHideCalendar('cal0Container');" class="btnCal"  /><div id="cal0Container" style="clear:both;display:none;position:absolute;z-index:9994"></div><div class="formClr"></div><span id="component85" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Gender: (*)</td>
		<td><input type="text" value="" size="10"  name="form[Gender]" id="Gender" /><div class="formClr"></div><span id="component84" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>House Num/Name: (*)</td>
		<td><input type="text" value="" size="30"  name="form[HouseNum]" id="HouseNum" /><div class="formClr"></div><span id="component83" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Street 1: (*)</td>
		<td><input type="text" value="" size="40"  name="form[Street1]" id="Street1" /><div class="formClr"></div><span id="component82" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Street 2:</td>
		<td><input type="text" value="" size="40"  name="form[Street2]" id="Street2" /><div class="formClr"></div><span id="component81" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Town or City: (*)</td>
		<td><input type="text" value="" size="40"  name="form[Town]" id="Town" /><div class="formClr"></div><span id="component80" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>County: (*)</td>
		<td><input type="text" value="" size="30"  name="form[County]" id="County" /><div class="formClr"></div><span id="component79" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Post Code: (*)</td>
		<td><input type="text" value="" size="10"  name="form[Postcode]" id="Postcode" /><div class="formClr"></div><span id="component78" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Email: (*)</td>
		<td><input type="text" value="" size="30"  name="form[Email]" id="Email" /><div class="formClr"></div><span id="component92" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Home Telephone:</td>
		<td><input type="text" value="" size="15"  name="form[HomePhone]" id="HomePhone" /><div class="formClr"></div><span id="component77" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Mobile Telephone: (*)</td>
		<td><input type="text" value="" size="15"  name="form[Mobile ]" id="Mobile " /><div class="formClr"></div><span id="component76" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
<tr>
<td colspan="3"><hr>
<span style="color:#000;font-weight:bold;">Parent or Guardian Details</span></td>
</tr>
	<tr>
		<td>Initials: (*)</td>
		<td><input type="text" value="" size="5"  name="form[Initials]" id="Initials" /><div class="formClr"></div><span id="component90" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Surname: (*)</td>
		<td><input type="text" value="" size="30"  name="form[Parentsurname ]" id="Parentsurname " /><div class="formClr"></div><span id="component75" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td valign="top">Address</td>
		<td><textarea cols="40" rows="8" name="form[Parentaddress]" id="Parentaddress" ></textarea><div class="formClr"></div><span id="component89" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Parent Phone</td>
		<td><input type="text" value="" size="15"  name="form[Parentphone]" id="Parentphone" /><div class="formClr"></div><span id="component74" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
<tr>
<td colspan="3"><hr>
<span style="color:#000;font-weight:bold;">Education</span></td></tr>
	<tr>
		<td>11-16 School Attended (*)</td>
		<td><select  name="form[School][]" size="1" id="School"  ><option  value="Brierton">Brierton</option><option  value="Dyke House">Dyke House</option><option  value="English Martyrs">English Martyrs</option><option  value="High Tunstall">High Tunstall</option><option  value="Manor">Manor</option><option  value="S.T Hilds">S.T Hilds</option><option  value="Other (Please specify)">Other (Please specify)</option><option  value=""></option><option  value=""></option><option  value=""></option><option  value=""></option><option  value=""></option><option  value=""></option></select><div class="formClr"></div><span id="component73" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	<tr>
		<td>Other School</td>
		<td><input type="text" value="" size="30"  name="form[Otherschool]" id="Otherschool" /><div class="formClr"></div><span id="component72" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
<tr>
<td colspan="3"><hr>
<span style="color:#000;font-weight:bold;">Careers</span><br>It will help us to provide appropriate guidance over your programme of study at the college if you would indicate below any career interests you may have. For example are you considering higher education and for what type of employment are you eventually aiming?
</td></tr>
	<tr>
		<td>Career Info</td>
		<td><textarea cols="40" rows="8" name="form[CareerInfo]" id="CareerInfo" ></textarea><div class="formClr"></div><span id="component94" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
	 <tr>
<td colspan="3"><hr>
<span style="color:#000;font-weight:bold;">Courses that I am interested in at the college</span><br>Please select below any of the college courses you are interested in taking and would like to discuss at interview.
</td></tr>
	<tr>
		<td>Courses (*)</td>
		<td><input  name="form[Courses][]" type="checkbox" value="Art&nbsp;&nbsp;&nbsp;" id="Courses0"  /><label for="Courses0">Art&nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Biology &nbsp;&nbsp;&nbsp;" id="Courses1"  /><label for="Courses1">Biology &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Business Studies &nbsp;&nbsp;&nbsp;" id="Courses2"  /><label for="Courses2">Business Studies &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Chemistry &nbsp;&nbsp;&nbsp;<br><br>" id="Courses3"  /><label for="Courses3">Chemistry &nbsp;&nbsp;&nbsp;<br><br></label><br/><input  name="form[Courses][]" type="checkbox" value="Child Care and Education &nbsp;&nbsp;&nbsp;" id="Courses4"  /><label for="Courses4">Child Care and Education &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Communication and Culture &nbsp;&nbsp;&nbsp;" id="Courses5"  /><label for="Courses5">Communication and Culture &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Computing and ICT <br><br>" id="Courses6"  /><label for="Courses6">Computing and ICT <br><br></label><br/><input  name="form[Courses][]" type="checkbox" value="Critical Thinking &nbsp;&nbsp;&nbsp;" id="Courses7"  /><label for="Courses7">Critical Thinking &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Design Technology &nbsp;&nbsp;&nbsp;" id="Courses8"  /><label for="Courses8">Design Technology &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="English &nbsp;&nbsp;&nbsp;" id="Courses9"  /><label for="Courses9">English &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Film Studies &nbsp;&nbsp;&nbsp;<br><br>" id="Courses10"  /><label for="Courses10">Film Studies &nbsp;&nbsp;&nbsp;<br><br></label><br/><input  name="form[Courses][]" type="checkbox" value="Geography &nbsp;&nbsp;&nbsp;" id="Courses11"  /><label for="Courses11">Geography &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="General Studies &nbsp;&nbsp;&nbsp;" id="Courses12"  /><label for="Courses12">General Studies &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Health and Social Care &nbsp;&nbsp;&nbsp;" id="Courses13"  /><label for="Courses13">Health and Social Care &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="History <br><br>" id="Courses14"  /><label for="Courses14">History <br><br></label><br/><input  name="form[Courses][]" type="checkbox" value="Law &nbsp;&nbsp;&nbsp;" id="Courses15"  /><label for="Courses15">Law &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Mathematics &nbsp;&nbsp;&nbsp;" id="Courses16"  /><label for="Courses16">Mathematics &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Media Studies &nbsp;&nbsp;&nbsp;" id="Courses17"  /><label for="Courses17">Media Studies &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Modern Foreign Languages <br><br>" id="Courses18"  /><label for="Courses18">Modern Foreign Languages <br><br></label><br/><input  name="form[Courses][]" type="checkbox" value="Music &nbsp;&nbsp;&nbsp;" id="Courses19"  /><label for="Courses19">Music &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Performing Arts &nbsp;&nbsp;&nbsp;" id="Courses20"  /><label for="Courses20">Performing Arts &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="PE/Sports &nbsp;&nbsp;&nbsp;" id="Courses21"  /><label for="Courses21">PE/Sports &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Photography &nbsp;&nbsp;&nbsp;" id="Courses22"  /><label for="Courses22">Photography &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Physics <br><br>" id="Courses23"  /><label for="Courses23">Physics <br><br></label><br/><input  name="form[Courses][]" type="checkbox" value="Psychology &nbsp;&nbsp;&nbsp;" id="Courses24"  /><label for="Courses24">Psychology &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Philosophy and Ethics &nbsp;&nbsp;&nbsp;" id="Courses25"  /><label for="Courses25">Philosophy and Ethics &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Science in Society &nbsp;&nbsp;&nbsp;" id="Courses26"  /><label for="Courses26">Science in Society &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Sociology &nbsp;&nbsp;&nbsp;<br<br>" id="Courses27"  /><label for="Courses27">Sociology &nbsp;&nbsp;&nbsp;<br<br></label><br/><input  name="form[Courses][]" type="checkbox" value="Textiles &nbsp;&nbsp;&nbsp;" id="Courses28"  /><label for="Courses28">Textiles &nbsp;&nbsp;&nbsp;</label><br/><input  name="form[Courses][]" type="checkbox" value="Travel and Tourism &nbsp;&nbsp;&nbsp;" id="Courses29"  /><label for="Courses29">Travel and Tourism &nbsp;&nbsp;&nbsp;</label><br/><div class="formClr"></div><span id="component95" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
<tr>
<td colspan="3"><hr></td></tr>
<tr>
		<td>Primary Choice</td>
		<td><input  name="form[FirstChoice][]" type="checkbox" value="Hartlepool Sixth Form College is my first choice for further education." id="FirstChoice0"  /><label for="FirstChoice0">Hartlepool Sixth Form College is my first choice for further education.</label><div class="formClr"></div><span id="component101" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
<tr>
<td colspan="3"><hr>
<span style="color:#000;font-weight:bold;">Anti Spam</span><br>Please enter the two words in the box below so we know you are a real person!
</td></tr>
	<tr>
<tr>
		<td></td>
		<td><script type="text/javascript"> 
	var RecaptchaOptions = {
	theme : "white"
};
</script><script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6LcteLwSAAAAAK6oTvERh0RJCdggjp99sYdYhceE"></script>

	<noscript>
  		<iframe src="http://api.recaptcha.net/noscript?k=6LcteLwSAAAAAK6oTvERh0RJCdggjp99sYdYhceE" height="300" width="500" frameborder="0"></iframe><br/>
  		<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
  		<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
	</noscript><div class="formClr"></div><span id="component93" class="formNoError">Invalid Input</span></td>
		<td></td>
	</tr>
 
	<tr>
		<td></td>
		<td><input type="submit" value="Submit" name="form[Submit]" id="Submit"  /><div class="formClr"></div></td>
		<td></td>
	</tr>
</table>
<input type="hidden" name="form[formId]" value="7"/></form>
sambeckham
Forum Newbie
Posts: 3
Joined: Wed Jan 26, 2011 4:14 am

Re: Sending Form values to PHP as an array

Post by sambeckham »

Oh I forgot to mention the error code I get when the form is submitted.
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'primary, id) VALUES('N;')' at line 1
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Sending Form values to PHP as an array

Post by AbraCadaver »

You don't serialize the array. You have specified the columns in your query so you have to specify the data for each column which will be the individual array elements:

/* etc. etc. */ is a comment meaning the remaing fields/vars.

Code: Select all

$f = array_map('mysql_real_escape_string', $_POST['form']);

$sql = "INSERT INTO enrollment (title, forename, surname, /* etc. etc. */ )
        VALUES('{$f['title']}', '{$f['forename']}', '{$f['surname']}', /* etc. etc. */ )";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Sending Form values to PHP as an array

Post by AbraCadaver »

Or maybe better:

Code: Select all

$columns = "`" . implode('`,`', array_map('strtolower', array_keys($_POST['form'])))   . "`";
$values  = "'" . implode("','", array_map('mysql_real_escape_string', $_POST['form'])) . "'";

$sql = "INSERT INTO enrollment ($columns) VALUES ($values)";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
sambeckham
Forum Newbie
Posts: 3
Joined: Wed Jan 26, 2011 4:14 am

Re: Sending Form values to PHP as an array

Post by sambeckham »

Cheers,

That would have worked a treat but then I got this error.
Warning: array_map() [function.array-map]: Argument #2 should be an array
So apparently it's not an array. Just a terribly named form. Possibly.

I'm tearing my hair out here. :(
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Sending Form values to PHP as an array

Post by AbraCadaver »

Where are you putting this PHP code?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply