PHP MYSQL Attendance Limit Once a Day - php

I am Beginner in php and i am making the project on attendance system but i don't know how to code for single-day attendance for the student once in a day. I didn't get the appropriate result.
Thanks in advance.
<?php
include 'config.php';
if($_SESSION['user']==true){
if(isset($_POST['submit'])){
$sql= ("INSERT INTO `student_attendence`(`Student_id`,`Date`,`Attendence`) VALUES ('".$_SESSION['user']."','$date','$attendence') ");
if(count($error)==0){
if(!mysqli_query($conn,$sql)){
echo "Error ".mysqli_error($conn);
}
else{
header("location:studentlogin.php");
}
}
}
}
else{
header("location:studentlogin.php");
}
?>

You have to increase one more attribute in DB table which is date.
So whenever you insert a new attendence insert today's date also.
Always check for today's date must not present in DB before inserting the attendence.
So while inserting the attendence in DB, you must have to check first -
If today's date is already present in DB
true : display message(today's attendence has already taken).
false: save the attendence in DB.
Note -
MySQL's default DATE format is YYYY-MM-DD.
This is the quick method you can impliment.

Related

using php to insert records in my database

I am trying to insert data into my database from a user form using php
I normally do not have any issues when doing this except this time and I believe it is because of the date type as explained below
Here is the code:
<?php
session_start();
$user = $_SESSION['who'];
if (!$_SESSION['who']){
header("location: login.php");
}
if (isset($_POST['submit'])){
require_once('dbconn.php');
$flightID = $_POST["flights"];
$sql= "INSERT INTO booking (flight_id,customer_id,checkedin,checkin_datetime, booking_datetime,baggage)
VALUES ('1', '1','0','10-10-10', '10-10-10','30')";
if($recordSet = $dbConn->query($sql)) {
echo "record set";
}
else
echo "not set";
}
?>
I will be using variables instead of hard coded values for my insert statement however I am just trying to get it to work.
Here are the attributes in my database table for booking
id, flight_id, customer_id, checkedin, checkin_datetime, booking_datetime, baggage
The ID is automatic which is why it is not in my sql statement
currently my page is displaying not set I think it may be due to the date field as I am not sure which format it may use.
UPDATE: I changed the date in my sql statement and it still is not working, no errors coming up as well.
You can Use
$sql= "INSERT into booking (flight_id,customer_id,checkedin,checkin_datetime, booking_datetime,baggage)
VALUES ('1', '1','0','2020-10-10', '2020-10-10','30')";
$sql= "INSERT INTO booking (flight_id,customer_id,checkedin,checkin_datetime, booking_datetime,baggage)
VALUES ('1', '1','0','2010-10-20', '2010-10-20','30')";
Note :
date format: YYYY-MM-DD
All id must be exists in the database (if you use Foreign Key for flight_id and customer_id).
Date format looks ok in the sql statement.
make sure ID field is auto increment in the database as you have not used in the sql statement.

PHP & MYSQL -- dont insert same data

I need your help. I’m stuck at a point from where I can’t figure it out that how to stop a product to be booked twice a day on a web form.
I have 41 products to rent out on daily basis.
My problem is: if a client books a product #4 (pg_no) and date 26-06-2017 then another client isn't able to book the same product for the same period of time.
If a client selects the same product and date, then a message should appear saying "already booked - please select another date"
Also, do I have to define anything on database level?
Your help will be highly appreciated.
Please note: this rule is only for product # (pg_no) and date fields are not allow for the same day.
<?php
//connecting string
include("dbconnect.php");
//assigning
$pg_no=$_REQUEST['pg_no'];
$name=$_REQUEST['Name'];
$tele=$_REQUEST['Tele'];
$city=$_REQUEST['City'];
$date=$_REQUEST['Date'];
//checking if pg_no and Date are same
$check=mysqli_query($db_connect,"SELECT * FROM lstclient WHERE pg_no='{$pg_no}', Date='{$date}'");
if(mysqli_fetch_row($check) ==0)
{
echo "Already booked please select another date<br/>";
}
//if not the insert data
else
{
$query=mysqli_query($db_connect,"INSERT INTO lstclient(pg_no,Name,Tele,City,Date) VALUES('$pg_no','$name','$tele','$city','$date')") or die(mysql_error());
}
mysqli_close($db_connect);
// messaging
if($query)
{
header("location:index.php?note=failed");
}
else
{
header("location:index.php?note=success");
}
?>
Create a UNIQUE KEY constraint on your table between the two columns.
ALTER TABLE lstclient ADD CONSTRAINT `pg_no_date` UNIQUE (pg_no, date)
Then when you try to insert a row that violates this constraint mysql will throw an error. You should catch this MySQL error and respond with the correct error.
if( mysqli_errno() == 1062) {
// Duplicate key error
}
Add a unique key to your lstclient table like ryan suggested. Also, your check query has syntax error, it should be like below.
SELECT * FROM lstclient WHERE pg_no='{$pg_no}' AND Date='{$date}'

below INSERT INTO SELECT not working with user_id,qty and ord_date,how it can be done?Thanx in advance

<?php
include("config.php");
if ($_SESSION['uname']=='' || $_SESSION['uid']=='') {
header('location:login.php');
} else {
$pid=$_GET['prod_id'];
$q="INSERT INTO
cart_tbl(cat_id,com_id,prod_id,
user_id,price,qty,ord_date)
SELECT cat_id,com_id,
prod_id,'".$_SESSION['uid']."',
(prod_price-prod_discount) as price,1,
date(Y-m-d h:i:s) FROM product_tbl
WHERE prod_id='".$pid."'";
$re=mysql_query($q);
}
?>
I want to insert user_id=$SESSION['UID'],qty=1 and ord_date=date(Y-m-d h:i:s) function and other things from product_tbl. but,code above not inserting anything in cart_tbl.
The SQL query is not correct. As per the query you are inserting values by selecting from a table. In your above written query there couldn't be a field like $_SESSION or date parameter. Insert values by traditional SQL query & from other tables do it as you are doing.

Unable to update date and time fields into Mysql record

I need some help with this :
I have a MySql database which has several text field and 2 sets each of date and time fields. The problem is while one set of date and time field gets updated correctly, the other set does not. The four fields are:
complaint_date
complaint_time
admin_date
admin_time
The four fields take date and time fields through date function. While the last two get updated into the table the first two display no value (all 0's to be precise). The name of the database is complaint_data and the table name is complaints.
The code for complaint_date and complaint_time
$user="root";
$password="";
$database="complaint_data";
$localhost="localhost";
mysql_connect($localhost,$user,$password);
#mysql_select_db($database) or die("Unable to open database");
$user=$_SESSION['staffnumber'];
$type=$_SESSION['input'];
$catg=$_SESSION['input1'];
$text=$_POST['Comment'];
$status="open";
$date=date_create();
$_SESSION['cid']=date_timestamp_get($date);
$cid=$_SESSION['cid'];
$cmpdate=date("Y-m-d");
date_default_timezone_set("Asia/Kolkata");
$cmptime=date("H:i:s");
$query="INSERT INTO complaints (`username`, `type`, `category`, `subcategory`, `comment`, `priority`, `status`, `complaintid`)
VALUES('$user','$type','$catg','$subcatg','$text','$p','$status','$cid')";
$result=mysql_query($query);
$query1="UPDATE complaints SET `complaint_date`='$cmpdate' , complaint_time='$cmptime' WHERE complaintid='$cid' ";
$result1=mysql_query($query1);
mysql_close();
header('Location:main.html');
TIA :)
Actually your date format is not same with mysql format so try to use below code.
$date = date("Y-m-d H:i:s");
$date = strtotime($date);
$cmpdate = date('Y-m-d',$date);
$cmptime = date('H:i:s',$date);
$query1="UPDATE complaints SET `complaint_date`='$cmpdate' , complaint_time='$cmptime' WHERE complaintid='$cid' ";
$result1=mysql_query($query1);
Hope this helps.
Folks , it worked. Thanks all for your inputs. There was no mistake except that another part of the code was inhibiting the insert function. So silly of me to have missed it out !
Thanks again :)

How do i fetch date and time from MySQL table row

i found an old blog script (very old) kicking around on my PC. im having troubles with fetching the date and time from my DB to display in PHP. Can someone help me.
This is my MySQL DB setup.
CREATE TABLE blog_posts (
id int(11) NOT NULL auto_increment,
title varchar(30) NOT NULL default '',
news text NOT NULL,
poster varchar(15) NOT NULL default '',
date timestamp(14) NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
And this is what im using to send a post:
<?php
include "db.php";
$title=$_POST['title'];
$news=$_POST['news'];
$poster=$_POST['poster'];
$query="INSERT INTO $blogposts (title, news, poster) VALUES ('$title', '$news', '$poster')";
$result=mysql_query($query) or die (mysql_error());
mysql_close();
header("Location: post.php");
?>
And finally this is what im using to call the date on the front-end:
<?php echo "posted on: - ".$day.".".$month.".".$year." at ".$hour.":".$min; ?>
I'm no expert (clearly) but the call for the date doesnt look right to me. Anyone have any ideas on how i could make it work, or even make it better??
EDIT::
<?php
include "db.php";
//query
$query="SELECT * FROM ".$blogposts." ORDER BY id DESC LIMIT 0,$limit ";
$result=mysql_query($query) or die (mysql_error());
//loop
while($row=mysql_fetch_array($result))
{
$querycomment = "SELECT ID FROM ".$newscomments." WHERE newsid=".$row['id'];
$resultcomment=mysql_query($querycomment) or die (mysql_error());
$num_rows = mysql_num_rows($resultcomment);
ereg("^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})",$row['date'],$res);
$year=$res[1];
$month=$res[2];
$day=$res[3];
$hour=$res[4];
$min=$res[5];
$sec=$res[6];
?>
Thanks
As date column has TIMESTAMP data type and got not null constraint so it will have default current_timestamp value if no data posted on date column while inserting. see mysql timestamp ref manual.
So to achieve a result that you ask you can try fetch that data by using code like this:
$sql = "select id, title, news, poster, DATE_FORMAT(date,'%d.%m.%Y at %k:%i') as posteddate from blog_posts";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo $row["title"]."<br/>";
echo "By : " . $row["poster"]."<br/>";
echo "posted on: - " . $row["posteddate"]."<br/>"; //"posted on: - 03.03.2011 at 7:35"
}
You don't seem to be adding any value to the date field in your insert command. You could use now() to add the current time stamp to add the current time during the insert. To extract the time you could either (a) read the date field from the table and format the time using PHP or (b) get formatted fields of the time stamp in the select command using the mysql date and time functions
You also need to have some code to read the values in your PHP to read the values for the fields. This seems to be missing in your question.
Surely you haven't defined any of the date variables, that you're trying to use?

Categories