在为新表添加另一行时,在3个表中插入一行

时间:2014-10-07 09:43:53

标签: php mysql

$fcname = isset($_POST["name"]) ? $_POST["name"] : "";
$fcshortname = isset($_POST["shortname"]) ? $_POST["shortname"] : "";
$fctext = isset($_POST["text"]) ? $_POST["text"] : "";
$fcfullimg = isset($_FILES["fullimg"]) ? $_FILES["fullimg"] : "";
$fccountry = isset($_POST["country"]) ? $_POST["country"] : "";
$fcgame = isset($_POST["game"]) ? $_POST["game"] : "";
$fcfirst = isset($_POST["first"]) ? $_POST["first"]: "";
$fcsecond = isset($_POST["second"]) ? $_POST["second"] : "";
$fcthird = isset($_POST["third"]) ? $_POST["third"] : "";
$fcgroups = isset($_POST["groups"]) ? $_POST["groups"] : "";
$fcngroups = isset($_POST["ngroups"]) ? $_POST["ngroups"] : "";
$fcbrackets = isset($_POST["brackets"]) ? $_POST["brackets"] : "";
$fcnbrackets = isset($_POST["nbrackets"]) ? $_POST["nbrackets"] : "";
$fcbracketsmode = isset($_POST["bracketsmode"]) ? $_POST["bracketsmode"] : "";

$new_window = isset($_POST['new_window']) ? 1 : 0;

if(isset($_POST['safe'])) {

$query = safe_query("INSERT INTO `".PREFIX."leagues` (`name`, `shortname`, `addedon`, `text`, `fullimg`, `country`, `game`, `first`, `second`, `third`, `groups`, `ngroups`, `brackets`, `nbrackets`, `bracketsmode`, `sortid`, `activated`, `new_window`) VALUES ('$fcname', '$fcshortname', NOW(), '$fctext', '$fullimg_name', '$fccountry', '$fcgame', '$fcfirst', '$fcsecond', '$fcthird', '$fcgroups', '$fcngroups', '$fcbrackets', '$fcnbrackets', '$fcbracketsmode', '$sortid', 1, $new_window )");
            if($query) {
                redirect('admincenter.php?site=leagues',$_language->module['fc_added'],2);
            } else {
                echo $_language->module['fc_added_error'];
            }

}

这是我现在的代码。我需要在3个括号表中插入一行,其中包含来自联赛表的ID值(来自AUTO_INCREMENT)。

通常我喜欢这样,但现在我不知道我必须用什么来替换值中的$ id。

safe_query("INSERT INTO `".PREFIX."brackets4` (`league`) VALUES ('$id')");
safe_query("INSERT INTO `".PREFIX."brackets8` (`league`) VALUES ('$id')");
safe_query("INSERT INTO `".PREFIX."brackets16` (`league`) VALUES ('$id')");

我该怎么办?

1 个答案:

答案 0 :(得分:0)

好吧,我找到了答案:D

$quera = mysql_query("SELECT id FROM `".PREFIX."leagues` ORDER BY id DESC LIMIT 1");
         while($res = mysql_fetch_array($quera)) {
               $ida = $res['id'];
               safe_query("INSERT INTO `".PREFIX."brackets4` (`league`) VALUES ('$ida')");
               safe_query("INSERT INTO `".PREFIX."brackets8` (`league`) VALUES ('$ida')");
               safe_query("INSERT INTO `".PREFIX."brackets16` (`league`) VALUES ('$ida')");
         }