Forum Moderators: coopster

Message Too Old, No Replies

Insert items into MySql db via Web form

Insert multiple items into a "SET" column with PHP

         

Ernos

6:56 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



I'm playing around with a very basic, bare-bones homegrown CMS (keeping it REALLY simple as I'm still a helpless, self-taught newbie), and I'm hoping someone can tell me how to do what I'm trying; I've looked at the relevant spots on mysql.com and php.net but can't seem to find a specific answer on this.

I've set up a simple Web form to insert articles into the database, passing along things like the headline, author's name, text of article, etc. In the table that holds everything, I also have a column for "category" and I made it a SET column (instead of ENUM) because I figured some articles would fall under multiple categories.

At first, I just used a <SELECT NAME="category"> on the form page to choose a single category for each article, just because I was trying to keep it simple while I played with it, and the form works great for that. On the submit.php page, it has:


if (isset($article) && $article!='')
{// This is for updating existing articles
$query = "update news
set
headline = '$headline',
article_text = '$article_text',
category = '$category',
where
id = $article";
}
else
{// This is for adding a new article
$query = "insert into news
(headline, article_text, category)
values
('$headline', '$article_text', '$category')";
}

So far, so good, but then I tried switching the form from <SELECT> to <INPUT TYPE="checkbox" NAME="category"> for each category, so that the user can select multiple categories, and of course right now it still only inserts one of the categories into the table.

What to I need to put in the script to let it retrieve all the categories that a user might assign to it from the form, and put them all in the table? I'm thinking explode might be involved somehow, or maybe a for to loop through the categories, but can't figure it out.

Oh, I have tried searching these forums for a solution as well, I just think my search-fu isn't very good. "php mysql set form" and the like didn't turn up anything similar to what I'm trying, so far.

jatar_k

7:32 pm on Mar 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how about a tip not really an answer.

A select sends one single variable through and your checkboxes will send none/1/many in an array.

Try this in the beginning of the script your form posts to

echo "<pre>";
print_r($_POST);
echo "</pre>";
die();

all this will do is spit out the $_POST array. Look carefully at what is sent to your script for the three different selections of 0, 1 or many selected checkboxes. This is the best way to understand how to deal with your submitted information to build your query.

I could just give you the code too but this accomplishes more than one purpose. :)

[edited by: jatar_k at 8:12 pm (utc) on Mar. 6, 2004]

Ernos

8:03 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



Tips are always welcome, especially since I'd certainly hope to figure things out on my own eventually. :)

Unfortunately, this didn't tell me anything I didn't already know, really; What displayed was


[category] => Leisure

("Leisure" being the last of the many categories that were selected.) So, yeah. Only one category going through, though many are selected with checkboxes. Argh.

I tried to do a for loop, something like


for ($i=0;$i<count($category);$i++);
{
$values.="($category[$i])";
}
$values=substr($values,0,-1);

and for the query,

$query = "insert into news
(headline, article_text, category)
values
('$headline', '$article_text', '$values')";
}

But every time I tried that, tweaking all over the place, it still inserted only one category. I also tried putting [] in the form itself, <INPUT TYPE="checkbox" NAME="category[]" VALUE="blahblah"> as I've seen mentioned a couple of places, but no dice. I hope at least I'm on the right track with a loop...

jatar_k

8:14 pm on Mar 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



my test script

checkform.html

<form name="checktest" action="checkform.php" method="post">
<p><INPUT TYPE="checkbox" NAME="category[]" VALUE="cat1"> 1
<p><INPUT TYPE="checkbox" NAME="category[]" VALUE="cat2"> 2
<p><INPUT TYPE="checkbox" NAME="category[]" VALUE="cat3"> 3
<p><INPUT TYPE="checkbox" NAME="category[]" VALUE="cat4"> 4
<p><input type="submit" value="sub it">
</form>

checkform.php

<?
echo "<pre>";
print_r($_POST);
echo "</pre>";
die();
?>

with all 4 checkboxes selected I get this output

Array
(
[category] => Array
(
[0] => cat1
[1] => cat2
[2] => cat3
[3] => cat4
)

)