Forum Moderators: coopster

Message Too Old, No Replies

Inserting multiple checkbox values into db (yet again!)

The script only inserts "Array" into the database.

         

pablonet

8:38 am on Jan 16, 2004 (gmt 0)

10+ Year Member



Hello, I have followed along other threads regarding this topic. I have followed the examples pretty closely (I think) but still fail to get the desired effect. Instead of inserting the values of the checked checkboxes, the script inserts Array into the database. Here are the relevant code:


//update.php
<input type="checkbox" value="East Bay" name="region[]" <?php if (preg_match("/East Bay/", "$region")) { echo "checked"; } else { echo ""; }?>>East Bay
<input type="checkbox" value="North Bay" name="region[]" <?php if (preg_match("/North Bay/", "$region")) { echo "checked"; } else { echo ""; }?>>North Bay <br>
<input type="checkbox" value="South Bay" name="region[]" <?php if (preg_match("/South Bay/", "$region")) { echo "checked"; } else { echo ""; }?>>South Bay
<input type="checkbox" value="Peninsula" name="region[]" <?php if (preg_match("/Peninsula/", "$region")) { echo "checked"; } else { echo ""; }?>>Peninsula <br>
<input type="checkbox" value="Bay Area" name="region[]" <?php if (preg_match("/Bay Area/", "$region")) { echo "checked"; } else { echo ""; }?>>Bay Area
<input type="checkbox" value="San Francisco" name="region[]" <?php if (preg_match("/San Francisco/", "$region")) { echo "checked"; } else { echo ""; }?>>San Francisco <br>

Handled by:

//updated.php
include("dbinfo.inc.php");
mysql_connect('localhost',$username,$password);

$id=$_POST["id"];
$sregion = $_POST['region'];

$query="UPDATE intern_db SET region='$sregion' WHERE id='$id'";
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
echo "Record Updated";
mysql_close();

Any ideas? Thank you in advance.

BTW, is there a more efficient way to search for a string in a field than using preg_match?

warumauchnicht

10:07 am on Jan 16, 2004 (gmt 0)

10+ Year Member



Heloo pablonet,

you'll have to set up a while-loop to get each of the selected checkboxes:


while (list($k, $v) = each($region)) :
// put into mySQL
endwhile;

dcrombie

11:19 am on Jan 16, 2004 (gmt 0)



Try the PHP functions strstr [php.net] and implode [php.net].

;)

pablonet

5:05 am on Jan 17, 2004 (gmt 0)

10+ Year Member



Thank you for replying. I tried the while suggestion but the script added only one value to the database. I finally got it to work by lifting an example script from PHP and MySQL for Dynamic Web Sites:

//updated.php

include("dbinfo.inc.php");
mysql_connect('localhost',$username,$password);

$id=$_POST["id"];

if (isset($_POST['region'])) {
foreach ($_POST['region'] as $key => $value) {
$ints .= "$value, ";
}
$ints = substr($ints, 0, -2); //removes leading comma and space
}else {
echo 'You forgot to enter a region.';
}

$sregion = $ints;

$query="UPDATE intern_db SET region='$sregion' WHERE id='$id'";
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
echo "Record Updated";
mysql_close();

Thank you again... :o)

broniusm

3:52 am on Feb 8, 2004 (gmt 0)

10+ Year Member



So you were trying to insert a comma-delim array of values as a string, not to insert multiple records, one per checkbox?

I'm trying to solve the latter, and I'm leaning toward a foreach or while.

-bronius

pablonet

3:08 am on Feb 9, 2004 (gmt 0)

10+ Year Member



Yeah, I know that it is not the best way but I did not want to complicate myself any more than I had to. The database is small and simple. On a display page I use the preg_match function to find one item in the string, like this:

<input value="Bay Area" name="region[]" type="checkbox" <?php if (preg_match("/Bay Area/", "$region")) { echo "checked"; } else { echo ""; }?> >Bay Area

broniusm

3:22 pm on Feb 9, 2004 (gmt 0)

10+ Year Member



claro.. In case someone is looking to insert multiple records based on checkboxes in Dreamweaver MX 2004 using PHP, here's what I added to the insert Server Behavior:

$valuesSets = "";
for ($i=0; $i < count($asstcoachid); $i++) {
$valuesSets.="(".GetSQLValueString($teamid[$i], "int").",".GetSQLValueString($asstcoachid[$i], "string").",".GetSQLValueString($eventid[$i], "int")."),";
}
$valuesSets=substr($valuesSets,0,-1); // to remove last comma

$insertSQL = sprintf("INSERT INTO TeamAsstCoaches (teamid, asstcoachid, eventid) VALUES %s",
$valuesSets);

mysql_select_db($database_usatss, $usatss);
if ($valuesSets <> "") {
// echo $insertSQL;
$Result1 = mysql_query($insertSQL, $usatss) or die(mysql_error());
}


The main concepts here:
- name your input fields so PHP knows they're arrays:
"team[]", not "team"
- loop thru params based on checkbox:
asscoachid[] or $asscoachid in my case
- use MySQL multiple insert functionality:
INSERT INTO table (fld1, fld2, fld3) VALUES (val1_1, val2_1, val3_1), (val1_2, val2_2, val3_2) .. etc

Hope this helps someone down the road.