删除用户记录后更新宿舍记录

时间:2016-12-01 04:12:26

标签: php mysql database phpmyadmin

我创建了一个预订系统,允许用户注册然后创建用户记录。从那里,他们将登录并被带到他们可以管理他们的预订的个人资料页面。然后,如果他们没有任何预订,他们将被带到一个页面进行他们的第一次预订。一旦宿舍预订,宿舍将被更新,以减少一个房间的数量,并增加一个房间的预留数量。这部分有效。我遇到问题的地方是允许用户删除他们现有的预订记录的部分。这将从数据库中的预订表中删除记录,并更新宿舍表以增加可用于该宿舍的空间并减少预留的房间。该记录成功从phpmyadmin数据库中删除,只是宿舍表没有更新。

这是在更新时打印出来的内容,并在尝试删除记录时选择sql:

这是您当前的预订:

名字:凯蒂

姓氏:bartolotta

CWID:20063636

性别:女性

班级:大二学生

居住区:

特殊需要:是

洗衣店:是的

设备齐全的厨房:没有

点击下方删除您的记录。

删除

警告:mysqli_num_rows()要求参数1为mysqli_result,第75行/home/ubuntu/workspace/Project_Three/reservations.php中给出布尔值。调用堆栈:0.0004 234480 1. {main}()/ home / ubuntu / workspace / Project_Three / reservations.php:0 0.0059 254400 2. mysqli_num_rows()/ home/ubuntu/workspace/Project_Three/reservations.php:75 SELECT * FROM dorms WHERE name =''警告:非法字符串偏移' raID'在第88行的/home/ubuntu/workspace/Project_Three/reservations.php中调用堆栈:0.0004 234480 1. {main}()/home/ubuntu/workspace/Project_Three/reservations.php:0 UPDATE宿舍SET roomsAvailable = 1 WHERE id = RUPDATE宿舍SET roomsReserved = WHERE id =

这些是我正在使用的功能:

<?php

$servername = "localhost";
$username = "kbartolotta";
$password = "";
$dbname = "Project3";
$dormTable = "dorms";
$usersTable = "Users";
$reservationsTable = "Reservations";

$conn = mysqli_connect($servername, $username, $password);

if (!$conn) {
    die("Connection failed: ".mysqli_connect_error());
}
else {
    mysqli_select_db($conn, $dbname); 
}

$table = $dormTable;

function tableExists($table) {
    $sql = "SELECT * FROM $table";
    if (query($sql) !== FALSE) 
        return true;
    else 
        return false; 
}

function query($sql) {
    global $conn;
    return mysqli_query($conn, $sql);
    echo "<br>Yes<br>";
}

function createTable($table, $columns) {
    $sql = "CREATE TABLE $table (" . implode(", ", $columns) . ")";
    return query($sql);
}

function createRecord($table, $values) {
    //echo "This is working";
    return insertInto($table, ["name","class","specialNeeds","laundry","fullyEquippeKitchen","roomsAvailable","roomsReserved","roomCapacity"], $values);
}

function createReservationRecord($table, $values) {
    //echo "<br> in createReservationRecord(), table is \"$table\", values are ".print_r($values)."\n<br>";
    return insertInto($table, ["id","reservationTime","userID","raID","CWID","firstName","lastName","class","gender","fullyEquippedKitchen","laundry","specialNeeds"], $values);

}


 // $table = $usersTable;

function createUserRecord($table, $values) {
   // echo "<br> in createUserRecord(), table is \"$table\", values are ".print_r($values)."\n<br>";
    return insertInto($table, ["id", "firstName", "lastName", "CWID", "username", "password", "admin", "email", "gender", "class", "fullyEquippedKitchen", "laundry", "specialNeeds"], $values);

}

function insertInto($table, $columns, $values) {
    $sql = "INSERT INTO $table (`" . implode("`, `", $columns) . "`) VALUES ('" . implode("', '", $values) . "')\n";
    //echo"\n<br>".print_r($sql)."\n<br>";
    return query($sql);
}

?>

这是reservations.php页面:

<?php
session_start();
require 'sql_helper3.php';
$dorm = $_POST["dorm"];
echo "<form action=reservations.php method=post>\n";
$sql = "select * from $reservationsTable WHERE userID = $_SESSION[user_id]";
//echo $sql;
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) == 1) {

        $aRow = mysqli_fetch_assoc($result); 
        //print_r($aRow);
        echo "This is your current reservation:<br><br>";
        ?>
        <html>
    <body>

   </table>
   <!-- Confirmation Number: <?php //echo $reservationsTable["id"]; ?> <br>
    Date: <?php //echo $reservationsTable['reservationTime'];?><br> -->
    First Name: <?php echo $_SESSION['user_firstname'];?><br> 
    Last Name: <?php echo $_SESSION['user_lastname']; ?><br>
    CWID: <?php echo $_SESSION['user_CWID'];?><br>
    Gender: <?php echo $_SESSION['user_gender']; ?><br> 
    Class: <?php 
        if($_SESSION['user_class'] == 1){
            echo "Freshman";
        }
        elseif($_SESSION['user_class'] == 2){
            echo "Sophomore";
        }
        elseif($_SESSION['user_class'] == 3){
            echo "Upperclassman";
        }
        else{
            echo $_SESSION['user_class'];
        }
     ?><br>
    Residence Area: <?php echo $_POST["dorm"]; ?><br>
    Special Needs: <?php 
        if (isset($_SESSION['user_specialneeds'])){
            echo "Yes";
        }
        else{
            echo "No";
        } ?><br>

        Laundry: <?php if (isset($_SESSION['user_laundry'])){
            echo "Yes";
        }
        else{
            echo "No";
        } 
    ?><br>

    Fully Equipped Kitchen: <?php 
        if (isset($_SESSION['user_kitchen'])){
            echo "Yes";
        }
        else{
            echo "No";
        } 
    ?>
    <br>
    </body>
    </html>
    <?php
    echo"<br><br>Click below to delete your record.<br><br>";
    //echo "<input type=hidden name=deleted value=deleted>\n<br>";
    echo "<input type=\"submit\" value=\"Delete\">\n<br>";
   // if ($_POST[submitted] == "deleted"){
    $delete = "DELETE FROM $reservationsTable WHERE userID = $_SESSION[user_id]";

    $result2 = mysqli_query($conn,$delete);
    if (mysqli_num_rows($result2) == 1) {

        $aRow = mysqli_fetch_assoc($result);
    }
    //$dorm = $dormRecord['name'];
    $sql = "SELECT * FROM $dormTable WHERE name = '$dorm'";
    echo $sql;

    if ($result = mysqli_query($conn, $sql)) {
            $dormRecord = mysqli_fetch_assoc($result); 

    }
    //Update the record where the dorm id is used and set the roomsAvailable to -1 for that dorm
    $sql = "UPDATE $dormTable SET roomsAvailable = ".++$dormRecord[roomsAvailable] ." WHERE id = $reservationsTable[raID]";
    query($sql);
    echo $sql;

    //Update the record where the dorm id is used and set the roomsreserved to +1 for that dorm
    $sql = "UPDATE $dormTable SET roomsReserved = ".--$dormRecord[roomsReserved] ." WHERE id = $dormRecord[id]";
    query($sql);
    echo $sql;

    echo"</form>";



} 
elseif (mysqli_num_rows($result) > 1) { // this should not happen
    die("User has multiple reservations");
} 
else { 
    echo "You have no previous reservations.\n <br>";
    echo "<br><a href=verify3.php>Click here</a> to make a new reservation.\n<br>";
}

?>

这是设置SESSION变量的登录页面:

<?php
session_start(); // Starting Session
require 'sql_helper3.php';

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

if (empty($_POST['username']) || empty($_POST['password'])) {
echo "Username or Password is empty, please provide input for both fields.";
} 
else {
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];

// SQL query to fetch information of registered users and finds user match.
$sql = "select * from $usersTable where password= '$password' AND username='$username'";
//$sql = "select * from $usersTable where password=password('$password') AND username='$username'";
echo "Running SQL $sql\n<br>";
$result = mysqli_query($conn,$sql);
//    echo mysqli_num_rows($query). " is the number of rows";

if (mysqli_num_rows($result) == 1) {
  $_SESSION['login_user']=$username; // user is logged in now
//      echo "Initializing session...";
  $aUser = mysqli_fetch_assoc($result);

//print_r($aUser);die;
  $_SESSION['user_firstname'] = $aUser['firstName'];
  $_SESSION['user_lastname'] = $aUser['lastName'];
  $_SESSION['user_CWID'] = $aUser['CWID'];
  $_SESSION['user_email'] = $aUser['email'];
  $_SESSION['user_class'] = $aUser['class'];
  $_SESSION['user_gender'] = $aUser['gender'];
  $_SESSION['user_kitchen'] = $aUser['kitchen'];
  $_SESSION['user_laundry'] = $aUser['laundry'];
  $_SESSION['user_specialneeds'] = $aUser['specialNeeds'];
  $_SESSION['user_admin'] = $aUser['admin'];
  $_SESSION['user_id'] = $aUser['id'];
  if ($aUser['admin']) {
    header("location: admin_main.php"); // redirecting to admin landing page
  } 
  else {
    header('Location: profile.php'); // Redirecting To Students Landing page
  }  
} 
else { // no such login
  echo "Username or Password is invalid.<br><br>";
  echo "Please re-enter your username and password correctly.<br><br>";
  echo "<input name=\"Sign Up\" type=\"submit\" value=\" Sign Up \">";
   if(isset($_POST['submit']))
       {
        header('Location: profile.php');
       }
} 

mysqli_close($connection); // Closing Connection

  }

}
?>

这是在根据用户的偏好验证住宅区域后运行的结果页面:

<?php 
//Take user selection from verify
session_start();
require 'sql_helper3.php'; 

echo "<form action=reservations.php method=post>\n";
date_default_timezone_set('America/New_York');
$date = date('m/d/Y h:i:s a', time());
$dorm = $_POST["dorm"];
$username = $_POST["username"];
$sql = "SELECT * FROM $dormTable WHERE name = '$dorm'";
$sql2 = "SELECT * FROM $usersTable WHERE username = '$username' ";

if ($result = mysqli_query($conn, $sql)) {
            $dormRecord = mysqli_fetch_assoc($result); 
}

if ($result = mysqli_query($conn, $sql2)) {
    $userRecord = mysqli_fetch_assoc($result);
}
$reservationsTable = "Reservations";
$r1 = (rand(11111,99999));

 createReservationRecord($reservationsTable, [$r1, $date,       $_SESSION['user_id'], $dormRecord['id'], $_SESSION['user_CWID'], $_SESSION['user_firstname'], $_SESSION['user_lastname'], $_SESSION['user_class'], $_SESSION['user_gender'], $_SESSION['user_kitchen'], $_SESSION['user_laundry'], $_SESSION['user_specialneeds']]);

//Update the record where the dorm id is used and set the roomsAvailable to -1 for that dorm
$sql = "UPDATE $dormTable SET roomsAvailable = ".--$dormRecord[roomsAvailable]." WHERE id = $dormRecord[id]";
query($sql);

//Update the record where the dorm id is used and set the roomsreserved to +1 for that dorm
$sql = "UPDATE $dormTable SET roomsReserved = ".++$dormRecord[roomsReserved]." WHERE id = $dormRecord[id]";
query($sql);
?>

<html>
<body>
<h1>Reservation Confirmation </h1>

 </table>
Confirmation Number: <?php echo "$r1"; ?> <br>
Date: <?php echo "$date";?><br>
First Name: <?php echo $_SESSION['user_firstname'];?><br> 
Last Name: <?php echo $_SESSION['user_lastname']; ?><br>
CWID: <?php echo $_SESSION['user_CWID'];?><br>
Gender: <?php echo $_SESSION['user_gender']; ?><br> 
Class: <?php 
    if($_SESSION['user_class'] == 1){
        echo "Freshman";
    }
    elseif($_SESSION['user_class'] == 2){
        echo "Sophomore";
    }
    elseif($_SESSION['user_class'] == 3){
        echo "Upperclassman";
    }
    else{
        echo $_SESSION['user_class'];
    }
; ?><br>
Residence Area: <?php echo $_POST["dorm"]; ?><br>
Special Needs: <?php 
    if (isset($_SESSION['user_specialneeds'])){
        echo "Yes";
    }
    else{
        echo "No";
    } ?><br>

    Laundry: <?php if (isset($_SESSION['user_laundry'])){
        echo "Yes";
    }
    else{
        echo "No";
    } 
?><br>

Fully Equipped Kitchen: <?php 
    if (isset($_SESSION['user_kitchen'])){
        echo "Yes";
    }
    else{
        echo "No";
    } 
?>
<br>
<?php
 echo "<br><br><a href=reservations.php>Click here</a> to manage your    reservations.\n<br><br>";
 echo "</form>";
 foreach ($_POST as $k => $v){
                    echo"<input type = hidden name = $k value = \"$v\"> <?php echo print_r($_POST) ?>";
            }
?>
</body>
</html>

让我知道需要哪些其他规格或代码。谢谢!

0 个答案:

没有答案