Hi,

I have done a search, but can't seem to apply any of the suggestions to my specific case... I am somewhat of a newbie, so please excuse my ignorance...

I am trying to insert multiple values from checkboxes into one table column...

here is the code I have:

<?php $sql = "INSERT INTO survey1 (name,email,bestsellers) VALUES ('$name','$email','$bestsellers')";
  // run SQL against the DB
  $result = mysql_query($sql);?>

<p><strong>Jewelry Buying Survey</strong></p>
<form name="survey_says" method="post" action="<?php echo $PHP_SELF?>">

	<input type="checkbox" name="bestsellers[]" value="Gold">&nbsp;Gold<br>
	<input type="checkbox" name="bestsellers[]" value="Silver">&nbsp;Silver<br>
	<input type="checkbox" name="bestsellers[]" value="Leather">&nbsp;Leather<br>
	<input type="checkbox" name="bestsellers[]" value="Gemstones">&nbsp;Gemstones<br>
	<input type="checkbox" name="bestsellers[]" value="Costume/Bridge">&nbsp;Costume/Bridge<br>
	<input type="checkbox" name="bestsellers[]" value="Earrings">&nbsp;Earrings<br>


this is just a small piece of a fairly large form. the result i get from the above code is simply "Array" in the bestsellers column.

anyone be able to update my code so this works right? also, how would i then display it on my results page?

thanks!

    Try this instead, serialize the array before inserting. This implies you will need to unserialize upon retreival of the information.

    $sql = "INSERT INTO
        survey1 (name,email,bestsellers)
    VALUES('$name',
        '$email',
        '" . mysql_escape_string(serialize($bestsellers)) . "')"

    Alternatively you could use implode:

    $sql = "INSERT INTO
        survey1 (name,email,bestsellers)
    VALUES('$name',
        '$email',
        '" . mysql_escape_string(implode(", ", $bestsellers)) . "')"

      cool, thanks...

      so this is what got inserted when i serialized it... a:3:{i:0;s:7:"Leather";i:1;s:5:"Cuffs";i:2;s:5:"Rings";}

      so now how would i output it... here is my current output code:

         
      
      <?php $result = mysql_query("SELECT * FROM survey1 WHERE id=$view",$db);
      	if ($myrow = mysql_fetch_array($result)) {?>
      <table>
      	<tr>
      		<td>
      			<strong>Buyer name</strong><br>
      			<?php print $myrow["name"]?><br>
      			<br>
      
      	</td>
      	<td>
      		<strong>Buyer email</strong><br>
      		<?php print $myrow["email"]?></a><br>
      		<br>
      	</td>
      </tr>
      <tr>
      	<td colspan="2"><?php print $myrow["bestsellers"]?></td>
      </tr>
      </table>
      <?php } ?>
      
      

      THANKS!

        Serialising an array into a database column is not the prefered way of storing information as it makes it very dificult to query on that data, and the query almost always results in a full table scan which is about the slowest thing you can do with a db.

        The way to store this information is as multiple rows in a second related table. Then you can easily index it and search for selective information efficiently; like find all the people who ticked 'leather' and/or 'rings'. You can even do complex number crunching, which I take it is the purpose of the survey in the first place.

          actually, the implode function accomplished exaclty what i wanted (inserting a list with commas)... excellent!

          thanks guys!

            Write a Reply...