0

I have two table:

Table Post:

    id              cateid            content
   ####             ######        ###############
    14                35           Lorem isplum

Category table:

    id              name
   ####          #########
    35              Sport

I usually using method to get all post from category like this:

$itemSport = array();
$itemSport = getAllPostFromSportCate(35);

with 35 is values of table.

Problem is:

  1. When I delete values 35 from Category table. I can't getAllPostFromSportCate($id) because Category table not exist this values.

  2. I don't want insert values like 35 to function getAllPostFromSportCate($id). If user change id of Category table. All logic will fail.

Problem 2:

I don't want pass values like 35.

Normal, I receive values when user click button.

Like:

select * from Post where cateid = (select id from Category)

or

select * from Post where cateid = $_POST('idCate')
Ave
  • 4,338
  • 4
  • 40
  • 67
  • can you explain your problem more clearly? – Nishant Nair Jan 20 '17 at 08:46
  • First problem: Just check if the `$id` exists before you try to find data from SQL. Second problem: As Nishant said, please explain more clearly. I don't understand what's your problem.. – Twinfriends Jan 20 '17 at 08:49
  • use concept of `cascade on delete,update ` of mysql. – Hikmat Sijapati Jan 20 '17 at 08:50
  • Why are you hard coding `35`? You can have a Category selection (select box - where you show all available categories) and based on the selection you can filter records right? – masterFly Jan 20 '17 at 08:53
  • @masterFly, in your case, user select a values available categories. If user not interactive with UI. – Ave Jan 20 '17 at 09:01
  • Ok, then according to your logic from where you get the id `35`? – masterFly Jan 20 '17 at 09:03
  • @masterFly I must open `phpMyadmin`, select the table `Category`, look all rows in this table to choose category correct with my conditional. I must hard code value `35` to my code. I don't want this. – Ave Jan 20 '17 at 09:20
  • So what is you logic from start to end? Do you want to display posts for all available categories in the category table? – masterFly Jan 20 '17 at 09:25
  • Nope. Only display all posts of one categories. – Ave Jan 20 '17 at 09:33
  • So where you would get that 1 category ID? – masterFly Jan 20 '17 at 11:41

3 Answers3

2

It looks like your question contains the answer (well, kind of):

select * from Post where cateid = $_POST('idCate')

You would build a form which lists the categories, that form could just use the ids of the categories as input values. Then the text shown for them would be the category name.

However, you need to make sure you properly escape the form values before you perform the database query, to prevent SQL injection

Community
  • 1
  • 1
aross
  • 3,325
  • 3
  • 34
  • 42
  • If I want get a array contain list Post with `cateid = 35`. In my question, I tried to example with user interactive with UI like select a `combobox` or click save button (event after click contain values). My case not like this. – Ave Jan 20 '17 at 09:13
  • @vanloc So you select based on category name? – aross Jan 20 '17 at 09:30
1

Obviously you cannot get all the post from a category which their id has been deleted but you can check the category_id if it exists then get all the posts under it. I'm wondering, why would a user change the id of a category?

0

Dont let anyone delete a category. Let the user only change it.

$a_categories = array(1, 3, 4, 5, 9, ...); // get the existing ids from the database 

if($i_category AND in_array($i_category, $a_categories)){
 // your change of category code here
 // mysqli_query($link, "UPDATE posts SET cateid = ".$i_category." WHERE id = ".$i_postnumber);
}

No posts will be lost then

Bernhard
  • 1,852
  • 11
  • 19