简体   繁体   中英

INSERT INTO table1 values FROM table2 WHERE

I've looked around nothing seems to be working for me. I have a button when pushed it INSERTS data into 1 table-1, then it gets values from table-3 to put in table-2 where in they the ID is the same.

        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.";
        }

You see tableCSeats has more than 1 row that has the same ID meaning I want to insert multiple data from tableCSeats to tableSSeats. tableSSeats is a has no data in it yet.

盲目猜测,似乎您正在寻找INSERT ... SELECT语句。

check the return values of your queries. You always get "Succesfully added schedule." because you don't check if the queries were succesful. Ex:

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

or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM