HTML
<label for="wall_category">Wall Category:</label>
<input type="checkbox" name="wallpaper"> Wallpaper
<input type="checkbox" name="photography"> Photography
PHP
//obtains last insert id
$wallId = mysql_insert_id();
echo $wallId;
I have categories
table in my database in which the following data is stored:
category_id category_name
1 Wallpaper
2 Photography
and I have another table called wall_categories
in which:
wall_id (P, F) category_id (P, F)
After getting the wall_id from last_insert_id, I want to check what category among the two is checked. For example, Wallpaper
checkbox is checked, since the category_id
is 1
, I want to store that category_id
along with whatever the wall_id
will be to my wall_categories
table. So my table will become (lets say wall_id
is 16
)
wall_id (P, F) category_id (P, F)
16 1
And one last thing to add since Categories are checkboxes, when both are selected, I want to add both to my wall_categories
table so my wall_categories
table would become:
wall_id (P, F) category_id (P, F)
16 1
16 2
I was trying some workarounds, but I'm still a rookie in PHP. Would like to ask your help on how to do this. Thanks! (The script will just be used internally.)
HTML
<form action="process.php" method="POST">
<label for="category">Wall Category:</label>
<input type="checkbox" name="category[]" value="1"> Wallpaper
<input type="checkbox" name="category[]" value="2"> Photography
</form>
If you want to proceed checkboxes input, i suggest you to do it as the script above. category[]
would be an array that could contain values such as 1
and 2
(your category id).
So you have to parse it in server side(PHP) before you insert it into database. if you set the wall_id
column value automatically generated you will get some errors, because wall_id
would be different with previous wall_id
. So i suggest you to set it automatic but manually defined(didn't get my words? I'll explain it).
Here's the PHP :
<?php
$con = mysqli_connect("host","username","password","database_name");
//First get the category from input
$checked = $_REQUEST['category']; // it's an array
//--------get last
//Then you have to get the last wall_id from your wall_categories table
//and add it 1 (+1)
$q1 = mysqli_query($con,"SELECT MAX(wall_id) as last_wall_id from wall_categories"); // get last wall_id
$last = mysqli_fetch_array($q1);
$next_wall_id = $last['last_wall_id']+1;//+1 last wall_id for next wall_id
//-------end of get last
foreach($checked as $cat){
mysqli_query($con,"INSERT INTO wall_categories (wall_id, category_id) values ('$next_wall_id','$cat') ");
}
?>
If your wall_id
comes from another table, please skip get last
step. and remove/change the $next_wall_id
by the wall_id
variable that you get from another table.
That's all if you want to do it. Any question? found some errors? comment it below :D
The code that worked very well for me, used last_insert_id instead since I'm sure that the most recent id is what I need to insert to my wall_categories table. Thanks a lot @Oki Erie Rinaldi!!
<?php
$con = mysqli_connect("host","username","password","database_name");
//First get the category from input
$checked = $_REQUEST['category']; // it's an array
$wallId = mysqli_insert_id($con); //retrieves the last ID.
foreach($checked as $cat){
mysqli_query($con,"INSERT INTO wall_categories (wall_id, category_id) values ('$wallId','$cat') ");
}
?>