4 Replies - 347 Views - Last Post: 23 December 2012 - 10:24 AM Rate Topic: -----

#1 nainas9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 12

"interests" does not take multiple values into db

Posted 22 December 2012 - 08:46 PM

I have attached the code here. Please tell me the changes to be made.

Attached File(s)


Is This A Good Question/Topic? 0
  • +

Replies To: "interests" does not take multiple values into db

#2 Sho Ke  Icon User is online

  • D.I.C Head
  • member icon

Reputation: 107
  • View blog
  • Posts: 246
  • Joined: 13-October 11

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

Posted 22 December 2012 - 09:04 PM

Have you tried anything? Any errors?
Was This Post Helpful? 0
  • +
  • -

#3 nainas9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 12

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

Posted 22 December 2012 - 09:16 PM

no errors. but just that interests isn't taking multiple values
Was This Post Helpful? 0
  • +
  • -

#4 Sho Ke  Icon User is online

  • D.I.C Head
  • member icon

Reputation: 107
  • View blog
  • Posts: 246
  • Joined: 13-October 11

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:
$interests = "";
for($i = 0; $i < count($_POST['interests']); $i++) {
	$interests .= $_POST['interests'][$i];
	if(isset($_POST['interests'][$i+1])) {
		$interests .= ", ";
	}
}
echo $interests;



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.

This post has been edited by Sho Ke: 22 December 2012 - 10:06 PM

Was This Post Helpful? 3
  • +
  • -

#5 calebjonasson  Icon User is offline

  • $bert = new DragonUnicorn(); $bert->rawr();
  • member icon

Reputation: 207
  • View blog
  • Posts: 988
  • Joined: 28-February 09

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.


<?php
if(!empty($_POST['interests']) && is_array($_POST['interests'])) {
    $string = implode(",", $_POST['interests']);
}
?>


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

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1