I'm very new to php and sql and I'm trying to set a _SESSION variable for my user_email, I know that the column name is correct and I'm pulling the other information from the session like the user_id etc correctly but cannot seem to get the email to set even though I've done it the same as my other variables. I'm also trying to use the mail() function but I'm not sure if this is set up correctly? Any help would be really appreciated!
I've tried doing the variable as a _POST one using a hidden input but that hasn't worked either.
<div>
<?php
if (isset($_POST['request_date'])) {
$user_email = $_SESSION['user_email'];
$user_id = $_SESSION['user_id'];
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
// $request_time = date("d-m-Y H:i:s");
$lend_status = 1;
$requested_start_date = date('Y-m-d H:i:s', strtotime($_POST['requested_start_date'] . ' ' . $_POST['requested_start_time'] . ':00:00'));
$requested_end_date = date('Y-m-d H:i:s', strtotime($_POST['requested_end_date'] . ' ' . $_POST['requested_end_time'] . ':00:00'));
$comments = $_POST['comments'];
// Declare available laptops array
$available_laptops = array();
// GET ALL LAPTOPS IN OPERATION
$STH = $DBH->prepare("
SELECT laptop_id,
laptop_name
FROM laptops
WHERE 1
");
$STH->execute(array());
while ($row = $STH->fetch()) {
// CHECK EACH LAPTOP FOR THE REQUESTED DATES
$STH2 = $DBH->prepare("
SELECT lend_id
FROM laptop_system
WHERE (
(
approved_start_date <= ?
AND approved_end_date >= ?
) OR (
approved_start_date <= ?
AND approved_end_date >= ?
) OR (
approved_start_date >= ?
AND approved_end_date <= ?
)
)
AND laptop_id = ?
");
$STH2->execute(array(
$requested_start_date,
$requested_start_date,
$requested_end_date,
$requested_end_date,
$requested_start_date,
$requested_end_date,
$row->laptop_id
));
// IF IT'S NOT BOOKED OUT, ADD TO ARRAY
if ($STH2->rowCount() < 1) {
$available_laptops[$row->laptop_id] = $row->laptop_name;
}
}
if (empty($available_laptops)) {
echo '<h3>Sorry, this date is not available.</h3>';
} else {
$STH = $DBH->prepare("
INSERT INTO laptop_system (
user_id,
first_name,
last_name,
lend_status,
requested_start_date,
requested_end_date,
comments
)
VALUES(?, ?, ?, ?, ?, ?, ?)
");
$STH->execute(array(
$user_id,
$first_name,
$last_name,
$lend_status,
$requested_start_date,
$requested_end_date,
$comments
));
echo '<h2 style="color:#D80B8C; margin-bottom:1em;">' . $first_name . ', your laptop request is now pending approval.</h2>';
$to = $user_email;
$subject = "Laptop Request";
$message = "Thank you for your laptop request for " . $requested_start_date . " - " . $requested_end_date . "It is now pending and you will be notified if it's been approved or declined.";
$message = wordwrap($message,70);
$headers = "From: Timmy and Edwardo";
mail($to,$subject,$txt,$headers);
}
} ?>
<form action="" method="post" >
<div>
<label for="requested_start_date"> Requested Start Date </label>
<input type="date" name="requested_start_date" value="<?php echo $requested_start_date; ?>">
<label for="requested_start_time">Requested start time </label>
<select name="requested_start_time" style="width:auto;margin:1em 1em 1em 0;">
<?php for ($i = 0; $i < 25; $i++) {$i = str_pad($i, 2, "0", STR_PAD_LEFT); ?>
<option value="<?php echo $i; ?>"><?php echo $i . ':00'; ?></option>
<?php } ?>
</select>
</div>
<div>
<label for="requested_end_date">Requested End Date </label>
<input type="date" name="requested_end_date" value="<?php echo $requested_end_date; ?>">
<label for="requested_end_time">Requested end time</label>
<select name="requested_end_time" style="width:auto;margin:1em 1em 1em 0;">
<?php for ($i = 0; $i < 25; $i++) {$i = str_pad($i, 2, "0", STR_PAD_LEFT); ?>
<option value="<?php echo $i; ?>"><?php echo $i . ':00'; ?></option>
<?php } ?>
</select>
</div>
<div>
<p style="margin-bottom:0;">
Please can you let us know below why you need the laptop and if there are any special requirements needed -
</p>
<input type="textarea" rows="4" cols="50" name="comments" placeholder="" required>
<input type="submit" name="request_date" value="Request Date">
<!-- <input type="hidden" name="user_email" value="<?php echo $user_email;?>" > -->
</div>
</form>
<?php
?>
</div>