Php MySQL多表更新语句

Php MySQL多表更新语句,php,mysql,sql,phpmyadmin,sql-update,Php,Mysql,Sql,Phpmyadmin,Sql Update,我在做学习计划。下面的SQL查询应该在登录用户试图从欢迎页面保存学习计划时运行。但是,代码没有任何作用。当我通过NetBeans运行应用程序时,输入值在提交时消失,没有初始化$result变量,没有标志更改,也没有任何错误打印输出 // Set up a flag for monitoring the individual queries $flag = true; $query = "UPDATE `modulecodes` LEFT JOIN `module

我在做学习计划。下面的SQL查询应该在登录用户试图从欢迎页面保存学习计划时运行。但是,代码没有任何作用。当我通过NetBeans运行应用程序时,输入值在提交时消失,没有初始化
$result
变量,没有标志更改,也没有任何错误打印输出

// Set up a flag for monitoring the individual queries
    $flag = true;

    $query = "UPDATE
    `modulecodes`
    LEFT JOIN `moduletitles` ON `moduletitles`.`modulecodeid` = `modulecodes`.`modulecodeid`
    LEFT JOIN `studyplans` ON `studyplans`.`modulecodeid` = `modulecodes`.`modulecodeid`
    LEFT JOIN `comments` ON `comments`.`modulecodeid` = `modulecodes`.`modulecodeid`
    SET
    modulecode = '$moduleCode', moduletitle = '$moduleTitle', studydate = '$moduleStudyDate', numberofstudyhours = '$moduleAllocatedHours', comment = '$studyPlanComments'
    WHERE userid = '$userid';";

    // Execute the query and put the result in the variable $result
    $result = mysqli_query($mysqli, $query);
    if (!$result) {
        $flag = false;
        echo $flag;
        echo "Error details for Result: " . mysqli_error($mysqli) . ".";
    }
…使用硬编码值(如下所示)和phpMyAdmin中的直接测试,它运行,但返回时有0行受影响。(查询耗时0.0069秒。)并且数据库中没有任何更改。所以,我觉得我没有很好地组织它。是否有人可以指导我如何将查询转换为一个带有子查询或更高效、更可靠的查询,请或至少帮助我指出我做错了什么

我怀疑代码的其他部分,但事实上,phpMyAdmin中的直接测试告诉我,问题在于SQL语句,此外,我已经在其他地方和另一个查询中使用了代码的其他部分,并且运行良好

UPDATE
    `modulecodes`
    LEFT JOIN `moduletitles` ON `moduletitles`.`modulecodeid` = `modulecodes`.`modulecodeid`
    LEFT JOIN `studyplans` ON `studyplans`.`modulecodeid` = `modulecodes`.`modulecodeid`
    LEFT JOIN `comments` ON `comments`.`modulecodeid` = `modulecodes`.`modulecodeid`
    SET
    modulecode = 'P00100', moduletitle = 'Java', studydate = '2017/04/10', numberofstudyhours = '1', comment = 'Java'
    WHERE userid = 20;
数据库代码:

CREATE TABLE `comments` (
  `commentid` int(10) NOT NULL,
  `modulecodeid` int(10) NOT NULL,
  `comment` text,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


-- --------------------------------------------------------

--
-- Table structure for table `modulecodes`
--

CREATE TABLE `modulecodes` (
  `modulecodeid` int(10) NOT NULL,
  `userid` int(10) NOT NULL,
  `modulecode` varchar(10) NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `moduletitles`
--

CREATE TABLE `moduletitles` (
  `moduletitleid` int(10) NOT NULL,
  `modulecodeid` int(10) NOT NULL,
  `moduletitle` varchar(100) NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `studyplans`
--

CREATE TABLE `studyplans` (
  `studyplan` int(10) NOT NULL,
  `modulecodeid` int(10) NOT NULL,
  `studydate` date NOT NULL,
  `numberofstudyhours` int(10) NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------


--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `userid` int(10) NOT NULL,
  `firstname` varchar(100) NOT NULL,
  `lastname` varchar(100) NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

要测试关系,请查看是否返回您希望更新的行:

SELECT 
    a.`modulecode`, 
    b.`moduletitle`, 
    c.`studydate`, 
    c.`numberofstudyhours`, 
    d.`comment`
FROM `modulecodes` a
    LEFT JOIN `moduletitles` b ON b.`modulecodeid` = a.`modulecodeid`
    LEFT JOIN `studyplans` c ON c.`modulecodeid` = a.`modulecodeid`
    LEFT JOIN `comments` d ON d.`modulecodeid` = a.`modulecodeid`
WHERE `userid` = '$userid';

如果任何列为NULL(未找到联接表行),则更新将失败。

1)使用参数化SQL;否则,您将接受SQL注入。所涉及的表的模式是什么?将更新重写为select会返回任何行吗?@xQbert坦率地说,我不知道你刚才提到的第一件事,但我会很快研究它。同时,您能否根据给定的SQL语句进行演示?@Sloachrisher我理解您问题的第一部分,现在已经包含了DB的SQL代码,但是,我不太理解您在第二个问题中提出的问题。请你浏览一下数据库代码,或者详细说明你的第二个问题,好吗?谢谢你添加了这个模式,但是你能把它限制在这个查询中涉及的表吗?@ChizzyMeka:那么,这是否返回了你想要的行,没有任何空值?嘿,谢谢你检查我。我觉得问题在于周围的PHP代码,所以我一直在试图找出问题所在。不过,非常感谢你的帮助。非常感谢。