how to add/remove three level keywords using mysql and php? -
i want build 3 level keywords system every entry. suggested i've created 1 table (ctypes) categories' information , 1 (categories) relation table entry.
create table ctypes ( cat_id int unsigned not null auto_increment, cat_name varchar(20) not null, cat_level int unsigned not null, parent_id int unsigned, primary key (cat_id), unique (cat_name) ); create table categories ( entry_id int unsigned not null, cat_ids varchar(100) not null, unique (entry_id) );
then i've build form collect keywords information checkboxs.
<form action="add_category.php" method="post"> <table> <tr> <td><b>level 1(a,b,c)</b></td> <td><b>level 2(aa,ab,ac)</b></td> <td><b>level 3(aa1,aa2,aa3)</b></td> </tr> <tr> <td>a</td> <td>aa</td> <td><input type="checkbox" name="cat[]" value="5" />aa1<td> </tr> <tr> <td></td> <td></td> <td><input type="checkbox" name="cat[]" value="6" />aa2</td> </tr> <tr> <td></td> <td>ab</td> <td><input type="checkbox" name="cat[]" value="7" />ab1</td> </tr> ... </table> <input type="hidden" name="eid" value="1" /> <input type="submit" name="submit" value="submit" /> </form>
and i've create relation table entry_id , cat_id, how save $cat[] data table?
then, how handle edit(add/remove) keywords every entry, , how search entry (1st,2nd,3rd keywords)?
i'm not entirely sure you're trying achieve. has been pointed out, table structure doesn't ideal, without knowing exact end goal it's hard assist , advise.
i think main mystery why have 3 levels in each category, 1 checkbox 1 level.
ie have this:
<tr> <td>a</td> <td>aa</td> <td><input type="checkbox" name="cat[]" value="5" />aa1<td> </tr>
however, why can't select "a" or "aa"?
maybe intend, or maybe (as per many of comments) you're trying show basic example of trying achieve , change code necessary once have answer.
also, solution wont work need create array insert mysql, you're appending variable, insert 1 row entire array data 1 value.
to inputs this:
//initialise things can work them (and no errors) $arysqlqry = array(); $strerror = false; //make sure have posted data first if (isset($_post['cat']) && is_array($_post['cat'])) { foreach ($_post['cat'] $strcatkey => $strcatvalue) { //ensure data want, no sql injection etc if ($strcatvalue != 'aa1' && $strcatvalue != 'aa2' && $strcatvalue != 'ab1') { $strerror = true; } else { //clean data , construct sql values ready insert $strcatvalue = mysql_real_escape_string($strcatvalue); $arysqlqry[] = "('".$eid."', '".$strcatvalue."')"; } //if have error in array //stop loop further looping pointless if ($strerror == true) { break; } }//end foreach //if have new array results, , no errors //insert db //if not, wont run , error catch below work if (!empty($arysqlqry) && $strerror != true) { $qryquery = '"insert categories (entry_id, cat_ids) values '.implode(',', $arysqlqry).'"'; $qryexecute = mysqli_query($dbc, $qryquery); } } //end if $_post set , array else //(post not there error) { $strerror = true; } //if there errors if ($strerror == true) { //do error reporting //ie tell user nothing entered in form, , therefore nothing saved }
you can see working commenting out line $qryexecute
, echoing var $qryquery
, show constructed mysql query string , insert database.
as getting data back, simple select, using while or grab array , foreach etc.
as has been mentioned, feel necessary again, mysql_
extensions depreciated of php 5.5.0, , should consider using pdo or mysqli, prepared statements manageability , security.
Comments
Post a Comment