PHP连接MySQL 联表操作 增删改查
联表查询
select *,a.id from stu_message a inner join stu_detail b on a.id=b.sid
联表删除
delete a,b from stu_message a inner join stu_detail b on a.id=b.sid where a.id={$id}
联表修改
update stu_message a inner join stu_detail b on a.id=b.sid set a.username='{$name}',a.sex={$sex}, b.major='{$zhuanye}',b.address='{$address}' where a.id={$id};
多表插入(只能操作完一个表,再操作另一个)
1 // 第一个sql语句 2 $sql = "insert into stu_message(id,username,sex,age,class) value(null,'{$name}',{$sex},{$age},'{$class}')"; 3 // 发送sql语句 4 $res = mysqli_query($con,$sql); 5 // 返回上一个insert语句插入的id值 6 $sid = mysqli_insert_id($con); 7 // 第二个sql语句 8 $sql2 = "insert into stu_detail(id,sid,major,address,tel,creatime) value(null,{$sid},'{$zhuanye}','{$address}','{$tel}',{$creatime})"; 9 // 成功操作的结果数量,用来判断第一个sql语句插入成功与否 10 $num = mysqli_affected_rows($con); 11 if($num>0){ 12 $res2 = mysqli_query($con,$sql2); $num2 = mysqli_affected_rows($con); 13 if($num2>0){ 14 echo "<script>alert('信息创建成功'); 15 }else{ 16 echo "<script>alert('信息修改失败'); 17 } 18 }else{ 19 echo "<script>alert('信息修改失败'); 20 }