手工编码的表名可以工作,但动态名称在PHP中不起作用

手工编码的表名可以工作,但动态名称在PHP中不起作用,php,Php,这是从表中删除学生的代码。 但它不起作用。 它仅在表名为静态时有效 public function delete_student($student_class,$school_session,$id){ $table_name = $student_class."_".$school_session."_student_record"; $pdo = KITE::getInstance('pdo'); $query = $pdo->prepare("DELETE F

这是从表中删除学生的代码。 但它不起作用。 它仅在表名为静态时有效

public function delete_student($student_class,$school_session,$id){
    $table_name = $student_class."_".$school_session."_student_record";
    $pdo = KITE::getInstance('pdo');
    $query = $pdo->prepare("DELETE FROM ".$table_name." WHERE `id` = ?");
    $query->bindValue(1,$id);
    $query->execute();

}

正如我在一篇评论中提到的,在表、列、函数名、过程名等的名称中不允许使用斜杠
/
。那么,当名称是静态类型时,为什么您的查询可以工作是一个谜

这是一个快速构建的演示,它使用带有外键的
innodb
表,希望很少/没有冗余。如果您在本地运行mysql,请尝试在gui(例如)中运行它,了解如何实现它

drop database if exists `demo_school_sessions`;
create database if not exists `demo_school_sessions`;
use `demo_school_sessions`;


drop table if exists `classes`;
create table if not exists `classes` (
  `id` int(10) unsigned not null auto_increment,
  `class` varchar(50) not null,
  primary key (`id`)
) engine=innodb default charset=latin1;

insert into `classes` (`id`, `class`) values
    (1, 'mathematics'),
    (2, 'physics'),
    (3, 'computing');


drop table if exists `sessions`;
create table if not exists `sessions` (
  `id` int(10) unsigned not null auto_increment,
  `session` varchar(50) not null,
  primary key (`id`)
) engine=innodb default charset=latin1;

insert into `sessions` (`id`, `session`) values
    (1, 'summer'),
    (2, 'winter'),
    (3, 'spring'),
    (4, 'autumn');

drop table if exists `students`;
create table if not exists `students` (
  `id` int(10) unsigned not null auto_increment,
  `student` varchar(50) not null,
  primary key (`id`)
) engine=innodb default charset=latin1;

insert into `students` (`id`, `student`) values
    (1, 'bobby droptables'),
    (2, 'sue select'),
    (3, 'david delete'),
    (4, 'valerie view'),
    (5, 'peter procedure'),
    (6, 'freddy function');

drop table if exists `year`;
create table if not exists `year` (
  `id` int(10) unsigned not null auto_increment,
  `year` varchar(9) not null default '2014/2015',
  primary key (`id`)
) engine=innodb default charset=latin1;

insert into `year` (`id`, `year`) values
    (1, '2014/2015'),
    (2, '2015/2016'),
    (3, '2016/2017'),
    (4, '2017/2018'),
    (5, '2018/2019'),
    (6, '2019/2020');

drop table if exists `master`;
create table if not exists `master` (
  `id` int(10) unsigned not null auto_increment,
  `student` int(10) unsigned not null default '0',
  `class` int(10) unsigned not null default '0',
  `session` int(10) unsigned not null default '0',
  `year` int(10) unsigned not null default '0',
  primary key (`id`),
  key `student` (`student`),
  key `class` (`class`),
  key `session` (`session`),
  key `year` (`year`),
  constraint `fk_year` foreign key (`year`) references `year` (`id`) on delete cascade on update cascade,
  constraint `fk_class` foreign key (`class`) references `classes` (`id`) on delete cascade on update cascade,
  constraint `fk_session` foreign key (`session`) references `sessions` (`id`) on delete cascade on update cascade,
  constraint `fk_stud` foreign key (`student`) references `students` (`id`) on delete cascade on update cascade
) engine=innodb default charset=latin1;

insert into `master` (`id`, `student`, `class`, `session`, `year`) values
    (1, 1, 3, 4, 1),
    (2, 1, 1, 4, 1),
    (3, 2, 2, 4, 1),
    (4, 2, 3, 4, 1);


/* example to select records */
select * from `master` m
  left outer join `students` s on s.id=m.student
  left outer join `classes` c on c.id=m.class
  left outer join `sessions` ss on ss.id=m.`session`
  left outer join `year` y on y.id=m.`year`
where m.`class`=3 or m.`student`=1;

/* Example to delete a student */
delete from `students` where `id`=1;


/*

*/

由于
外键依赖关系
如果从学生表中删除学生,则
主表
中的所有相关记录也将被删除,因为
更新和
删除都设置了
级联
。像这样使用
外键
时必须小心,因为如果您意外删除了一条记录,并且该键上设置了级联删除,则所有相关记录都会消失

打印表名并检查它是否等于静态表名。在我看来,这是一个可怕的数据库设计roost@pupil,我已经印好了。你能举个例子说明一下表名在所有方面都是什么样的吗?我敢肯定表名中不允许使用斜杠!哇,你为这个答案付出了不少努力。希望你不介意,我做了一个小的编辑,这样它就可以开箱即用了,只是删除了
自动增量
,并将
主表
表定义移到了创建的最后一个表,因为如果没有先创建其他表,所有外键的东西都不起作用。为了努力,我得到了一个加分1。我希望这家伙能感激所有的努力call@RiggsFolly-希望它能帮助OP了解如何更好地构建db的要点…我有点怀疑,但我们生活在希望中。谢谢@RamRaider。我刚刚获得了一个关于如何与数据库表交互的新概念。谢谢你的努力。我真的很感激你的愚蠢,谢谢你的补充。谢谢你的时间。