using Session ID to order something - php

So I am making a project where you can order food at school cafeteria, I already have a project where you can make a order with the menu and the student, and time.
But now I want that the student can order at it's own and not select any student so for the order it takes the currently logged in student. Sorry for bad english and thanks for every answer, i appreciate it.
[enter image description here](https://i.stack.imgur.com/NFz6G.png)
So Schueler is student and Menu is menu
uhrzeit is time
Now I want that there is no student to select, it takes the session id to make a order.
This is my order/create.php file
<?php
$title = "Menü reservieren";
include '../layouts/top.php';
session_start();
if(!isset($_SESSION['schuelerid'])) {
die('Bitte zuerst einloggen');
}
//Abfrage der Nutzer ID vom Login
$userid = $_SESSION['schuelerid'];
$email = $_SESSION['email'];
echo "Hallo Schueler: ".$userid;
echo " ".$email;
require_once('../../models/Bestellungen.php');
// create new/empty model, contains all input-values and error-messages
$r = new Bestellungen();
if (!empty($_POST)) {
$r->setMenu(isset($_POST['menu']) ? $_POST['menu'] : '');
$r->setUhrzeit(isset($_POST['uhrzeit']) ? $_POST['uhrzeit'] : 0);
$r->setSchueler(isset($_POST['schueler']) ? $_POST['schueler'] : 0);
// redirect to view-page after successfull saving
if ($r->save()) {
header("Location: view.php?id=" . $r->getId());
exit();
}
}
?>
<div class="container">
<div class="row">
<h2><?= $title ?></h2>
</div>
<form class="form-horizontal" action="create.php" method="post">
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-2">
<div class="form-group required">
<label class="control-label">Schueler *</label>
<select style="width: 200px" name="schueler">
<option selected></option>
<?php
require_once "../../models/Schueler.php";
foreach (Schueler::getAll() as $g){
?>
<option value='<?=$g->getId()?>'><?=$g->getName()?></option>
<?php
}
?>
</select>
<?php if (!empty($r->getErrors()['schueler'])): ?>
<div class="help-block"><?= $r->getErrors()['schueler'] ?></div>
<?php endif; ?>
</div>
</div>
<div class="col-md-2">
<div class="form-group required ">
<label class="control-label">Menu *</label>
<select name="menu" style="width: 200px">
<option selected></option>
<?php
require_once "../../models/Menu.php";
foreach (Menu::getAll() as $z){
?>
<option value='<?=$z->getId()?>'><?=$z->getName()?></option>
<?php
}
?>
</select>
<?php if (!empty($r->getErrors()['menu'])): ?>
<div class="help-block"><?= $r->getErrors()['menu'] ?></div>
<?php endif; ?>
</div>
</div>
</div>
<div class="row">
<div class="col-md-2">
<div class="form-group required ">
<label class="control-label">Uhrzeit *</label>
<input type="datetime-local" class="form-control" name="uhrzeit" value="">
<?php if (!empty($r->getErrors()['uhrzeit'])): ?>
<div class="help-block"><?= $r->getErrors()['uhrzeit'] ?></div>
<?php endif; ?>
</div>
</div>
<div class="col-md-1"></div>
</div>
<div class="row">
<div class="form-group">
<button type="submit" name="submit" class="btn btn-success">Erstellen</button>
<a class="btn btn-default" href="view.php">Abbruch</a>
</div>
</div>
</form>
</div> <!-- /container -->
<?php
include '../layouts/bottom.php';
?>

Related

Convert a text field to a selection field drop-down lists

the following code shows a text field for the user to write their city, I would like to convert that text field into a selection field with a drop-down list. My knowledge in php is very basic, I appreciate you can help me.
The code is located in a PHP template of a user profile.
<?php
global $current_user;
wp_get_current_user();
$userID = $current_user->ID;
$user_login = $current_user->user_login;
$state = get_the_author_meta( 'user_state' , $userID );
?>
<div class="row">
<div class="col-md-12">
<div class="couple-profile-main-block dashboard-box">
<form id="user-profile" class="ajax-auth form-horizontal" method="post">
<div class="status"></div>
<?php wp_nonce_field('ajax-user-profile-nonce', 'security'); ?>
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<div class="col-sm-4"><label><?php esc_html_e('State','theme');?>*</label></div>
<div class="col-sm-8"><input class="form-control" id="state" name="state" type="text" value="<?php echo esc_attr($state);?>"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="row">
<div class="col-sm-offset-4 col-sm-8"><button id="couple-profile-on" class="btn btn-pink"><?php esc_html_e('Update Profile','theme');?></button></div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
You’ll need to switch out that text-input to a select input - check out https://www.w3schools.com/tags/tag_select.asp
In your case, something like this should replace where the text input is.
<select class="form-control" id="state" name="state">
<option value=“State1” <?php if ($state == “State1”) echo “selected”; ?>>State 1</option>
<option value=“State2” <?php if ($state == “State2”) echo “selected”; ?>>State 2</option>
<option value=“State3” <?php if ($state == “State3”) echo “selected”; ?>>State 3</option>
</select>

How Can I Get Admin Id and Use it across the application?

In codeigniter I want to add the product on admin Id which is required in DB Table. when Admin log in his ID must be catch. whenever and where Id is needed it must be available.
here is my VIEW in which
<?php echo form_open_multipart('admin/add_product');?>
<!------------------Product Name------------------------>
<div class="row form-group">
<div class="col-sm-10">
<label>Product Name: *</label>
<?php echo form_input(['name'=>'name','class'=>'form-
control','placeholder'=>'Product name..','value'=>
set_value('name')])?>
<div>
<?php echo form_error('name');?>
</div>
</div></div>
<!------------------Product Cat---------------------------->
<div class="row form-group">
<div class="col-sm-12">
<label>Category: *</label> <br>
<select class="selectpicker" name="pro_cat_id">
<option label="Select Category">Category</option>
<?php
foreach($product_cat as $cat)
{?>
<option value="<?= $cat['pro_cat_id'] ?>"><?= $cat['category'] ?>
</option>
<?php }?>
</select>
<div>
<!------------------Product Price---------------------------->
<div class="row form-group">
<div class="col-md-10">
<label>Product Price: *</label>
<?php echo form_input(['name'=>'price','class'=>'form-
control','placeholder'=>'Product Price..','value'=>
set_value('price')])?>
<div>
<?php echo form_error('price');?>
</div>
</div></div>
<!------------------Product Location---------------------------->
<div class="row form-group">
<div class="col-md-10">
<label>Product Location: *</label>
<?php //echo form_input(['name'=>'location','class'=>'form-
control','placeholder'=>'Product Location'])?>
</div></div>
</div></div>
<!-----------End of body---------------->
</div>
<div class="modal-footer">
<input name="" type="submit" value="ADD" class="btn btn-success">
</div>
<?php echo form_close();?>
</div>
</div>
</div>
Here is my Controller
function add_product(){
$data['product_size'] = $this->show->show_product_size();
$data['product_cat']= $this->show->show_product_category();
$data['product_color'] = $this->show->show_product_color();
$data['pr']= $this->show->show_provider();
$post= $this->input->post();
$ =this->form_validation->set_error_delimiters("<p class='text-danger'>","
</p>");
if($this->form_validation->run('add_product')){
if($this->add->add_product($post)==TRUE){
$this->db->close();
$this->session->set_flashdata('success','Product has been added
successfully');
return redirect('admins/pages/add_product_page');
//$this->load->admin_template('admin/add_product',$data);
}
}
else
{
$this->session->set_flashdata('failled','Sorry, fail to add product');
//return redirect('admins/pages/add_product_page');
$upload_error= $this->upload->display_errors();
$this->load->admin_template('admin/add_product',$data);
}
}
Use this query, preferably in your controller:
$this->session->userdata('id');

Codeigniter : Form became weird when using select option (form_dropdown) to edit my data

It's work fine when I am inputing data using select option on CI but when I try to edit or update my data again my form became weird like this (See picture edit form)
Here my controller :
public function updateobat ($kode_obat){
if($_POST==null){
$this->load->model('a_model');
$data['hasil'] = $this->a_model->select2($kode_obat);
$data['title'] = "Data Stok Obat | Praktik Dokter Umum";
$data['tb_jenisobat'] = $this->a_model->ambil_jenisobat();
$this->template->admin('admin/edit-obat',$data);
}else{
$this->load->model('a_model');
$this->a_model->update_obat($kode_obat);
$data['title'] = "Data Stok Obat | Praktik Dokter Umum";
$data['obat'] = $this->a_model->ambil_obat();
$this->template->admin('admin/data-obat',$data);
}
}
Model
public function update_obat($kode_obat){
$nama_obat = $this->input->post('nama_obat');
$kode_jenis_obat = $this->input->post('kode_jenis_obat');
$stok= $this->input->post('stok');
$data = array('nama_obat' => $nama_obat,
'kode_jenis_obat' => $kode_jenis_obat,
'stok' => $stok,
);
$this->db->where('kode_obat',$kode_obat);
$this->db->update('tb_obat',$data);
}
public function select2($kode_obat){
return $this->db->get_where('tb_obat', array('kode_obat' => $kode_obat))->row();
}
View
<!-- Main content -->
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-8">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Silahkan melakukan edit stok obat</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="<?php echo base_url().'index.php/a_controller/updateobat/'.$hasil->kode_obat; ?>" method="post">
<div class="box-body">
<div class="form-group">
<label for="exampleInputNama">Nama Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-medkit"></i></span>
<input type="text" class="form-control" name="nama_obat" id="exampleInputNama" placeholder="Nama Obat" action="<?php echo form_input('nama_obat',$hasil->nama_obat);?>">
</div>
</div>
<div class="form-group">
<label for="exampleInputJenis">Jenis Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-list"></i></span>
<select name="kode_jenis_obat" class="form-control" action="<?php echo form_dropdown('kode_jenis_obat',$hasil->kode_jenis_obat);?>">
<option value="none" selected="selected">Pilih Jenis Obat</option>
<!-----Displaying fetched cities in options using foreach loop ---->
<?php foreach($tb_jenisobat as $jenisobat):?>
<option value="<?php echo $jenisobat->kode_jenis_obat?>"><?php echo $jenisobat->nama_jenis_obat?></option>
<?php endforeach;?>
</select>
</div>
</div>
<div class="form-group">
<label for="exampleInputUsername">Stok Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-list"></i></span>
<input type="text" class="form-control" name="stok" id="exampleInputUsername" placeholder="Stok" action="<?php echo form_input('stok',$hasil->stok);?>">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
<!-- /.box -->
</div>
</div>
<!-- /.row -->
</section>
I am using form_dropdown for my select option
I think there is something wrong with my controller syntax?
$data['tb_jenisobat'] = $this->a_model->ambil_jenisobat();
But I am not sure enough, can someone help me?
Picture : edit form

Cannot able to increment row count in every year

I want to restrict user to upload 50 photos per year. Say for example user can upload 50 photos in 2015 and after that user gets error message. Again in 2016 user can able to upload 50 photos but the photo which user uploads in 2015 will not be deleted.
This is my upload.php
<?php
// set timeout period in seconds
$inactive = 100;
// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) ) {
$session_life = time() - $_SESSION['timeout'];
if($session_life > $inactive)
{ session_destroy(); header("Location: logout.php"); }
}
$_SESSION['timeout'] = time();
include "lib/connection.php";
if (!isset($_SESSION['user']))
{
//die ("Access Denied");
header("location:index.php?msg=Please login with valid credentials");
}
if (isset($_SESSION['user']))
{
include "lib/top.php";
include "lib/left.php";
?>
<div class="col-md-10">
<div class="row">
<div class="col-md-12">
<ol class="breadcrumb">
<li>Home</li>
<li class="active">Upload Photo</li>
</ol>
</div>
</div>
<?php
if(!empty($_GET['msg'])) {
$message = $_GET['msg'];
print "$message";
}
?>
<div class="block block-fill-white">
<div class="header">
<h2>Upload Photo</h2>
</div>
<FORM METHOD="POST" ACTION="pic-db-upload.php" enctype = "multipart/form-data">
<div class="content controls">
<div class="form-row">
<div class="col-md-3">Title:</div>
<div class="col-md-9"><input type="text" class="form-control" id="title" name="title" required placeholder="Enter some Title"/></div>
</div>
<div class="form-row">
<div class="col-md-3">Description:</div>
<div class="col-md-9"><textarea class="form-control" id="description" name="description" required></textarea></div>
</div>
<?php
$sel="SELECT * FROM `category`";
$tab=mysql_query($sel);
?>
<div class="form-row">
<div class="col-md-3">Category:</div>
<div class="col-md-9">
<select class="form-control" name="category" required>
<option value="0">Select</option>
<?php
while($row=mysql_fetch_array($tab))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['cname']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-row">
<div class="col-md-3">Thumbnail Height:</div>
<div class="col-md-9"><input type="text" class="form-control" id="theight" name="theight" required placeholder="Enter height"/></div>
</div>
<div class="form-row">
<div class="col-md-3">Thumbnail Width:</div>
<div class="col-md-9"><input type="text" class="form-control" id="twidth" name="twidth" required placeholder="Enter width"/></div>
</div>
<div class="form-row">
<div class="col-md-3">File:</div>
<div class="col-md-9">
<div class="input-group file">
<input type="text" class="form-control"/>
<input type="file" id="picture" name="picture" required/>
<span class="input-group-btn">
<button class="btn btn-primary" type="button">Browse</button>
</span>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-3"> </div>
<div class="col-md-9" style="align:midle">
<input type="reset" class="btn btn-danger">
<input type="submit" class="btn btn-primary" name="submit" value="UPLOAD">
</div>
</div>
</div>
</div>
</form>
</div>
</div>
<?php
include "lib/bottom.php";
}
?>
and this is my insert query
<?php
ini_set('display_errors', 0);
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ob_start();
include("lib/connection.php");
if (!isset($_SESSION['user']))
{
//die ("Access Denied");
header("location:index.php?msg=Login with valid credentials");
}
if (isset($_SESSION['user']))
{
//======================= INSERT =================================================
if($_POST[submit]=="UPLOAD")
{
if($_POST[title]!="" && $_POST[category]!="" && $_POST[description]!="" && $_FILES[picture][name]=="")
{
header("location:upload-photo.php?msg=You have not inserted any picture ");
}
else
{
$resource = mysql_query("SELECT COUNT(id) FROM album");
$count = mysql_result($resource,0);
if ($count < 50)
{
//======================== THUMBNAIL CODE ========================
function thumb($a,$b)
{
$image_path=$a;
$new_width=$_POST[twidth];
$new_height=$_POST[theight];
$thumb_path=$b;
$ext=strtolower(end(explode('.',$image_path)));
if($ext=='jpg'||$ext=='jpeg'||$ext=='gif'||$ext=='png')
$img=#imagecreatefromjpeg($image_path);
else
die("only jpg or jpeg or gif or png format supported");
$width=imagesx($img);
$height=imagesy($img);
$tmp_img=imagecreatetruecolor($new_width,$new_height);
imagecopyresized($tmp_img,$img,0,0,0,0,$new_width,$new_height,$width,$height);
imagedestroy($img);
imagejpeg($tmp_img,$thumb_path);
}
//==================================================================
$p=md5(rand(1,1000));
$pict=$p.".jpg";
$target_path1 = "big_pic/big_".$pict;
copy($_FILES[picture][tmp_name],$target_path1);
$target_path2="thumb_pic/thumb_".$pict;
thumb($target_path1,$target_path2);
$ins = "INSERT INTO `album`(`id`,`date`,`title`,`description`,`cid`,`picture`,`thumbnail`,`default`)VALUES('',now(),'$_POST[title]','$_POST[description]','$_POST[category]','$target_path1','$target_path2','$pict')" ;
mysql_query($ins);
header("location:upload-photo.php?msg=Photo successfully uploaded");
}
else
{
header("location:upload-photo.php?msg=Sorry maximum number of photo uploaded. Please contact system administrator");
}
}
}
//======================= END INSERT =================================================
}
?>
Change the query to only count the photos in the current year:
SELECT COUNT(*) FROM album WHERE YEAR(date) = YEAR(NOW())

Log in page looks like not redirecting to the next page how do i fix this? [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
Log in page looks like not redirecting to the next page how do i fix this kind of problem? Log in page looks like not redirecting to the next page and still stick on index.php page how do i fix this error.... i think the session is the problem?
index.php
<?php include('header.php'); ?>
<?php include('navbar.php'); ?>
<div class="container">
<div class="margin-top">
<div class="row">
<div class="span12">
<div class="login">
<div class="log_txt">
<p><strong>Please Enter the Details Below..</strong></p>
</div>
<form class="form-horizontal" method="POST">
<div class="control-group">
<label class="control-label" for="inputEmail">Username</label>
<div class="controls">
<input type="text" name="username" id="username" placeholder="Username" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" name="password" id="password" placeholder="Password" required>
</div>
</div>
<div class="control-group">
<div class="controls">
<button id="login" name="submit" type="submit" class="btn"><i class="icon-signin icon-large"></i> Submit</button>
</div>
</div>
<?php
if (isset($_POST['submit'])){
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($dbcon, $query)or die(mysql_error());
$num_row = mysqli_num_rows($result);
$row=mysql_fetch_array($result);
if( $num_row > 0 ) {
header('location:../admin_uplbcdc/dashboard.php');
$_SESSION['id']=$row['user_id'];
}
else{ ?>
<div class="alert alert-danger">Access Denied</div>
<?php
}}
?>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include('footer.php') ?>
dashboard.php
<?php require_once('../db/dbcon.php'); ?>
<?php include('../admin_uplbcdc/session.php'); ?>
<?php include('../admin_uplbcdc/header_2.php'); ?>
<?php include('../admin_uplbcdc/navbar.php'); ?>
<div class="container">
<div class="margin-top">
<div class="row">
<?php include('../admin_uplbcdc/head.php'); ?>
<div class="span2">
<?php include('../admin_uplbcdc/sidebar.php'); ?>
</div>
<div class="span10" align="center">
<br/> <br/>
<?php include('../admin_uplbcdc/slider.php'); ?>
</div>
</div>
</div>
</div>
<?php include('footer.php'); ?>
Set correctly error reporting and see the Warnings.
You can't start session or redirect using header when there is any output before, move session_start to the top of PHP script.
index.php
<?php include('header.php'); ?>
<?php include('navbar.php'); ?>
<?php
session_start();
if (isset($_POST['submit'])) {
// all PHP code goes here
}
?>
HTML goes here

Categories