1

I have a table that has a list of clients on a waiting list where you can select to move one of them into a vacant room.

When you select this client, it moves onto a page called 'MoveIn1.html' where it gets that client's ID and uses it to display his/her name and ID. It also displays a drop-down box showing all the rooms that are vacant, giving the user a choice of which room to move the client into. The user then also chooses the date the client will move in and submits this information.

After it is submitted, it goes onto 'MoveIn2.php' where it should update the client's active to '1' (aka, yes), update the selected room's room_vacant to '0' (aka, no) and create/insert into a new occupancy record, along with the client's id (occupancy_client_id), the room id (occupancy_room_id) and the start date. I don't get a single error but my tables do not get updated and no occupancy record is created; when echo'ing my carried variables nothing is displayed either so I'm not sure how to sort this. Any help will be much appreciated.

Some of MoveIn1.html code:

$id = $_GET['id'];
//SELECTS from client where the selected client's ID is equal to client_id
$stmt = $dbh->prepare("SELECT * FROM client WHERE client_id=:id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$row = $stmt->fetch();

echo "<b>Client ID: </b>" . $row['client_id'];
echo "<br><br>";
echo "<b>Client Name: </b>" . $row['first_name'] . " " . $row['last_name'];
echo "<br><br>";


///////////////////////////////////////////////////////////////
//Creates a form for room_id
echo "<form action='MoveIn2.php'>";
//Creates drop down box to show the current rooms vacant

echo "<b>Choose a room: </b>";

$sql = "SELECT * FROM room";
$sql.= " WHERE room_vacant = 1";
$stmt = $dbh->query($sql);
echo "<select name='room_id'>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "<option value='" . $row['room_id'] . "'>" . $row['room_id'] . "</option>";
} //Closes drop down box
echo "</select><br><br>";

//Move-in date
echo "<b>Move-in date: </b>";
echo "<input type='date' name='start_date' value='" . date("Y/m/d") . "' required/><br><br>";


echo "<input type='hidden' name='id' value='" . $id .  "'/>";
echo "<input type='hidden' name='submit' value='submit' />";

//Submit button
echo "<input type='submit' value='Submit'><br><br>";
echo "<input type='button' name='cancel' value='Cancel' onclick='window.location='ViewWaitingList.html'' /><br><br>";

//Closes form
echo "</form>";
/////////////////////////////////////////////////////////////////

?>  

Some of MoveIn2.php code:

<?php
require("dbconnect.php");

//CLIENT
$id = $_POST['id'];
//UPDATES client's to 1/Yes to say that they're now an active client in JRH, 
//where the selected client's ID is equal to :id
$stmt = $dbh->prepare("UPDATE client SET active = 1 WHERE client_id=:id");
$stmt->bindParam(':id', $id);
$stmt->execute();

//ROOM
$room_id = $_POST['room_id'];
$stmt = $dbh->prepare("UPDATE room SET room_vacant = 0 WHERE room_id = :room_id");
$stmt->bindParam(':room_id', $room_id);
$stmt->execute();

//OCCUPANCY
$id = $_POST['id'];
$room_id = $_POST['room_id'];
$start_date = $_POST['start_date'];
$stmt = $dbh->prepare("INSERT INTO occupancy (occupancy_client_id, occupancy_room_id, start_date) VALUES(:id, :room_id, :start_date)");
$stmt->bindParam(':id', $id);
$stmt->bindParam(':room_id', $room_id);
$stmt->bindParam(':start_date', $start_date);
$stmt->execute();

echo $id;
echo $room_id;
echo $start_date;


?>
17
  • You shouldn't be surprised that you're not seeing errors: you're not looking for them. Check your calls to MySQL Commented Mar 1, 2015 at 14:54
  • I take it that you've instructed Apache to treat MoveIn1.html to run as PHP? If not, you'll need to rename it to MoveIn1.php, otherwise that code won't run. Commented Mar 1, 2015 at 15:04
  • @HoboSapiens I've looked, nothing comes up Commented Mar 1, 2015 at 15:06
  • @Fred-ii- yo fred, all the php stuff in MoveIn1.html is within php tags within html tags/file but I just took a little snippet of the code and put it here Commented Mar 1, 2015 at 15:09
  • 1
    @Fred-ii- yay it works!! Thanks again Fred :) (post it as an answer so I can tick it), thanks for kinda following my questions by the way, I'm sure I'll have some more on the way haha ;) Commented Mar 1, 2015 at 15:32

1 Answer 1

2

You're using echo "<form action='MoveIn2.php'>"; without explicitly stating that it should be a POST method and you're trying to pass POST arrays in the next page, forms default to GET, which in turn will fail silently.

Change:

echo "<form action='MoveIn2.php'>";

to

echo "<form action='MoveIn2.php' method='post'>";

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks again Fred! Can't believe how much of an idiot I was, too many times my code has failed due to silly mistakes such as this.
@Jack You're quite welcome Jack. I've been down that road also, so I'm passing on the help. We all have to start somewhere. Cheers ;-)
Exactly, I'm (hopefully) going to be studying computer science at university in September and I can't wait

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.