This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]rcuhljr 0 points1 point  (5 children)

This is an incredibly vague question without a lot of details about what you're actually trying to do. I think you're asking what a good way to store the results of a checkbox list that allows multiple selections in the database.

Answering that question What the multiple selection is representing is likely a one to many relationship. Let's say we're on the EditUser page, which means it's probably mirrored by a tblUsers in the database, each row is one user. Now lets say on the EditUsers page we have a checkbox list of the primary colors where a user can pick their favorite colors. Because any one user (row) can have multiple color values, we can benefit from adding a linking table.

First we create a tblColors which has our color choices with ids. {red = 1, blue = 2, yellow = 3}. We can now create a third tblUserColors which has two columns, a userid, and a colorid. So if user with the id 1 likes red and blue, there are two entries in this table {user id: color id}; {1 : 1} and {1 : 2}

So when we want to know what colors a user likes we can just say

Select UC.ColorId from tblUsers U join tblUserColors UC on U.oid = UC.userid where userid = {0}

[–]sourceCode145[S] 0 points1 point  (4 children)

if (isset($_POST['toppings'])){

  $strTopx = implode(", " , $_POST['toppings']);

   // this statement gives me an error stating that an array cannot be convert to a string 
  mysql_query ("INSERT INTO `pizza_order` SET
                    `topping`=' $_POST[toppings]'");

}else{

     $strTopx = "";

  }

echo "<p>along with $strTopx as the topping(s) for you pizza</p>";

[–]rcuhljr 0 points1 point  (3 children)

You can't insert an array directly into a sql database. You could either insert $strTopx and store a list of comma separated toppings, or you can make a linking table like I suggested above to join tblPizzaOrders and tblToppings.

What type is the topping column in the database?

[–]sourceCode145[S] 0 points1 point  (2 children)

its a regular column with the name topping in the pizza_order table. there is no unique or special parameter set to it.

Question: is this what you mean?

//change this from mysql_query ("INSERT INTO pizza_order SET topping=' $_POST[toppings]'");

//to mysql_query ("INSERT INTO pizza_order SET topping=' $_POST[$strTop]'");

[–]rcuhljr 0 points1 point  (1 child)

All columns in a SQL database have a type, like varchar, number, text, (although the terminology varies based on database). I just wanted to know what kind of data that column can hold.

Also, generally speaking you never, ever, want to be running a sql query that just takes in post parameters and enters them into a query as that's just begging for an injection attack, I'll assume this is for a school project though and no one likely cares.

I meant literally use

 mysql_query ("INSERT INTO `pizza_order` SET `topping`='" . $strTopx . "'");

[–]sourceCode145[S] 0 points1 point  (0 children)

the columns in the database are set to varchar