座位预订系统

时间:2013-08-05 04:05:03

标签: php mysql

所以,我建立了一个座位预订系统,每个座位都有复选框,用户选择此框,座位被视为保留。

但现在我希望让用户能够为每个标记的座位命名。我怎么能这样做?

seats.php

<?php include("login.php"); ?>
<html>
<head>
    <title>Tickets</title>
    <style>
        * {
            font-size: 11px;
            font-family: arial;
        }
    </style>
    <script>

        function reserveSeats() {

            var selectedList = getSelectedList('Reserve Seats');

            if (selectedList) {
                if (confirm('Do you want to reserve 
                               selected seat/s ' 
                             + selectedList + '?')) {
                    document.forms[0].oldStatusCode.value=0;
                    document.forms[0].newStatusCode.value=1;
                    document.forms[0].action='bookseats.php';
                    document.forms[0].submit();
                } else {
                    clearSelection();
                }
            }
        }


        function cancelSeats() {

            var selectedList = getSelectedList('Cancel Reservation');

            if (selectedList) {
                if (confirm('Do you want to cancel reserved seat/s ' + selectedList + '?')) {
                    document.forms[0].oldStatusCode.value=1;
                    document.forms[0].newStatusCode.value=0;
                    document.forms[0].action='bookseats.php';
                    document.forms[0].submit();
                } else {
                    clearSelection();
                }
            }
        }


        function confirmSeats() {

            var selectedList = getSelectedList('Confirm Reservation');

            if (selectedList) {
                if (confirm('Do you want to confirm reserved seat/s ' + selectedList + '?')) {
                    document.forms[0].oldStatusCode.value=1;
                    document.forms[0].newStatusCode.value=2;
                    document.forms[0].action='bookseats.php';
                    document.forms[0].submit();
                } else {
                    clearSelection();
                }
            }
        }


        function getSelectedList(actionSelected) {

            // get selected list
            var obj = document.forms[0].elements;
            var selectedList = '';
            for (var i = 0; i < obj.length; i++) {
                if (obj[i].checked && obj[i].name == 'seats[]') {
                    selectedList += obj[i].value + ', ';
                }
            }

            // no selection error
            if (selectedList == '') {
                alert('Please select a seat before clicking ' + actionSelected);
                return false;
            } else {
                return selectedList;
            }

        }

        function clearSelection() {
            var obj = document.forms[0].elements;
            for (var i = 0; i < obj.length; i++) {
                if (obj[i].checked) {
                    obj[i].checked = false;
                }
            }
        }


        function refreshView() {
            clearSelection();
            document.forms[0].action='<?php echo $_SERVER['PHP_SELF']; ?>';
            document.forms[0].submit();
        }

    </script>
</head>
<body>
<table>
<tr><td width="100%" align="center">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<input type="hidden" name="oldStatusCode" value=""/>
<input type="hidden" name="newStatusCode" value=""/>

<table width='100%' border='0'>
    <tr><td align='center'>
        <input type='button' value='Refresh View' onclick='refreshView();'/>
    </td></tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<table width='100%' border='0'>
    <tr><td align='center'>
        <input type='button' value='Reserve Seats' onclick='reserveSeats()'/>
        &nbsp;<input type='button' value='Confirm Reservation' onclick='confirmSeats()'/>
        &nbsp;<input type='button' value='Cancel Reservation' onclick='cancelSeats()'/>
    </td></tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<table width='100%' border='0'>
    <tr><td align='center'>
        <input type='button' value='Clear Selection' onclick='clearSelection()'/></td>
    </tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<?php
/*
 * Created on Mar 17, 2007
 * Author: dayg
 */

$linkID = @ mysql_connect("localhost", "tickets", "tickets") or die("Could not connect to MySQL server");
@ mysql_select_db("tickets") or die("Could not select database");
/* Create and execute query. */
$query = "SELECT * from seats order by rowId, columnId desc";
$result = mysql_query($query);
$prevRowId = null;
$seatColor = null;
$tableRow = false;
//echo $result;
echo "<table width='100%' border='0' cellpadding='3' cellspacing='3'>";
while (list($rowId, $columnId, $status, $updatedby) = mysql_fetch_row($result))
{
    if ($prevRowId != $rowId) {
        if ($rowId != 'A') {
            echo "</tr></table></td>";
            echo "\n</tr>";
        }
        $prevRowId = $rowId;
        echo "\n<tr><td align='center'><table border='1' cellpadding='8' cellspacing='8'><tr>";
    } else {
        $tableRow = false;
    }
    if ($status == 0) {
        $seatColor = "lightgreen";
    } else if ($status == 1 && $updatedby == 'user1') {
        $seatColor = "FFCC99";
    } else if ($status == 1 && $updatedby == 'user2') {
        $seatColor = "FFCCFF";
    } else if ($status == 2 && $updatedby == 'user1') {
        $seatColor = "FF9999";
    } else if ($status == 2 && $updatedby == 'user2') {
        $seatColor = "CC66FF";
    } else {
        $seatColor = "red";
    }

    echo "\n<td bgcolor='$seatColor' align='center'>";
    echo "$rowId$columnId";
    if ($status == 0 || ($status == 1 && $updatedby == $_SERVER['PHP_AUTH_USER'])) {
        echo "<input type='checkbox' name='seats[]' value='$rowId$columnId'></checkbox>";
    }
    echo "</td>";
        if (($rowId == 'A' && $columnId == 7) 
            || ($rowId == 'B' && $columnId == 9) 
            || ($rowId == 'C' && $columnId == 9) 
            || ($rowId == 'D' && $columnId == 10) 
            || ($rowId == 'E' && $columnId == 8) 
            || ($rowId == 'F' && $columnId == 5) 
            || ($rowId == 'G' && $columnId == 13) 
            || ($rowId == 'H' && $columnId == 14) 
            || ($rowId == 'I' && $columnId == 14) 
            || ($rowId == 'J' && $columnId == 12) 
            || ($rowId == 'K' && $columnId == 14) 
            || ($rowId == 'L' && $columnId == 13) 
            || ($rowId == 'M' && $columnId == 9)) {
            // This fragment is for adding a blank cell which represent the "center aisle"
            echo "<td>&nbsp;</td>";
        }
}

echo "</tr></table></td>";
echo "</tr>";
echo "</table>";

/* Close connection to database server. */
mysql_close();
?>
</td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td width="100%" align="center">
    <table border="1" cellspacing="8" cellpadding="8">
        <tr>
            <td bgcolor='lightgreen'>Available</td>
            <td bgcolor='FFCC99'>Reserved user1</td>
            <td bgcolor='FF9999'>Confirmed user1</td>
            <td bgcolor='FFCCFF'>Reserved user2</td>
            <td bgcolor='CC66FF'>Confirmed user2</td>
        </tr>
    </table>
</td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td width="100%" align="center">
  <a href="seatplan.jpg" target="new">View Layout</a> 
</td></tr>
</table>
</form>
</body>
</html>

bookseats.php

<html>
<head>
    <title>Book Seats</title>
    <style>
        * {
            font-size: 14px;
            font-family: arial;
        }
    </style>
</head>
<body>
<?php include("login.php"); ?>
<center>
<br/>
<br/>
<br/>
<?php
    if (isset($_POST['seats']))
    {
        $user = $_SERVER['PHP_AUTH_USER'];

        $newStatusCode = $_POST['newStatusCode'];
        $oldStatusCode = $_POST['oldStatusCode'];

        // open database connection
        $linkID = @ mysql_connect("localhost", "tickets", "tickets") or die("Could not connect to MySQL server");
        @ mysql_select_db("tickets") or die("Could not select database");

        // prepare select statement
        $selectQuery = "SELECT rowId, columnId from seats where (";
        $count = 0;
        foreach($_POST['seats'] AS $seat) {
            if ($count > 0) {
                $selectQuery .= " || ";
            }
            $selectQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";
            $selectQuery .= " and columnId = " . substr($seat, 1) . " ) ";
            $count++;
        }
        $selectQuery .= " ) and status = $oldStatusCode";
        if ($oldStatusCode == 1) {
            $selectQuery .= " and updatedby = '$user'";
        }

        //echo $selectQuery;

        // execute select statement
        $result = mysql_query($selectQuery);
        //echo $result;

        $selectedSeats = mysql_num_rows($result);
        //echo "<br/>" . $selectedSeats;

        if ($selectedSeats != $count) {
            $problem = "<h3>There was a problem executing your request. No seat/s were updated.</h3>";
            $problem .= "Possible problems are:";
            $problem .= "<ul>";
            $problem .= "<li>Another process was able to book the same seat while you were still browsing.</li>";
            $problem .= "<li>You were trying to Confirm an unreserved Seat.</li>";
            $problem .= "<li>You were trying to Cancel an unreserved Seat.</li>";
            $problem .= "<li>You were trying to Reserve a reserved Seat.</li>";
            $problem .= "<li>There was a problem connecting to the database.</li>";
            $problem .= "</ul>";
            $problem .= "<a href='seats.php'>View Seat Plan</a>";
            die ($problem);
        }

        // prepare update statement
        $newStatusCode = $_POST['newStatusCode'];
        $oldStatusCode = $_POST['oldStatusCode'];

        $updateQuery = "UPDATE seats set status=$newStatusCode, updatedby='$user' where ( ";
        $count = 0;
        foreach($_POST['seats'] AS $seat) {
            if ($count > 0) {
                $updateQuery .= " || ";
            }
            $updateQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";
            $updateQuery .= " and columnId = " . substr($seat, 1) . " ) ";
            $count++;
        }
        $updateQuery .= " ) and status = $oldStatusCode";
        if ($oldStatusCode == 1) {
            $updateQuery .= " and updatedby = '$user'";
        }

        // perform update
        $result = mysql_query($updateQuery);
        $updatedSeats = mysql_affected_rows();

        if ($result && $updatedSeats == $count) {
            //$mysql->commit();
            echo "<h3>";
            echo "You have successfully updated $updatedSeats seat/s: ";
            echo "[";
            foreach($_POST['seats'] AS $seat) {
                $rowId = substr($seat, 0, 1);
                $columnId = substr($seat, 1);
                echo $rowId . $columnId . ", "; 
            }
            echo "]";
            echo "...</h3>";
        } else {
            //$mysql->rollback();
            echo "<h3>There was a problem executing your request. No seat/s were updated.</h3>";
            echo "Possible problems are:";
            echo "<ul>";
            echo "<li>Another process was able to book the same seat while you were still browsing.</li>";
            echo "<li>You were trying to Confirm an unreserved Seat.</li>";
            echo "<li>You were trying to Cancel an unreserved Seat.</li>";
            echo "<li>You were trying to Reserve a reserved Seat.</li>";
            echo "<li>There was a problem connecting to the database.</li>";
            echo "</ul>";
        }

        echo "<a href='seats.php'>View Seat Plan</a>";

        // Enable the autocommit feature
        //$mysqldb->autocommit(TRUE);

        // Recuperate the query resources
        //$result->free();

        mysql_close();
    }
?>
</center>
</body>
</html>

修改

我尝试了以下内容: 当用户提交复选框时,包括:

name.php

<?php
//$mysql->commit();
session_start();
$_SESSION['rowId'] = $_POST['rowId'];
$_SESSION['columnId'] = $_POST['columnId'];
            echo "<h3>";
            echo "Please enter the name for each seat:<br><p>&nbsp</p>";
            echo "";
            foreach($_POST['seats'] AS $seat) {
                $rowId = substr($seat, 0, 1);
                $columnId = substr($seat, 1);
                echo $rowId . $columnId . '</br><form method="post" name="input" action="pt2.php" >
<input name="name" type="text"/></br>';
            }
?>

<input type="submit" name="Submit" value="insert" />
</form>

pt2.php:

<?php
// Connect to MySQL
mysql_connect("localhost", "root", "root") or die("Connection Failed");
mysql_select_db("tickets")or die("Connection Failed");

$name = $_POST['name'];
session_start();
$rowId = $_SESSION['rowId'];
$columnId = $_SESSION['columnId'];

$result = mysql_query("UPDATE seats SET updatedby='".$name."' WHERE rowId='".$rowId."' AND columnId='".$columnId."'") 
or die(mysql_error());  

?>

如果用户只选择一个座位,则此代码就像魅力一样。但是当有两个或更多时,$ columnId和$ rowId不会改变,只有最后一个检查过座位的名称会改变。

1 个答案:

答案 0 :(得分:0)

我认为你不应该在这部分使用jquery。尝试使用带有html表单的php(在它有复选框的座位旁边显示输入)然后将数据保存到mysql seatid,seat_userId,seat_name等...然后只需提取数据并随意执行任何操作。