我有两张桌子:
表格帖子:
id cateid content
#### ###### ###############
14 35 Lorem isplum
分类表:
id name
#### #########
35 Sport
我通常使用方法从类别中获取所有帖子:
$itemSport = array();
$itemSport = getAllPostFromSportCate(35);
带有35
的是表格的值。
问题是:
当我从35
表中删除值Category
时。
我不能getAllPostFromSportCate($id)
因为Category
表不存在此值。
我不希望35
等插入值用于getAllPostFromSportCate($id)
。
如果用户更改id
表的Category
。所有逻辑都会失败。
问题2:
我不想传递35
等传递值。
正常,我在用户点击按钮时收到值。
像:
select * from Post where cateid = (select id from Category)
或
select * from Post where cateid = $_POST('idCate')
答案 0 :(得分:1)
显然,您无法从已删除其ID的类别中获取所有帖子,但您可以检查category_id(如果存在),然后获取其下的所有帖子。我想知道,为什么用户会更改类别的ID?
答案 1 :(得分:1)
看起来您的问题包含答案(好吧,有点):
select * from Post where cateid = $_POST('idCate')
您将构建一个列出类别的表单,该表单可以只使用类别的ID作为输入值。然后显示的文本将是类别名称。
但是,在执行数据库查询之前,您需要确保正确转义表单值,以防止SQL injection
答案 2 :(得分:0)
不要让任何人删除某个类别。让用户只更改它。
$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);
}
不会丢失任何帖子