Forum Moderators: coopster

Message Too Old, No Replies

Update Multiple Checkbox Array In Database

Display and Serialize New Information

         

oceanwave

8:00 pm on Jul 14, 2007 (gmt 0)

10+ Year Member



Hi,

I have a form that has a few checkbox arrays in it (shortened here).

Heat: Please Check<br>
<input type="checkbox" name="heat[]" value="Electric">Electric&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="heat[]" value="Gas">Gas&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="heat[]" value="None">None&nbsp;&nbsp;&nbsp;&nbsp;
<br/><br/>

I had to serialize to get the selected choices in an array named "heat" in a database. It might look like this in the database:

a:2:{i:0;s:8:"Electric";i:1;s:13:"Oil Hot Water";}

Finally works. Now I want the user to be able to update the information in the database. So I have:

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

and I have the code to update my database (shortened here)...

mysql_query("UPDATE homes SET stnum='". $stnum ."', heat='". $heat ."', air='". $air ."' WHERE id='". $id ."'");

Now here's the tricky part. I want to display the checkbox information to the user to update. I want the user to see what they originally checked and didn't check, make the necessary changes, click submit, and get a serialized array back in the "heat" field. To get the checkbox information displayed to the user I found this code (shortened again):

<u>Heat:</u><br>
<?php
$experiences = array('Electric', 'Gas', 'None');

$heat = unserialize($row['heat']);

for ($i = 0; $i < count($experiences); $i++){
$checked = in_array($experiences[$i],$heat)? " checked" : "";
echo "<input type=\"checkbox\" name=\"experiences[$i]\" value=\"$experiences[$i]\"$checked> $experiences[$i] <br>\n";
}
?>
<br><br>

Displays perfectly the checked and unchecked choices according to the database.

The user makes a correction and unchecks and checks different checkbox choices, clicks SUBMIT, and that's when everything falls apart. The database field "heat" becomes "N;" instead of the chosen serialized array. When the page displays, I therefore get "Warning: in_array() [function.in-array]: Wrong datatype for second argument..." messages all over the page.

How do I change my existing code to update the "homes" database field "heat" with the changed, serialized, array information?

Thank you!

oceanwave

4:02 pm on Jul 16, 2007 (gmt 0)

10+ Year Member



Just changing the name of the checkbox to heat[] seemed to help. I have three different arrays on my page (heat, app, ft). All three programmed like below. With my last attempt (when the page loads), heat and app work, ft shows the repeated error messages for each value in ft:

Warning: in_array() [function.in-array]: Wrong datatype for second argument in ....

Did some reading and found others who got the same error message when using "in-array", but can't find a solution. What do I need to change in the script below?

<u>Heat:</u><br>
<?php
$experiences = array('Electric', 'Gas', 'None');

$heat = unserialize($row['heat']);

for ($i = 0; $i < count($experiences); $i++){
$checked = in_array($experiences[$i],$heat)? " checked" : "";
echo "<input type=\"checkbox\" name=\"heat[]\" value=\"$experiences[$i]\"$checked> $experiences[$i] <br>\n";
}
?>

RonPK

4:53 pm on Jul 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Warning: in_array() [function.in-array]: Wrong datatype for second argument in ....

That usually means that the second argument is not an array. Please post the relevant code if you can't get it to work.

oceanwave

11:37 pm on Jul 16, 2007 (gmt 0)

10+ Year Member



Hi RonPK,

Thanks for responding. To post the entire page would take a great amount of space, so I've used abreviations and shortened it here. A previous page posts the information to the database. The fields of "heat", "app", and "ft" are in the database as serialized arrays. The page below is the page that would update the database. This page is where I get the errors "Warning: in_array() [function.in-array]: Wrong datatype for second argument in ...."

<?php
include("sec.php");
include("config.php");

if($_POST['Submit']){

$f = $_SESSION['f'];
$l = $_SESSION['u'];
$id = $_POST['id'];
$br = $_POST['br'];
$ba = $_POST['ba'];
$heat=serialize($_POST['heat']);
$app=serialize($_POST['app']);
$ft=serialize($_POST['ft']);
$last_login = $_POST ['last_login'];

mysql_query("UPDATE db SET br='". $br ."', ba='". $ba ."', heat='". $heat ."', app='". $app ."', ft='". $ft ."', last_login= now() WHERE id='". $id ."'");

header("location:mypage.php");
exit;
}
$id=$_GET[id];

$result=mysql_query("select * from db where id='$id'");

$row=mysql_fetch_assoc($result);

mysql_close();
?>

<html>

<HEAD>
</HEAD>
<body bgcolor="#BCD2EE">

<form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF;?>">

BR :
<input name="br" type="text" id="br" value="<?php echo $row['br'];?>"/>
<br /><br />

BA :
<input name="ba" type="text" id="ba" value="<?php echo $row['ba'];?>"/>
<br /><br />

<u>Heat:</u><br>
<?php
$experiences = array('Electric', 'Gas', 'None', 'Oil Hot Air', 'Oil Hot Water');

$heat = unserialize($row['heat']);

for ($i = 0; $i < count($experiences); $i++){
$checked = in_array($experiences[$i],$heat)? " checked" : "";
echo "<input type=\"checkbox\" name=\"heat[]\" value=\"$experiences[$i]\"$checked> $experiences[$i] <br>\n";
}
?>
<br><br>

<u>App:</u><br>
<?php
$experiences1 = array('Comp', 'Dw', 'Dis', 'Mc');

$app = unserialize($row['app']);

for ($i = 0; $i < count($experiences1); $i++){
$checked = in_array($experiences1[$i],$app)? " checked" : "";
echo "<input type=\"checkbox\" name=\"app[]\" value=\"$experiences1[$i]\"$checked> $experiences1[$i] <br>\n";
}
?>
<br><br>

<u>Ft:</u><br>
<?php
$experiences2 = array('A On Pr', 'At', 'Bal', 'Be R', 'Car');

$ft = unserialize($row['ft']);

for ($i = 0; $i < count($experiences2); $i++){
$checked = in_array($experiences2[$i],$ft)? " checked" : "";
echo "<input type=\"checkbox\" name=\"ft[]\" value=\"$experiences2[$i]\"$checked> $experiences2[$i] <br>\n";
}
?>
<br><br>

Last Update: <?php echo $row['last_login'];?>
<p><hr color="blue"><br><b><font color="#CC3300" size="5">Click the SUBMIT button to save your changes.</font></b><br><br>
<input type="submit" name="Submit" value="SUBMIT" />
</p>
</form>
</body>
</html>

RonPK

9:49 am on Jul 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It seems that
$ft = unserialize($row['ft']);
does not return an array. You might want to check that by temporarily adding
print_r($ft);
after the unserialization.

You can avoid the error by checking whether $ft is an array. If not, make it an empty array for use in in_array():

$ft = unserialize($row['ft']); 
if (!is_array($ft)) {
$ft = array();
}
// rest of code...

oceanwave

3:46 pm on Jul 17, 2007 (gmt 0)

10+ Year Member



Hi RonPK,

Thank you so much. You were correct. I added the line print_r($#*$!); after each of the 3 arrays, and "ft" did not print out an array. The funny thing is, initially "heat" had the error messages, and "app" and "ft" worked fine. After I got "heat" working without the error messages, "ft" had the errors.

I also added your code:
$ft = unserialize($row['ft']); if (!is_array($ft)) { $ft = array(); } // rest of code...

after each of the unserializations. It immediately got rid of the error messages in "ft", leaving all the checkboxes unchecked.

I went back into the database and pasted in an "ft" array from another row into this record, just in case something was corrupted. I don't know why I had to do this.

By the way, the database for the 3 array fields look like:

ft text latin1_swedish_ci Null=Yes Default=NULL

I can't figure out what the problem was to cause these errors when all 3 arrays were programmed exactly the same way.

Thank you again RonPK.

whoisgregg

7:11 pm on Jul 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It sounds like you have different data overwriting what you expect to find in your variables.

Do you have register_globals [us.php.net] on by any chance?

echo 'register_globals = '.ini_get('register_globals').'</p>';

oceanwave

9:20 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



Hi whoisgregg,

Thank you for helping. Your code produced "register_globals = "

I looked in my php.ini file and it says register_globals = Off.

Is this a problem? Do I need to modify my page? If so, how?

I think the page is working now, but I can't be sure. The last time I thought it was working, it started throwing those pesky error messages.

WesleyC

5:04 pm on Jul 19, 2007 (gmt 0)

10+ Year Member



You definitely want to keep register globals off, as it creates severe security holes in any script. Also, scripts depending on register globals will break when the server they're on updates to PHP 6, as the setting is removed completely from it.

Pardon me if this has been asked already, but have you tried doing a var_dump or print_r on the value both directly before and directly after the unserialize() call?

oceanwave

11:24 pm on Jul 20, 2007 (gmt 0)

10+ Year Member



Hi WesleyC,

Thanks for the information.

print_r($ft); before $ft = unserialize($row['ft']); produced nothing

print_r($ft); after $ft = unserialize($row['ft']); produced the array of previously selected items