Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP: Passing checkbox values in MS Access db

Status
Not open for further replies.

bigcat48

Programmer
Aug 27, 2008
72
US
All - I am trying to complete a simple task in passing an array of data to my MS Access database and then display this information in a list page. I searched this and other forums & tutorials and cannot come up with a complete answer to this issue.

Here is the code:

Form page snippet
=================
Code:
<div class="row">
				<label>Event(s):</label>	
					<span><input type="checkbox" class="chkbx" name="events[]" value="forest" /><label>Forest</label></span>
					<span><input type="checkbox" class="chkbx" name="events[]" value="laurelvalley" /><label>Laurel Valley</label></span>
			</div>


Action page snippet
=================
Code:
<h2>Add Individual</h2>
		<?php
		// create database connection
		include("includes/connection.php");		
		
		// set variables
		$companyName = $_POST['companyName'];
		$firstName = $_POST['firstName'];
		$lastName = $_POST['lastName'];
		$events = serialize($_POST['events']);
		$compInfoUpdated = $_POST['compInfoUpdated'];
		$persInfoUpdated = $_POST['persInfoUpdated'];

		// query to update database
		$query = "INSERT INTO tblIndividuals (
		companyName, 
		firstName, 
		lastName, 
		events,
		compInfoUpdated, 
		persInfoUpdated)
		
		VALUES (
		'$companyName',
		'$firstName',
		'$lastName',
		'$events',
		'$compInfoUpdated',
		'$persInfoUpdated')";
		
		// run the query
		$rs = odbc_exec($conn, $query);
		 if(!rs)
			{exit("Error, insert query failed. No record(s) added.");}
		echo "<p>Record has been added.</p>";
		
		odbc_close($conn)
		?>

Thanks in advance.
 
So what is the problem?

and

How is the events field in your table defined?





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
When I submit to a form, I need to have my code to be able to insert either or both values to my database.

In my db table, the events field is defined as text.
 
To clarify,

I have created a form and now I'm attempting to create a PHP script that will take the selected values of the checkboxes and insert the information into a database table.

Once that has happened, I want the database values to be displayed in the list page.

Goal is for php to work with MS Access database
 
You code basically should do that, except the values for the check boxes will be serialized, So they may mean very little unless you de-serialize them upon retrieval and then display them.

But the concept is there, so again I ask, what problem are you having with it?






----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Confused on how to de-serialize on my list page. Please help.

Here is the code.
Code:
<table id="SortTable" class="tablesorter" width="950" style="table-layout:fixed">
			<thead>
				<tr>
				
					<th class="w190">Company Name</th>
					<th class="w100">First Name</th>
					<th class="w100">Last Name</th>
					<th class="w125">Title</th>
					<th class="w125">Event(s)</th>
					<th class="w60">Response</th>
					<th class="w100" colspan="2">Action</th>
				</tr>
			<tbody>
		
		<?php
		// create database connection
		include("includes/connection.php");
		  
		// call data to be viewed
		$sql="SELECT * FROM tblIndividuals ORDER BY tblIndividuals.lastName ASC";
		
		// submit the query
		$rs=odbc_exec($conn,$sql);
		
		// if query is invalid do this 
		if (!$rs)
		  {exit("Error in SQL");}
		  
		// if query is valid do this 
		while (odbc_fetch_row($rs))
		{
		  $id=odbc_result($rs,"ID");
		  $comp=odbc_result($rs,"companyName");
		  $fName=odbc_result($rs,"firstName");
		  $lName=odbc_result($rs,"lastName");
		  $title=odbc_result($rs,"title");
		  $events=odbc_result($rs,"events");
		  $response=odbc_result($rs,"response");
		  
		  echo "<td>$comp</td>";
		  echo "<td>$fName</td>";
		  echo "<td>$lName</td>";
		  echo "<td>$title</td>";
		  echo "<td>$events</td>";
		  echo "<td>$response</td>";
		  echo "<td class=\"center\"><a href=\"update_individual.php?id=$id\"><img src=\"images/edit-page-blue.gif\" title=\"Edit\" /></a></td>";
		  echo "<td class=\"center\"><a href=\"delete_individual-test.php?id=$id\" onclick=\"return confirm('Are you sure you wish to delete this record?');\"><img src=\"images/delete.png\" title=\"Delete\" /></a></td></tr>";
		}
		odbc_close($conn);
		?>
			</tbody>
		</table><br />
 
For starters, using the unserialize function to recreate the original array.

Code:
 $events=unserialize(odbc_result($rs,"events"));

Then you can just access the array normally.
Perhaps even explode it into a comma separated list if you need to.

Or check the values, and dynamically check the appropriate checkboxes if you are going to use it that way.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Still showing serialized text in my database. Is there anyway to prevent this?
 
Yes, don't use the serialize function on your variable when you insert it into the database.
Code:
        $companyName = $_POST['companyName'];
        $firstName = $_POST['firstName'];
        $lastName = $_POST['lastName'];
        [red]$events = serialize($_POST['events']);[/red]
        $compInfoUpdated = $_POST['compInfoUpdated'];
        $persInfoUpdated = $_POST['persInfoUpdated'];

        // query to update database
        $query = "INSERT INTO tblIndividuals (
        companyName,
        firstName,
        lastName,
        events,
        compInfoUpdated,
        persInfoUpdated)
...



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
If I shouldn't use the serialize function, what method should I use to pass an array of values into the database?

 
You can explode() it into a comma separated string, or keep using serialize, but use the unserialize function when you retrieve it from the database and want to display it.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I figured it out. I resolved this issue by researching on while loops, if then statements, for each, and implode/explode.

I wanted to take a step back to actually see how they all work. By doing that I have a better understanding that sticks.

Here is the code.

This is the entry form.

Code:
<h2>Add New Individual</h2>
		
		<form id="ContactForm" action="insert_individual.php" method="post">
			<p class="red bold">All fields are required.</p>
			<div class="row">
				<label>Company Name:</label>
					<select name="companyName">
						<option selected value="">-- select a company --</option>
						<?php include("includes/dsp_companyName.php"); ?>						
					</select>
			</div>
			
			...(represents more rows to insert)
	
			<div class="row">
				<label>Event(s):</label>
					<?php include("includes/dsp_events.php");?>
			</div>
						
			<div class="row">
				<label>Response:</label>	
					<span class="inline"><input type="radio" class="radio" name="response" value="yes" />Yes</span>
					<span class="inline"><input type="radio" class="radio" name="response" value="no" />No</span>
			</div>
			
			<input type="submit" value="Submit" id="submitbutton130" class="button positive" />
			
		</form>

Here is the insert to the database.

Code:
<h2>Add Individual</h2>
		<?php
		// create database connection
		include("includes/connection.php");		
		
		// set variables
		$companyName = $_POST['companyName'];
		$events = $_POST['events'];
		$response = $_POST['response'];
		
		// handle array data to post to database
			foreach ($_POST['events'] as $events) {
				$events = implode(", ",$_POST['events']);
			}
		
		// query to update database
		$query = "INSERT INTO tblIndividuals (
		companyName,
		events,
		response)
		
		VALUES (
		'$companyName',
		'$events',
		'$response')";
		
		// run the query
		$rs = odbc_exec($conn, $query);
		 if(!rs)
			{exit("Error, insert query failed. No record(s) added.");}
			
		echo "<p>Record has been added.</p>";
		
		odbc_close($conn)
		?>


I wanted to post what I've done to help others resolve their issue.

 
Glad you got it sorted out, and we appreciate posting back the final solution so others may benefit form it.

I do have to apologize though, as I told you to explode the array, when in reality you had to implode it. But seeing as you figured it out, then no harm was done.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top