php - 用 IF 同时插入两个表
问题描述
早上好,朋友!!
我正在尝试同时将数据插入两个表中。第一个条件 IF 可以,但第二个条件 ELSE 不行。我在下面包括我的插入代码。我已经用 ELSE 和 ELSEIF 尝试过,即使是 IF,但它不起作用。
if (isset($_POST['save')) {
if (@$_POST['id_confrontations'] == "") {
@$description = $_POST['description'];
@$dt_confrontation = $_POST['dt_confrontation'];
@$id_competitions = $_POST['id_competitions'];
@$id_stages = $_POST['id_stages'];
@$score1 = $_POST['score1'];
@$score2 = $_POST['score2'];
@$mandant_club = $_POST['mandant_club'];
@$visitor club = $_POST['visitor_club'];
@$situation = $_POST['situation'];
@$phase = $_POST['phase'];
@$id_trainers = $_POST['id_trainers'];
@$history = $_POST['history'];
//saves the record in the "confrontations" table - In this case insertion is perfect
$confrontations = "INSERT INTO confrontations
(description, dt_confrontation, id_competitions,
stadiums_id, score1, scoring2, binder_club,
situation, stage, id_trainers, history)
VALUES ('$description','$dt_confrontation','$id_competitions',
'$id_estadios','$score1','$score2',
'$mandant_club','$visitor_club','$situation',
'$stage','$id_trainers','$historia')';
//saves the record in the "panel" table - In IF insertion is perfect, but in ELSE it inserts as if it were IF
//In the IF rule the mandating_club would be == '1', so the GF would receive the score1 and the GC would receive the score2
//In the ELSE rule the visiting_club would be == '1', so the GC would receive the score1 and the GF would receive the score2
if(visiting_club != '1'){
$panel = "INSERT INTO panel (GF, GC, binant_club, visitor_club, id_competitions, status, status)
VALUES ('$score1','$scor2','$mandant_club','$visitor_club',
'$id_competitions','$id_states','$situation')";
}else{
$panel = "INSERT INTO panel
(GC, GF, mandant_club, visitor_club,
id_competitions, status, status)
VALUES ('$score1','$scor2','$mandant_club',
'$visitor_club','$id_competitions',
'$id_states','$situation')";
}
谢谢大家的关注和建议。
解决方案
您的代码有大量错误数据和缺少变量
这个变量@$visitor club = $_POST['visitor_club']; 应该@$visitor_club
您声明变量 @$history = $_POST['history']; 但是在将数据插入 [confrontations] 表时调用 $historia 变量。
您开始 ["] 双引号将数据插入表 [confrontations],但结束 ['] 单引号。
您的表格 [confrontations] 的插入代码。您声明 [11] 列名,但您声明 [12] 列值。因此,您现在应该在数据插入代码中使用相同的列名和值。
当您将数据插入 [panel] 表时,您的代码如下
INSERT INTO 面板(GF、GC、binant_club、visitor_club、id_competitions、status、status)
这里最后两列(状态,状态)名称相同。您不能使用相同的列名插入数据。
注意:如果你想用 MySQL 创建一个插入代码。转到 PHP MyAdmin 并选择您的数据库 >> 选择您的表 >> 单击插入 >> 插入一个虚拟数据 >> 现在 MySQL 为 PHP 生成一个自动插入代码。通过更改列值将此代码用于插入数据。
谢谢。
推荐阅读
- python - 如何将二进制字符串列表放入二维数组
- sql-server - 存储过程,子查询返回超过 1 个值
- r - R:带有隐藏字段的 Web 抓取 .aspx 表单,“未知字段名称”错误
- mysql - 错误代码:1822。添加外键约束失败。引用表 'Works' 中的约束 'project_ibfk_1' 缺少索引
- python - Geopandas - 在与多边形相交处分割线,在新链接属性中保留多边形 ID
- python - 如何使用 BeautifulSoup 加快解析速度?
- javascript - 尝试提取连续子数组时返回的循环对象数组
- python - Python在表格行中查找数据
- javascript - React textarea组件无法水平扩展
- gemfire - 两个不同局域网的 Gemfire 配置