Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
blkthndr
Forum Newbie
Posts: 2 Joined: Sat Aug 24, 2002 11:52 am
Post
by blkthndr » Sat Aug 24, 2002 11:52 am
I am attempting to put in some checkbox info and pull out the values without much success. I have worked with text info just fine in the past and it goes in and comes out fine for me with the example below, however the checkbox info does not display anything.
Checkbox field:
Code: Select all
<tr>
<td colspan="1" class="regularright">
Years Applicable:
</td>
<td colspan="6" class="regular">
<input type="checkbox" name="yearsappї]" value="1989"> 1989
<input type="checkbox" name="yearsappї]" value="1990"> 1990
<input type="checkbox" name="yearsappї]" value="1991"> 1991
<input type="checkbox" name="yearsappї]" value="1992"> 1992
<input type="checkbox" name="yearsappї]" value="1993"> 1993
<input type="checkbox" name="yearsappї]" value="1994"> 1994
<input type="checkbox" name="yearsappї]" value="1995"> 1995
</td>
</tr>
My insert:
Code: Select all
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$sql = "INSERT INTO parts (partnumber, partdescription, yearsapp) VALUES ('$partnumber','$partdescription','$yearsapp')";
$result = mysql_query($sql);
I use phpmyAdmin and can see the years app field listed as "Array".
My query to pull it out:
Code: Select all
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM parts";
$result = mysql_query($query) or die("Invalid query");
$num_results = mysql_num_rows($result);
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
if ($i % 2)
{
echo "<tr bgcolor="E2E2EB"><td valign="top" class="regular">".stripslashes($rowї"partnumber"]); // partnumber
}
else
{
echo "<tr bgcolor="B8B8CE"><td valign="top" class="regular">".stripslashes($rowї"partnumber"]); // partnumber
}
echo "</td><td valign="top" class="regular">".stripslashes($rowї"partdescription"]);
echo "</td><td valign="top" class="regular">".stripslashes($rowї"yearsapp"]);
}
Any help here is appreciated.
Thanks in advance.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Aug 24, 2002 1:44 pm
try this
Code: Select all
<html><body>
<?php
if (isset($_POSTї'yearsapp']))
{
print('print($_POSTї''yearsapp'']): '); print($_POSTї'yearsapp']);
print('<pre>print_r($_POSTї''yearsapp'']): '); print_r($_POST); print('</pre>');
print ('imploded array:');
print ( implode(',', $_POSTї'yearsapp']) );
}
else
print('no data submitted yet');
?>
<form method="POST">
<table>
<tr>
<td colspan="1" class="regularright">
Years Applicable:
</td>
<td colspan="6" class="regular">
<input type="checkbox" name="yearsappї]" value="1989"> 1989
<input type="checkbox" name="yearsappї]" value="1990"> 1990
<input type="checkbox" name="yearsappї]" value="1991"> 1991
<input type="checkbox" name="yearsappї]" value="1992"> 1992
<input type="checkbox" name="yearsappї]" value="1993"> 1993
<input type="checkbox" name="yearsappї]" value="1994"> 1994
<input type="checkbox" name="yearsappї]" value="1995"> 1995
</td>
</tr>
<tr><td><input type="submit"/></td></tr>
</table>
</form>
</body></html>
$sql = "INSERT INTO parts (partnumber, partdescription, yearsapp) VALUES ('$partnumber','$partdescription','$yearsapp')";
this will replace $yearsapp by it's string representation and for variables of the type array this is "Array" not the contents of it
manual:
implode()
blkthndr
Forum Newbie
Posts: 2 Joined: Sat Aug 24, 2002 11:52 am
Post
by blkthndr » Sat Aug 24, 2002 3:09 pm
I inserted this:
Code: Select all
$yearsapp = implode(', ', $_POSTї'yearsapp']);
right before my insert command and it works just like I want it to work.
Thanks!