Re: "interests" does not take multiple values into db

Posted 22 December 2012 - 10:03 PM

When I run the code, I get:

Quote

Notice: Array to string conversion in C:\wamp\www\insertform.php on line 78

Which is referring to $_POST['interests']. There are a couple of ways to fix that, depending on your DB setup. If you want the "interests" column in the database to look something like "museums, beaches", then you'll need to break up the array into a string that says just that. A for loop can help you with that:

FYI, mysql_* functions are soon to be deprecated and are vulnerable to SQL injections. PDO and mysqli are much better alternatives for database interactions.Dormilich made an amazing guide on PDO here.

Re: "interests" does not take multiple values into db

Posted 23 December 2012 - 10:24 AM

In response to sho ke's response there is a simpler way to turn an array into string data. The first method as an alternative to what he posted would be to use implode. This function allows you to take an array, and turn it into a string with a delimiter ie: a comma.

The next method of doing so would be to serialize or encode the information in string format. See json

<?php
$string = json_encode($_POST['interests'];
?>

When retrieving the information you will have to use json_decode() which will convert the item back into a php object, array or whatever you encoded.

The final approach would be to use a relational database. This is the best approach. and what it requires is you to create a model for another table that just hold individual interests and have a one to many join that returns the interests.

The final method is a little more complicated but it will allow you to perform queries like select most popular interests without having to do a string search. You could just do it by Nid.

This post has been edited by calebjonasson: 23 December 2012 - 10:30 AM