INSERT INTO table1值FROM table2 WHERE

时间:2012-05-16 07:30:37

标签: php sql

我环顾四周,似乎没有什么对我有用。将INSERTS数据推入1表-1时,我有一个按钮,然后从表-3中获取值放入表-2中,其中ID是相同的。

        if ($movieTime != "") {
        $query = "SELECT SchedID FROM tblCinemaSched WHERE TheaterID='$tid' AND CinemaID='$cid' AND MovieDate='$date' AND MovieTime='$movieTime'";
        //echo "$query<br>";
        $result=$conn->executeUpdate($query);
        $numRows=$conn->numRows($result);
        if ($numRows<=0) {

            $query = "INSERT INTO tblCinemaSched SET TheaterID='$tid', CinemaID='$cid', MovieDate='$date', MovieTime='$movieTime', MovieID='$movieId', PriceAmt='$priceId', CrtBy='$username', CrtDate=NOW()";
            //echo "$query<br>";
            $result=$conn->executeUpdate($query);
            //get seat defaults from tblCSeats
            $query = "INSERT INTO tblSSeats SELECT TheaterID,  CinemaID, '$date', '$movieTime', SeatID, RowNo, ColumnNo, Handicap, Status, LeftSeat, RightSeat, NULL, NULL,NULL,NULL,NULL,NULL,NULL,'$username',NOW() FROM tblCSeats WHERE TheaterID='$tid' AND CinemaID='$cid'";
            //echo "$query<br>";
            $result=$conn->executeUpdate($query);

            $errorStr   = "Succesfully added schedule.";
        }
        else {
            $errorStr   = "There's already an existing schedule for the specified time.";
        }

您看到tableCSeats有多个具有相同ID的行,这意味着我想将多个数据从tableCSeats插入tableSSeats。 tableSSeats还没有数据。

2 个答案:

答案 0 :(得分:1)

盲目猜测,您似乎在寻找INSERT ... SELECT声明。

答案 1 :(得分:0)

检查查询的返回值。你总是得到“成功添加时间表”。因为您不检查查询是否成功。例如:

if(!$result=$conn->executeUpdate($query)) {
    die('error');
}

或类似的东西。