I am trying to post a $_GET value so it cant insert the variable value into the database using $_SERVER["REQUEST_METHOD"].
<?php require_once("../includes/initialize.php"); ?>
<?php include("../includes/form_validation_card.php"); ?>
<?php $username = $_SESSION['username']; ?>
<?php
if(!isset($_GET['total']) && !isset($_GET['order_id'])){
redirect_to('order_summary.php');
}
$total = $_GET['total'];
$order = $_GET['order_id'];
?>
<?php
$username = $_SESSION['username'];
$sql = "SELECT * FROM customers WHERE username='$username'";
$result_set = $database->query($sql);
$found_user = $database->fetch_array($result_set);
?>
<?php include_layout_template('header2.php'); ?>
<div class="container">
<div class="row">
<br/><br/><?php echo output_message($message); ?>
</div>
<div class="row ">
<div class="jumbo jumbotron-fluid mx-auto d-block" style="height: 500px; width: 440px; background-color:#DCDCDC; border-radius: 5px;" >
<div class="text-center" style="margin: 3px;"><img src="logo/eden_petshop_logo.png" width="32" height="32"/><?php echo $found_user['first_name']; ?> <?php echo $found_user['last_name']; ?></div>
<p class="text-center">order id: #<?php echo $order; ?></p>
<h4 class="display-4 lead text-center">N<?php echo $total; ?></h4>
<div class="col-sm-6 col-sm-offset-3 mx-auto d-block">
<?php echo output_message($message); ?>
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="post" class="form-horizontal">
<div class="form-group">
<div class="col">
<input type="text" name="card_number" onchange="trim(this)" placeholder="Card Number" class="form-control" id="card_number"/>
<span style="color: #EA4335"><?= $card_number_error; ?></span>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col">
<input type="text" name="mm_yy" class="form-control" onchange="trim(this)" placeholder="MM/YY">
<span style="color: #EA4335"><?= $mm_yy_error; ?></span>
</div>
<div class="col">
<input type="password" name="cvv" class="form-control" onchange="trim(this)" placeholder="CVV">
<span style="color: #EA4335"><?= $cvv_error; ?></span>
</div>
</div>
</div>
<div class="col-sm-12 col-sm-push-3">
<button type="submit" name="submit" value="Pay" class="btn bg-info btn-sm btn-block" onClick="return confirm('Are you sure your details are correct?');">Pay</button>
</div>
</form>
</div>
</div>
</div>
<?php include_layout_template('footer2.php'); ?>
This is the input display page but i am trying to the $total and $order $_GET variables insert into the database... Please note that every other part of the code assignment is working fine.
<?php
//define variables and set them to empty values
$total_error = $order_error = $card_number_error = $mm_yy_error = $cvv_error = "";
$timestamp = strftime("%Y-%m-%d %H:%M:%S", time());
//form is submitted with post method
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty($_POST["card_number"])){
$card_number_error = "<div class=''>Card number is required</div>";
}else{
$card_number = test_input($_POST["card_number"]);
//Check if name only contains letters and whitespaces
if(!preg_match("/^(?=.*?[0-9]).{16,}$/",$card_number)){
$card_number_error = "<div>Only 16 numbers allowed</div>";
}
}
if(empty($_POST["mm_yy"])){
$mm_yy_error = "<div class=''>Card expiry is required</div>";
}else{
$mm_yy = test_input($_POST["mm_yy"]);
//Check if name only contains letters and whitespaces
if(!preg_match("/^(?=.*?[0-9]).{3,}$/",$mm_yy)){
$mm_yy_error = "<div class=''>Only numbers allowed</div>";
}
}
if(empty($_POST["cvv"])){
$cvv_error = "<div class=''>Card verification is required</div>";
}else {
$cvv = test_input($_POST["cvv"]);
//check if username is atleast 7 characters
if(!preg_match("/^(?=.*?[0-9]).{3,}$/",$cvv)){
$cvv_error = "<div class=''>Card verification must not be more than 3 numbers</div>";
}
}
if($card_number_error == "" && $mm_yy_error == "" && $cvv_error == ""){
$token = 'vfjhvbkebecbjDRCWVJEcbkrvlnke24tir7c_zdvbejw968350124';
$token = str_shuffle($token);
$token = substr($token, 0, 15);
$username = $_SESSION['username'];
$sql = "SELECT * FROM customers WHERE username='$username'";
$result_set = $database->query($sql);
$found_user = $database->fetch_array($result_set);
$email = $found_user['email_address'];
$pay = new Payment();
$pay->username = $username;
$pay->order_id = $order;
$pay->total = $total;
$pay->card_number = $card_number;
$pay->expiry = $mm_yy;
$pay->cvv = $cvv;
$pay->transaction_id = $token;
$pay->status = 0;
$pay->created_at = $timestamp;
if($pay->save()){
//$mail = new Mail();
//$mail->email_address = $email_address;
//$mail->send_transaction_confirmation();
unset($_SESSION['shopping_cart']);
$session->message('<div class="btn bg-success">Congratulations!!! Your order has been processed.</div>');
redirect_to('photos.php');
}
}
if(empty($_POST["message"])){
$message = "";
} else{
$message = test_input($_POST["message"]);
}
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = htmlentities($data);
return $data;
}
?>
Please note that my $order and total are not getting inserted into my database... Any assistance on my preg_match would be appreciated... Thanks in advance.
You need to use $_POST in your broken code instead of $_GET. Notice your working code used $_POST instead, which is why it works.
Related
Hi I'm learning php and mysql and creating simple user system. Now I wan't to display user data from mysql to the page using PHP. But I'm struggling with that because I can't get data from databasa even though user correctly sign in. I don't know what is wrong with the code, can't find problem and I was trying difference ways to grab data and display but nothing is working and always variable with data is empty.
login-process.php
<?php
include('connection.php');
$error = array();
$email = $_POST['email'];
if(empty($email)){
$error[] = 'Email can not be empty!';
}
$password = $_POST['password'];
if(empty($password)){
$error[] = 'Password can not be empty!';
}
if(empty($error)){
// query
$query = "SELECT userID, firstName, lastName, email, password, profileImage FROM user WHERE email=?";
$q = mysqli_stmt_init($conn);
mysqli_stmt_prepare($q, $query);
mysqli_stmt_bind_param($q, 's', $email);
mysqli_stmt_execute($q);
// store result
$result = mysqli_stmt_get_result($q);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if(!empty($row)){
// checking password
// unhasing password
if(password_verify($password, $row['password'])){
header('location: user-profile.php');
exit();
} else {
echo "You are not a member";
}
}
}
// get user info
function get_user_info($con, $userID){
$query = "SELECT firstName, lastName, email, profileImage FROM user WHERE userID=?";
$q = mysqli_stmt_init($con);
mysqli_stmt_prepare($q, $query);
// bind the statement
mysqli_stmt_bind_param($q, 'i', $userID);
// execute sql statement
mysqli_stmt_execute($q);
$result = mysqli_stmt_get_result($q);
$row = mysqli_fetch_array($result);
return empty($row) ? false : $row;
}
user-profile.php
<?php
session_start();
include ('header.php');
$user = array();
if(isset($_SESSION['userID'])){
require ('mysqli_connect.php');
$user = get_user_info($con, $_SESSION['userID']);
}
?>
<section id="main-site">
<div class="container py-5">
<div class="row">
<div class="col-4 offset-4 shadow py-4">
<div class="upload-profile-image d-flex justify-content-center pb-5">
<div class="text-center">
<img class="img rounded-circle" style="width: 200px; height: 200px;" src="<?php echo isset($user['profileImage']) ? $user['profileImage'] : './assets/profile/beard.png'; ?>" alt="">
<h4 class="py-3">
<?php
if(isset($user['firstName'])){
printf('%s %s', $user['firstName'], $user['lastName'] );
}
?>
</h4>
</div>
</div>
<div class="user-info px-3">
<ul class="font-ubuntu navbar-nav">
<li class="nav-link"><b>First Name: </b><span><?php echo isset($user['firstName']) ? $user['firstName'] : ''; ?></span></li>
<li class="nav-link"><b>Last Name: </b><span><?php echo isset($user['lastName']) ? $user['lastName'] : ''; ?></span></li>
<li class="nav-link"><b>Email: </b><span><?php echo isset($user['email']) ? $user['email'] : ''; ?></span></li>
</ul>
</div>
</div>
</div>
</div>
</section>
<?php
include "footer.php";
?>
login.php
<?php
session_start();
include ('header.php');
include('login-process.php');
?>
<?php
$user = array();
require ('connection.php');
if(isset($_SESSION['userID'])){
$user = get_user_info($conn, $_SESSION['userID']);
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
require ('login-process.php');
}
?>
<section id="register">
<nav class="navbar nav">
<img src="./image/logo.png" width="150px" height="150px" alt="">
</nav>
<div class="row ml-5 m-0">
<div class="col-lg-4 offset-lg-1">
<div class="text-center pb-5">
<h1 class="login-title text-white">Sign in.</h1>
<p class="p-1 m-0 font-poppins text-white-50">Welcome back! Please enter your details.</p>
</div>
<div class="upload-profile-image d-flex justify-content-center pb-5">
<div class="text-center">
<div class="d-flex justify-content-center">
<img class="camera-icon" src="./image/camera.png" alt="camera">
</div>
<img src=<?php echo isset($user['profileImage']) ? $user['profileImage']: "./assets/profile-picture/avatar.jpg"; ?> style="width: 200px; height: 200px" class="img rounded-circle" alt="profile">
<small class="form-text text-white-50">Choose Image</small>
<input type="file" form="reg-form" class="form-control-file" name="profileUpload" id="upload-profile">
</div>
</div>
<div class="d-flex justify-content-center">
<form action="login.php" method="post" enctype="multipart/form-data" id="reg-form">
<div class="form-row my-4">
<div class="col">
<input type="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" required name="email" id="email" class="form-control" placeholder="Email*">
</div>
</div>
<div class="form-row my-4">
<div class="col">
<input type="password" required name="password" id="password" class="form-control" placeholder="Password*">
</div>
</div>
<small id="login-error" class="text-danger">hi</small>
<div class="form-inline">
<input type="checkbox" name="agreement" class="form-check-input">
<label for="agreement" class="form-check-label pr-3 font-poppins text-white-50">Remember for 30 days</label>
Forgot password
</div>
<div class="submit-btn text-center my-5">
<button type="submit" onclick="confirmPassword(event)" class="btn btn-dark rounded-pill text-white px-5 py-3">Create account</button>
</div>
</form>
</div>
</div>
</div>
</section>
<?php
include ('footer.php');
?>
register-process.php
<?php
include ('connection.php');
//data from the form
$firstName = $_POST['first-name'];
$lastName = $_POST['last-name'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$files = $_FILES['profileUpload'];
$profileImage = upload_profile("./assets/profile-picture", $files);
//sending data from input to the database
$sql = "INSERT INTO user VALUES (DEFAULT , '$firstName', '$lastName', '$email', '$password', '$profileImage', NOW())";
if (mysqli_query($conn, $sql)) {
session_start();
$_SESSION['userID'] = mysqli_insert_id($con);
header('location:login.php');
exit();
} else {
echo "ERROR";
echo mysqli_error($conn);
}
//image
function upload_profile($path, $file){
$targetDir = $path;
$default = "avatar.jpg";
$filename = basename($file['name']);
$targetFilePath = $targetDir.$filename;
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(!empty($filename)){
// file format
$allowType = array('jpg', 'png', 'gif', 'jpeg');
if(in_array($fileType, $allowType)){
// send file to the folder
if(move_uploaded_file($file['tmp_name'], $targetFilePath)){
return $targetFilePath;
};
}
}
//if user does not load picture return the default one
return $path .$default;
}
I would like to ask for code example how to display data from database into website when I have ID
You're not setting $_SESSION['userId'] when a user logs in successfully, so the part
if(isset($_SESSION['userID'])){
$user = get_user_info($conn, $_SESSION['userID']);
}
will not trigger and $user will not be set.
Change
if(password_verify($password, $row['password'])){
header('location: user-profile.php');
exit();
} else {
echo "You are not a member";
}
to
if(password_verify($password, $row['password'])){
$_SESSION['userId'] = $row['userId']
header('location: user-profile.php');
exit();
} else {
echo "You are not a member";
}
You haven't set the value for $_SESSION['userID']
use this
session_start();
$user_id = $_SESSION['userID'];
hope it will solve your problem
I have PHP code retrieving data from MySQL DB. What I want is to press a button then a popup must display data for that relevant id or display data on that id row. So for the first btn it works well but on other btn's it displays data from btn 1.
Is there any solution out there?
Thanks
$query = "SELECT * FROM users"
$results = mysqli_query ($conn, $query);
$chck_res = mysqli_num_rows($results);
if ($chck_res > 0) {
while($row = mysqli_fetch_array($results)) {
$id = $row['id'];
$name =$row['name'];
$lastName = $row['lName'];
?>
<div class="container">
<button onClick="popWin()">View data at <?php echo $id; ></button>
</div>
<div id="popup">
echo $id = $row['id'];
echo $name =$row['name'];
echo $lastName = $row['lName'];
</div>
<?php
}
}
CSS:
#popup {
display: none;
}
JS:
function popWin() {
document.getElementById('popup').style.display = "Block";
}
DOM elements must have unique ID, you can't get access to the second element with the same ID via getElementById.
Try to replace your divs with next:
<div>
<div class="container">
<button onClick="popWin(this)">View data at <?php echo $id; ></button>
</div>
<div class="popup">
<?= $id = $row['id']; ?>
<?= $name = $row['name']; ?>
<?= $lastName = $row['lName']; ?>
</div>
</div>
JS:
function popWin(btn) {
btn.parentNode.parentNode.getElementsByClassName("popup")[0].style.display = "Block";
console.log(btn.parentNode.parentNode.getElementsByClassName("popup")[0].innerHTML);
}
function popWin(btn) {
btn.parentNode.parentNode.getElementsByClassName("popup")[0].style.display = "Block";
console.log(btn.parentNode.parentNode.getElementsByClassName("popup")[0].innerHTML);
}
<div>
<div class="container">
<button onClick="popWin(this)">View data at 2</button>
</div>
<div class="popup">
name id 2
</div>
</div>
<div>
<div class="container">
<button onClick="popWin(this)">View data at 1</button>
</div>
<div class="popup">
name id 1
</div>
</div>
Your code has errors. you
did not terminate the select query
did not end php tag on echo id on line 14
<?php
$query = "SELECT * FROM users";
$results = mysqli_query ($conn, $query);
$chck_res = mysqli_num_rows($results);
if ($chck_res > 0) {
while($row = mysqli_fetch_array($results)) {
$id = $row['id'];
$name =$row['name'];
$lastName = $row['lName'];
?>
<div class="container">
<button onClick="popWin()">View data at <?php echo $id; ?></button>
</div>
<div id="popup">
<?php echo $id = $row['id'];
echo $name =$row['name'];
echo $lastName = $row['lName'];?>
</div>
<?php }}
I am creating a seat reservation system. In my system, the code check number of seats a bus contains then pass it inside a for loop. When a user pick 2 passengers, it means two seats will be booked. How can I validate the checkbox in the for loop depending on the number of passenger(s) selected.
Using the GUI for more explanation.
on the main page, 2 there indicates number of passenger(s) selected.
When you come to the second page where the values are passed to, you can see 2 Adults as the selected number of passengers. When you click on Submit Button it does not validate the checkbox based on the number of passenger(s) selected. And if I should put required in the checkbox it validates the whole checkbox since it is in a loop
$_SESSION['seat_no'] is the number of seat(s) a bus contains. Let assume a user that want to book a seat selected two passengers which means two seats is booked, how can I validate the checkbox based on the number of seat(s) selected?
Here is my code:
<?php
for ($i = 1; $i <= $_SESSION['seat_no']; $i++) {
if(in_array($i,$mseat)){
echo "<div class='checkbox_wrapper_pick'>
<label>".$i."</label>
</div>";
}else{
echo "<div class='checkbox_wrapper'>
<input type='checkbox' value=".$i." name='seat_book[]' />
<label>".$i."</label>
</div>";
}
}
?>
The full source code:
<?php include("header.php"); error_reporting(0); ?>
<?php
if(isset($_POST['submit'])){
$from = $_POST['from'];
$to = $_POST['to'];
$date = $_POST['d_date'];
$nop = $_POST['nop'];
$_SESSION['from'] = $from;
$_SESSION['to'] = $to;
$_SESSION['date'] = $date;
$_SESSION['nop'] = $nop;
$get = mysqli_query($mysqli,"SELECT * FROM routes WHERE present_loc = '$from' and destination = '$to' ");
while($roys = mysqli_fetch_array($get)){
//get bus details
$bno = $roys['bus_no'];
$ploc = $roys['present_loc'];
$des = $roys['destination'];
$time = $roys['dept_time'];
$_SESSION['time'] = $time;
$amt = $roys['amount'];
$_SESSION['amt'] = $amt;
$b = str_replace( ',', '',$_SESSION['amt'] );
if( is_numeric($b) ) {
$a = $b;
}
$bus = mysqli_query($mysqli,"select * from bus where bus_no = '$bno'");
while($bu = mysqli_fetch_array($bus)){
$_SESSION['model'] = $bu['model'];
$_SESSION['seat_no'] = $bu['seat_no'];
$_SESSION['ac'] = $bu['bus_type'];
$_SESSION['excess_luggage'] = $bu['excess_luggage'];
$_SESSION['more_legs'] = $bu['more_legs'];
$_SESSION['id'] = $bu['id'];
}
$coun = mysqli_query($mysqli, "select count(booking_id) as seat, seats from booking where bus_no = '$bno' and seats !='' GROUP by booking_id" );
$mseat = array();
while($e = mysqli_fetch_array($coun)){
$bseat = $e['seat'];
$mseat[] = $e['seats'];
}
//$seatss = array();
$seat_string = implode(",",$mseat);
//get seats
$couns = mysqli_query($mysqli, "select sum(counter) as seat from booking where bus_no = '$bno' and seats !='' GROUP by bus_no" );
$rseats = mysqli_fetch_array($couns);
$lseat = $rseats['seat'];
if($_SESSION['seat_no'] == $lseat){
$tell = " No more seat(s) available.";
}else{
$tell = $_SESSION['seat_no'] - $lseat. " Seat(s) remaining.";
}
}
}
?>
<!--Main layout-->
<main class="mt-5">
<!--Main container-->
<form action="details" method="POST">
<!--Grid row-->
<div class="row">
<div class="col-lg-12 title-header mb-3 mx-auto z-depth-1">
<div class="row">
<div class="col-lg-8">
<?php echo '<h2> '.$_SESSION['from']. ' to '. $_SESSION['to']. '</h2>'; ?><br/>
<b><?php echo $_SESSION['date']; ?> :: <?php if($_SESSION['nop'] < '2') { echo $_SESSION['nop'] . ' Adult'; }
elseif($_SESSION['nop'] > 1) { echo $_SESSION['nop'] . ' Adults'; }
?></b>
</div>
</div>
</div>
<div class="col-lg-12 mbody"> <label style="margin-left: 4%; font-weight:bolder; font-size:20px; color:#000;">Details </label> </div>
<div class="col-lg-12 mbody bg-white ">
<table class="table table_view" style = "width: 100%; margin-left: 4%; margin-right:4%;">
<tbody>
<tr>
<td><b><?php echo $_SESSION['model']; ?></b><br/><?php echo $_SESSION['from']. ' to '. $_SESSION['to']; ?>
<br/><?php if($_SESSION['ac'] == 'AC') { echo '<span class="alert-info ac">'. $_SESSION['ac'] .'</span>'; }
else{ echo '<span class="alert-warning">No AC</pan>'; } ?>
<?php if($_SESSION['more_legs'] == 'Yes') { echo '<span class="alert-info ac">More Leg Room</span>'; }
else{ echo '<span class="alert-warning no">More Leg Not Available</pan>'; } ?>
</td>
<td><b>Departing Time</b><br/><i class="fa fa-clock-o" aria-hidden="true"></i> <?php echo $_SESSION['time']; ?></td>
<td> <img id = "seatimg" src="../images/seatsLayout/av.gif" class="img-responsive"> <?php echo $tell; ?></td>
<td>Adult <b>₦<?php echo $_SESSION['amt']; ?></b></td>
</tr>
</tbody>
</table>
</div>
<div class="col-lg-12">
<div class="col-lg-12 mbody"> <label style="margin-left: 3%; font-weight:bolder; font-size:20px; color:#000;"><img id = "seatimg" src="../images/seatsLayout/av.gif" class="img-responsive"> Select Seat</label> </div>
<div class="row detail">
<!--Grid column-->
<div class="col-lg-7 animation slideUp" >
<div class="well" id="bus_seats_layout" >
<table class="table table-bordered" cellspacing = "1" id="seatstable">
<tr>
<td><img id = "driverimg" src="../images/seatsLayout/steering.png" class="img-responsive" width="25" height="25"></td>
<td colspan="2" rowspan="3">
<?php
for ($i = 1; $i <= $_SESSION['seat_no']; $i++) {
if(in_array($i,$mseat)){
echo "
<div class='checkbox_wrapper_pick'>
<label>".$i."</label>
</div>
";
}else{
echo "
<div class='checkbox_wrapper'>
<input type='checkbox' value=".$i." name='seat_book[]' />
<label>".$i."</label>
</div>
";
}
}
?>
</td>
</tr>
</table>
</div>
</div>
<div class="col-lg-5">
<ul class="bt">
<li><img src="../images/seatsLayout/seat_available.png" class="img-responsive"> Available</li>
<li><img src="../images/seatsLayout/picked.png" class="img-responsive"> Selected</li>
<li><img src="../images/seatsLayout/seat_booked.png" class="img-responsive"> Booked</li>
</ul>
</div>
</div>
<div class="col-lg-12">
<input type="hidden" name="bus_no" value="<?php echo $bno; ?>">
<input type="hidden" name="to" value="<?php echo $to; ?>">
<input type="hidden" name="from" value="<?php echo $from; ?>">
<input type="hidden" name="amt" value="<?php echo $nop*$a; ?>">
<input type="hidden" name="nop" value="<?php echo $nop; ?>">
<div class="form-group">
<div align="right">
<input type="submit" name="submit" class="bme" value="Continue">
</div>
</div>
</div>
</div>
</div>
</form>
</main>
<?php include("footer.php"); ?>
i have three files
leaveform.php
leavefrom.tpl
leaveform.class.php
i will send the codings below. olease find and solve this issue.
leaveform.php
<?php
include_once 'init.php';
include CLASS_DIR."leaveform.class.php";
$leaveformObj = new leaveform;
$email = '';
$password = '';
$err_msg = '';
$s_msg = '';
$verfication_code = '';
if(isset($_POST['leaveSubmit'])){
$branch = $_POST['branch'];
$date = $_POST['date'];
$name = $_POST['name'];
$designation = $_POST['designation'];
$employeeid = $_POST['employeeid'];
$noofdays = $_POST['noofdays'];
$from = $_POST['from'];
$to = $_POST['to'];
$reasonforleave = $_POST['reasonforleave'];
$address = $_POST['address'];
$contactno = $_POST['contactno'];
$approvedby = $_POST['approvedby'];
if(empty($branch)){
$err_msg = "Please select your branch";
}elseif(empty($date)){
$err_msg = "Please select The Date";}
else {
$leaveformObj->branch = $branch;
$leaveformObj->date = $date;
$leaveformObj->name = $name;
$leaveformObj->designation = $designation;
$leaveformObj->employeeid = $employeeid;
$leaveformObj->noofdays = $noofdays;
$leaveformObj->from = $from;
$leaveformObj->to = $to;
$leaveformObj->reasonforleave = $reasonforleave;
$leaveformObj->address = $address;
$leaveformObj->contactno = $contactno;
$leaveformObj->approvedby = $approvedby;
$addleave = $leaveformObj->addleave(); //Login as Church Admin
if($addleave){
$leaveformObj->ticket_no = "Ticket#".$addleave;
$leaveformObj->leave_id = $addleave;
$Ticket = $leaveformObj->updateTicket();
if ($Ticket) {
$s_msg = "Successfully Submitted<br>";
$s_msg .= "Your Ticket No : Ticket#".$addleave;
}
}else{
$err_msg = "Invalid leaveform details";
}
}
}
$smarty->assign('s_msg', $s_msg);
$smarty->assign('err_msg', $err_msg);
$smarty->display('leaveform.tpl');
?>
leaveform.tpl
{include file="inc/main_header.tpl"}
<style type="text/css">
option {
text-transform: capitalize;
}
.nopadding div{
padding: 0px !important;
}
form[role="login"] input.inputText{
padding: 0;
border: 0;
background: none;
border-bottom: 1px dotted;
box-shadow: none;
border-radius: initial;
width: 85%;
}
form[role="login"] .threeCol input.inputText{
width: 68%;
}
</style>
<div class="wrapper">
<!-- Left side column. contains the logo and sidebar -->
<div class="container">
<div class="col-md-12">
<!--breadcrumbs start -->
<!--<ul class="breadcrumb front">
<li><i class="fa fa-home"></i> Home</li>
<li class="active">Admin Login</li>
</ul>-->
<!--breadcrumbs end -->
</div>
<section class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<form method="post" action="" role="login" id="FormValidate">
<div class="col-md-3">
<img src="images/krishnasweet.png" alt="Krishna Sweet" class="" width='180'>
</div>
<div class="col-md-6">
<h3 style="text-align: center;color: blue;font-style: italic;">Leave Application Form<br></h3>
</div>
{if $s_msg}
<p style="text-align: center; color:green;">{$s_msg}</p>
{elseif $err_msg}
<p style="text-align: center; color:red;">{$err_msg}</p>
{/if}
<div class="col-md-12" style="min-height:30px;"></div>
<div class="col-md-3"> </div>
<div class="col-md-15" >
<div class="col-md-12 nopadding">
<div class="col-md-6"><label style="display: inline-block;">Branch :</label><input type="text" class="inputText" name="branch"></div>
<div class="col-md-6"><label style="display: inline-block;">Date :</label><input type="date" class="inputText" name="date"></div>
</div>
<div class="col-md--1"> </div>
</div>
<div class="col-md--1"> </div>
<div class="col-md--1" >
<div class="col-md--1">
</div>
</div>
<div class="col-md--1"> </div>
<div class="col-md-2" >
</div>
<div class="col-md-12 nopadding">
<div class="col-md-8"><label style="display: inline-block;">Name :</label><input type="text" class="inputText" name="name"></div>
<div class="col-md-8"><label style="display: inline-block;">Designation:</label><input type="text" class="inputText" name="designation"></div>
</div><div class="col-md-12 nopadding">
<div class="col-md-7"><label style="display: inline-block;">Employee ID :</label><input type="text" class="inputText" name="employeeid"></div>
</div>
<div class="col-md-12 threeCol nopadding">
<div class="col-md-4"><label style="display: inline-block;">No.Of.Days:</label><input type="text" class="inputText" name="noofdays"></div>
</div><div class="col-md-12 threeCol nopadding">
<div class="col-md-4"><label style="display: inline-block;">From:</label><input type="date" class="inputText" name="from" ></div>
<div class="col-md-4"><label style="display: inline-block;">To:</label><input type="date" class="inputText" name="to" ></div>
</div>
<div class="col-md-12 nopadding">
<div class="col-md-12"><label style="display: inline-block;">Reason For Leave:</label><input type="text" class="inputText" name="reasonforleave" "></div>
<div class="col-md-12"><label style="display: inline-block;">Address:</label><input type="text" class="inputText" name="address" "></div>
</div>
<div class="col-md-9"><label style="display: inline-block;">Contact No:</label><input type="text" class="inputText" name="contactno"></div>
<div class="col-md-9"><label style="display: inline-block;">Approved By:</label><input type="text" class="inputText" name="approvedby"></div>
</div>
<button type="submit" name="leaveSubmit" class="btn btn-block btn-info">Submit</button>
</form>
</div>
</div>
</section>
</div>
</div>
{include file="inc/main_footer.tpl"}
<script>
$(document).ready(function(){
$(document).on("change","#categorySection",function(event) {
var _val = $(this).val();
if(_val != ""){
$("#subCategorySection option").hide();
$("#subCategorySection option[data-cat="+_val+"]").show();
$("#subCategorySection").removeAttr("disabled");
}
});
});
</script>
leaveform.class.php
<?php
class leaveform
{
var $leave_id;
var $branch;
var $date;
var $name;
var $designation;
var $employeeid;
var $noofdays;
var $from;
var $to;
var $reasonforleave;
var $address;
var $contactno;
var $approvedby;
function complaints($leave_id = '')
{
global $db,$smarty;
if ( $leave_id )
{
$this->leave_id = $leave_id;
$sql = "SELECT * FROM leaveform WHERE leave_id=$this->leave_id";
$leaveform = $db->getRow($sql);
if($leaveform != null)
{
$this->leave_id = trim(stripslashes($leaveform['leave_id']));
$this->branch = trim(stripslashes($complaints['branch']));
$this->date = trim(stripslashes($complaints['date']));
$this->name = trim(stripslashes($complaints['name']));
$this->designation = trim(stripslashes($complaints['designation']));
$this->employeeid = trim(stripslashes($complaints['employeeid']));
$this->noofdays = trim(stripslashes($complaints['noofdays']));
$this->from = trim(stripslashes($complaints['from']));
$this->to = trim(stripslashes($complaints['to']));
$this->reasonforleave = trim(stripslashes($complaints['reasonforleave']));
$this->address = trim(stripslashes($complaints['address']));
$this->contactno = trim(stripslashes($complaints['contactno']));
$this->approvedby = trim(stripslashes($complaints['approvedby']));
}
}else {
$this->leave_id = "";
$this->branch = "";
$this->date = "";
$this->name = "";
$this->designation = "";
$this->employeeid = "";
$this->noofdays = "";
$this->from = "";
$this->to = "";
$this->reasonforleave = "";
$this->address = "";
$this->contactno = "";
$this->approvedby = "";
}
}
/***
Add Church Admin
Used Php Files : Index.php
***/
function addleave()
{
global $db, $smarty;
$sql = "INSERT INTO leaveform(branch,date,name,designation,employeeid,noofdays,from,to,reasonforleave,address,contactno,approvedby) VALUES
('$this->branch','$this->date', '$this->name','$this->designation','$this->employeeid','$this->noofdays','$this->from','$this->to','$this->reasonforleave','$this->address',
'$this->contactno','$this->approvedby')";
$result = $db->Execute($sql);
$leave_id = $db->insert_ID();
if($result)
{
return $leave_id;
}else{
return 0;
}
}
function updateTicket()
{
global $db, $smarty;
$sql = " UPDATE leaveform SET ticket_no = '$this->ticket_no' WHERE leave_id = '$this->leave_id' ";
$leave_id = $db->Execute($sql);
if($leave_id){
return true;
}else{
return false;
}
}
}
?>
i am using three php codes. i have a error with invalid details in php form.
kindly help me to solve this issue..and i am new to php
The failure is because of the below piece of Code. You are basically not setting the parameters correctly.
$sql = "INSERT INTO leaveform(branch,date,name,designation,employeeid,noofdays,from,to,reasonforleave,address,contactno,approvedby) VALUES
('$this->branch','$this->date', '$this->name','$this->designation','$this->employeeid','$this->noofdays','$this->from','$this->to','$this->reasonforleave','$this->address',
'$this->contactno','$this->approvedby')";
$result = $db->Execute($sql);
$leave_id = $db->insert_ID();
No one would go around debugging it for you. Turn on the "Show errors" options in your php.ini. The below link will help you.
https://www.inmotionhosting.com/support/website/php-troubleshooting/troubleshoot-php-errors
This will start showing the errors in the page itself and you should be able to debug.
Advice: Use PDO objects and bind every parameter to execute script in a cleaner way.
At the very top of my document I created a varible called $selectedID using $_GET to get the id of the selected user(ill later switch it to an encrypted string) so the url looks a little like this profile.php?id=4. I then have a function which loops though a database for all the blog post with the same number as the $selectedID variable, which is inputted as the second parameter of the function.
The issue is that the second parameter in my function $selectedId Is supposed to be used as a number to filter by while looping through my database, but it seems like the variable is not properly being inserted into my function therefore the loop is not working properly
This is my main page showing the information
<div id="blogFeed">
<ul>
<!-- Exicute the fucntion from the functions.inc.php which grabs all the feed articles from the database -->
<?php
$_result = display_blogs_profile($connect, $selectedId);
// Count how many rows are in the database
$limit = count($_result);
// Loop through all the articles(rows) from the database
for($i = 0; $i < $limit; $i++){
// Define varibles for each column
$id = $_result[$i][6];
$title = $_result[$i][0];
$date = $_result[$i][1];
$article = $_result[$i][2];
$photo = $_result[$i][3];
$icon = $_result[$i][4];
$author = $_result[$i][5];
$findAuthor = $_result[$i][7];
$feedRadius = "50px";
$iconPhoto = "newsRed.png";
$photo = $_result[$i][3];
$feedRadius = "0px";
$contentLink = "Read More";
$realignIcon = 'margin-right: -195px;';
// Only show the first 10 articles the stop building the html
if((string)$employeeID != $findAuthor){
continue;
}else{
?>
<li>
<div class="eventBoxImage" style="background: url('images/<?php echo $photo ?>'); background-size: cover; background-position: center; border-radius: 50px"></div>
<div class="eventContentContainer">
<h1><?php echo $title; ?></h1>
<h4>Posted By: <?php echo $author; ?></h4>
<h5><?php echo $date ?></h5>
<p><?php echo $article ?></p>
<h6><?php echo $contentLink ?></h6>
<span style="background: url('images/<?php echo $iconPhoto ?>') no-repeat; <?php echo $realignIcon; ?> background-size: 45px;"></span>
</div>
</li>
<?php }}?> <!-- Close php loop -->
</ul>
</div>
<div id="blogPostContainer">
<h1 class="blog">Create Blog Post</h1>
<form class="blog" action="includes/insert.php" method="post">
<input name="blogTitle" type="text" placeholder="Enter The Blog Title" required/>
<input name="blogArticle" type="textbox" placeholder="Enter Your Blog Post" required/>
<input name="blogSubmit" type="submit" value="Submit" />
</form>
</div>
<div id="profileSearch">
<div id="searchBar">
<form id="search" action="index.php" method="POST">
<div id="searchIcon"></div>
<input type="search" name="search" placeholder="Search Through <?php echo $firstName ?>'s Posts" onkeyup="searchQ();" />
<input type="hidden" name="editId" id="editId" value="<?php echo $employeeID ?>" />
<div id="userResults">
</div>
</form>
</div>
</div>
</div>
And here is the function
function display_blogs_profile($connect, $selectId)
{
$sql = "SELECT * FROM intranet";
$query = mysqli_query($connect, $sql);
$finalArray = array();
$i = 0;
while ($row = mysqli_fetch_assoc($query)) {
$id = $selectId;
$title = $row['title'];
$date = $row['date'];
$article = $row['article'];
$photo = $row['photo'];
$icon = $row['icon'];
$authorID = $row['findAuthor'];
$author = $row['author'];
if($row['icon'] === "3" && $authorID === $selectId){
$finalArray[$i] = array($title, $date, $article, $photo, $icon, $author, $id, $authorID);
}
$i++;
}
$finalArrayDesc = array_reverse($finalArray);
return $finalArrayDesc;
}
Got it! Thanks for all the help! The if Get statement and the if $employeeID != $findAuthor statement would break or continue the loop no matter what unless on my own account. I just had to take the $employeeID != $findAuthor statement out :)
This is true
"it seems like the variable is not properly being inserted"
That is because it was never set. Try this:
$selectedID = $_GET['id'];
right after the check of if(isset($_GET['id'])) so
if(isset($_GET['id'])){
$selectedID = $_GET['id'];
....