Forum Moderators: coopster

Message Too Old, No Replies

PHP forms - checkboxes and mysql

PHP forms - checkboxes and mysql

         

Tjobbe

10:29 am on May 18, 2005 (gmt 0)

10+ Year Member



I have created a form that submits company data to a table, this works fine no problems at all.

What I'd like to do, is have multiple checkboxes on the same form but the data is stored in a seperate table (same database).

what happens is, when a user goes to the search page, they click the drop down menu and select a product, upon submitting, a list of companies is generated that supply us with that particular product.

I would like to know what to name the check boxes, how to code the script.php file to post the data I need.

I am completely lost on this, and I have searched and searched but unable to find the answer I need, if anyone could help me, I'd be eternally grateful!

Thanks in advance,

Tjobbe

Here are my files:

database.sql

# phpMyAdmin SQL Dump
# version 2.5.6
# http://www.phpmyadmin.net
#
# Host: localhost
# Generation Time: May 18, 2005 at 12:11 AM
# Server version: 4.0.18
# PHP Version: 4.3.6
#
# Database : `database`
#

# --------------------------------------------------------

#
# Table structure for table `company`
#

CREATE TABLE `company` (
`companyid` tinyint(4) NOT NULL auto_increment,
`name` text NOT NULL,
`phone` text NOT NULL,
`web` text NOT NULL,
`notes` text NOT NULL,
PRIMARY KEY (`companyid`)
) TYPE=MyISAM AUTO_INCREMENT=4 ;

#
# Dumping data for table `company`
#

INSERT INTO `company` VALUES (1, 'example site 1', '01295788522', 'www.example.co.uk1', 'lsdfng podfgadv hgadpfh alduyoer gfjohgfh g');
INSERT INTO `company` VALUES (2, 'example site 2', '07789176017', 'www.example2.co.uk', 'dsfg');
INSERT INTO `company` VALUES (3, 'Home', '01295730112', 'localhost', 'this works');

# --------------------------------------------------------

#
# Table structure for table `coprod`
#

CREATE TABLE `coprod` (
`companyid` tinyint(4) NOT NULL default '0',
`productsid` tinyint(4) NOT NULL default '0'
) TYPE=MyISAM;

#
# Dumping data for table `coprod`
#

INSERT INTO `coprod` VALUES (1, 1);
INSERT INTO `coprod` VALUES (1, 2);
INSERT INTO `coprod` VALUES (1, 3);
INSERT INTO `coprod` VALUES (1, 4);
INSERT INTO `coprod` VALUES (2, 1);
INSERT INTO `coprod` VALUES (2, 2);
INSERT INTO `coprod` VALUES (2, 3);
INSERT INTO `coprod` VALUES (2, 4);
INSERT INTO `coprod` VALUES (3, 3);

# --------------------------------------------------------

#
# Table structure for table `products`
#

CREATE TABLE `products` (
`id` tinyint(4) NOT NULL auto_increment,
`name` text NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=5 ;

#
# Dumping data for table `products`
#

INSERT INTO `products` VALUES (1, 'bags');
INSERT INTO `products` VALUES (2, 'boxes');
INSERT INTO `products` VALUES (3, 'envelopes');
INSERT INTO `products` VALUES (4, 'tubes');

insert.htm (simplified version)

<html>
<head>
</head>
<center>
<form method="post" action="script.php">
<input type="hidden" name="id" value="null">
<table align="center">
<tr>
<td align="left"><div align="left"><strong>Company Name: </strong></div></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td align="left"><div align="left"><strong>Phone Number: </strong></div></td>
<td><input type="text" name="phone"></td>
</tr>
<tr><td>
<p align="left"><strong>Web site:</strong></td>
<td><input type="text" name="web"></td>
</tr>
<tr>
<td><div align="left"><strong>Notes:</strong></div></td>
<td><input type="text" name="notes"></td>
</tr>
<tr>
<td colspan="2"><div align="center">Bags
<input type="checkbox" name="checkbox" value="checkbox">
<!--
----------------

<INPUT TYPE="checkbox" NAME="data[Item][1]" VALUE="Bags"<?php echo(in_array("Bags", (array)($data["Item"]))? "CHECKED" : "");?>>
<INPUT TYPE="checkbox" NAME="data[Item][2]" VALUE="Boxes" <?php echo(in_array("Boxes", (array)($data["Item"]))? "CHECKED" : "");?>>
<INPUT TYPE="checkbox" NAME="data[Item][3]" VALUE="Envelopes"<?php echo(in_array("Envelopes", (array)($data["Item"]))? "CHECKED" : "");?>>

----------------
-->
</div></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="submit" type="submit" value="Enter record">
</div></td>
</tr>
</table>
</form>
</center>
</html>

script.php

<?
$DBhost = "localhost";
$DBName = "database";
$table = "company";
mysql_connect($DBhost) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select database $DBName");

$sqlquery = "INSERT INTO $table VALUES('$id','$name','$phone','$web','$notes')";

$results = mysql_query($sqlquery);

mysql_close();

print "<html><body><center>";
print "<p>You have just entered this record<p>";
print "<strong>Company Name :</strong> $name<br>";
print "<strong>Phone Number :</strong> $phone<br>";
print "<strong>Web Site :</strong> $web<br>";
print "<strong>Notes :</strong> $notes<br>";
print "</body></html>";
?>

[edited by: jatar_k at 9:49 pm (utc) on May 18, 2005]
[edit reason] generalized urls [/edit]

mcibor

12:30 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To check multiple checkboxes the best method is to make an array out of them.
In your form.html:
<input type="checkbox" name="check[]" value="value1">First checkbox<br>
<input type="checkbox" name="check[]" value="value2">Second checkbox
...

The [] is very important as it means that check is an array. You can put the value from php: value="<?php echo $value;?>"

The php script is such:

<?php $check = $_POST["check"];//you can add some slashes stripping, but the values can be only "on" or not set
//Then do you want the db table with one field, or as many fields as there are checkboxes?
//For one field:
$one_field = implode(";", $check); - implodes the array into string separated by ";".

//For many fields use
foreach($check as $key => $value)
{}//loop

Hope this helps you somehow.
I didn't write the exact script that'll do what you want, because I didn't want to get through your script. So this is just a start ahead
Best regards
Michal Cibor

Tjobbe

3:46 pm on May 18, 2005 (gmt 0)

10+ Year Member



How would I name the check box so that the database knows which is which?

The coprod tbalke, is where the products are stored, do i then name it like this by name or by its id number?

<input type="checkbox" name="check[]" value="bags">Bags

or like this:

<input type="checkbox" name="check[]" value="1">Bags

mcibor

8:47 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's no difference really.
If you have selected <input ... name="check[]" value="String"><input... value="1"> and you retrieve them by
$check = $_POST["check"] then what you become is an array:
$check = array("String", "1");

So to answer your question it's up to you if you prefer fetching the item by id or by name. I would recommend to always fetch such things by id, as name may be sometimes used somewhere else.

Tell me what do you want to do with the ids of the items? Do you want to select all the items that match? If yes, then do this. Store the id in checkboxes and then:

$ids = implode(",", $check);
$sql = SELECT * FROM table WHERE id IN('$ids')";//this query will select all the appropriate items with id selected.
Hope this helps!
Michal Cibor