Name
Date of Visit
Alice
30-01-2023
Bob
31-01-2023
<?php
if (isset($_POST['savevisitor']))
{
$dateofvisit = $_POST['dateofvisit'];
$visitorname = $_POST['visitorname'];
$my_query = "insert into visitors(dateofvisit,visitorname) values('$dateofvisit','$visitorname')";
$result = mysqli_query($conn, $my_query);
if($result)
{
echo "Successfully Inserted";
}
else
{
echo "Failed";
}
}
?>
This is my PHP code for inserting data into database of visitors. My question is I have to limit the number of visitors per day, so how do I do that?
I want to allow only 5 persons per day. If 6th person try to book then show booking is full.
Related
In my page there are 2 values which is employee id and type, inside type in and out is there. if student select in so value inserted in in time in mysql and if student select out so value inserted in out time column. But in my case not this happended. In time only inserted for student id 1 after that whenever i select in time for any student it will goes in else part and show me already added. Out time updated for everyone but in time is not inserted for everyone so help me for this.
<?php
session_start();
$branch=$_SESSION['branch'];
include('../dist/includes/dbcon.php');
$emp_id = $_POST['emp_id'];
$type = $_POST['type'];
//echo $type;
$query2=mysqli_query($con,"select in_time,emp_id from emp_a")or die(mysqli_error($con));
//$count=mysqli_num_rows($query2);
while($row=mysqli_fetch_array($query2))
{
$in = $row['in_time'];
$id = $row['emp_id'];
}
//echo $in;
if($type == 'In' && is_null($in))
{
mysqli_query($con,"INSERT INTO emp_a(emp_id,att_date,in_time,status) VALUES('$emp_id',curdate(),now(),1)")or die(mysqli_error($con));
echo "<script type='text/javascript'>alert('Successfully added time log!');</script>";
}
elseif($type == 'Out')
{
mysqli_query($con,"update emp_a set out_time = now(),status = 1 where emp_id = '$emp_id' and att_date=curdate()")or die(mysqli_error($con));
echo "<script type='text/javascript'>alert('Leaving Time added!');</script>";
}
else
{
echo "<script type='text/javascript'>alert('No!');</script>";
}
echo "<script>document.location='home.php'</script>";
?>
You need to pass $emp_id in order to get in_time, out_time for perticular employee, currently it gives in and out time for all the employees and get the last employee detail from the while loop.
$query2=mysqli_query($con,"select in_time,emp_id from emp_a where emp_id='$emp_id'")or die(mysqli_error($con));
//$count=mysqli_num_rows($query2);
while($row=mysqli_fetch_array($query2))
{
$in = $row['in_time'];
$id = $row['emp_id'];
}
I am building a car parking application in which different users have different numbers of parking spots. This number is set by an administrator in a database. The user can input a numberplate which then will be added to a database as well. What I want is that when a user has occupied all the spots, that he will not be able to insert any more number plates.
However, now I have the following code at the moment:
if(isset($_POST['number_plate'])){
$numberPlate = $_POST['number_plate'];
$user_id = $_SESSION['id'];
$query = mysql_query("SELECT `parking_spots` FROM `login` WHERE `id` = ".$user_id." ");
$row = mysql_fetch_assoc($query);
$totalNumberOfSpots = $row['parking_spots'];
$occupiedNumberOfSpots = 0;
$sql = "INSERT INTO amsterdam (numberplate, user_id) VALUES ('$numberPlate','$user_id')";
if(mysql_query($sql))
{
echo 'numberplate added';
$occupiedNumberOfSpots++;
if($occupiedNumberOfSpots == $totalNumberOfSpots)
{
echo "There are no more spots avialable";
}
}
else
{
echo 'Something went wrong!';
}
}
But when I echo the $occupiedNumberOfSpots variable it keeps returning 1 and does not increment every time I add numberplate.
How can I solve this issue?
It is because You are running the same code each time You add a plate to your db.
this:
$occupiedNumberOfSpots = 0;
should be taken from db as well. I guess it should be like that:
$totalNumberOfSpots = 100; // for example
$occupiedNumberOfSpots = $row['parking_spots']; // taken from db
instead of:
$totalNumberOfSpots = $row['parking_spots'];
$occupiedNumberOfSpots = 0;
I am preparing a MLM website in which I have to update all users single leg income, direct income and referral income when admin login. I was used same codes for individual updates I;e for each users.
For Eg: I am user and I am going to check my earnings, I have to login and check...So at the time of login I am updating all earnings of that user in database. So if user want to see updated earning,each time they have to singout and login again...simple. But in this case when user login...they passing a userid in session...through which I am synchronizing all incomes.
This is all about a particluar user...and may be their will be several users, and code for each as same.
But when we taking about admin, than??
They have to see earnings of all users in one page. I want help in that...I think I have to run all codes, single leg income, direct income and refral income in while loop..So all users must selected one by one and all incomes updating.
By bit confused ...please help...I thought I explain best of my knowledge...
Codes here:
Direct Income:
<?php
$userid=$_SESSION['userid'];
$sql="SELECT * from personal where userid='$userid'";
$query=mysqli_query($conn, $sql);
while($row=mysqli_fetch_array($query))
{
$id=$row['id'];
}
$sql="SELECT * from personal where id>'$id' limit 10";
$query=mysqli_query($conn,$sql);
$a=0;
while($row=mysqli_fetch_array($query))
{
$userid=$row['id'];
$username=$row['userid'];
$sponserid=$row['sponserid'];
$amount=$row['amount'];
if($row['amount']==2500)
{
$total1=2500;
}
else if($row['amount']==5000)
{
$total1=5000;
}
else if($row['amount']==10000)
{
$total1=10000;
}
else
{
$total1=0;
}
$i=array(0.05,0.02,0.01,0.005,0.0025,0.0025,0.0025,0.0025,0.0025,0.0025);
$total[]=$total1*$i[$a];
$a++;
$level_income = array_sum($total);
}
$qry="update personal set direct_income='$level_income' where userid='".$_SESSION['userid']."'";
mysqli_query($conn,$qry);
$_SESSION['level_income']=$level_income;
?>
Referral Income Code
<?php
include('head.php');
$query = "SELECT userid,amount FROM personal WHERE sponserid='$_SESSION[userid]'";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result) or die(mysqli_error($conn)))
{
$total[]=$row['amount'];
$income=array_sum($total);
$_SESSION['refral_income']=$income * 0.05;
$qry="update personal set refral_income='$_SESSION[refral_income]' where userid='".$_SESSION['userid']."'";
}
?>
Single Leg Income Code:
<?php
include('head.php');
$sql="SELECT * from personal where userid ='$_SESSION[userid]'";
$query=mysqli_query($conn, $sql);
while($row=mysqli_fetch_array($query))
{
$id=$row['id'];
}
$sql="SELECT * from personal where id>'$id'";
$query=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($query))
{
$userid=$row['id'];
$username=$row['userid'];
$fullname=$row['fullname'];
$sponserid=$row['sponserid'];
$amount=$row['amount'];
if($row['amount']==2500)
{
$total1=10;
}
else if($row['amount']==5000)
{
$total1=20;
}
else if($row['amount']==10000)
{
$total1=40;
}
else
{
$total1=0;
}
$total[]=$total1;
$single_leg_income=array_sum($total);
}
$_SESSION['income']=$single_leg_income;
$qry="update personal set single_leg_income='$single_leg_income' where userid='".$_SESSION['userid']."'";
mysqli_query($conn,$qry);
?>
I am using simple coding, and thinking to use PDO..But before that actually want to know...How I can do this in a single page for all users as a Admin.
Thanks.
Thanks for checking out my question. I am trying to only update a value in my database if that field is null (so existing users won't be overwritten if someone tries to signup for a spot that is all ready taken and an error message will be output). I have listed below 2 of the most recent scripts I have tried. The first script works for updating the database if the select statement is not there but will overwrite users if entered for the same day and time. Thanks everybody!
$sql = ("SELECT `player1` FROM `users` where id = '$id' and Times = '$time'");
$result = $conn->query($sql);
if ($result->fetch_assoc === NULL) {
$update_player = ("UPDATE users SET player1 = '$name' where id = '$id' AND Times = '$time'")
if($update_player){
echo "Date for $name inserted successfully!";
}
}
else {
echo 'That spot is all ready taken!';
}
//2nd script
$query=mysql_query("UPDATE users SET
player1 = isNULL (player1, $name)
where id = '$id' AND Times = '$time'" );
if($query){
echo "Data for $name inserted successfully!";
}
else {
echo 'That spot is all ready taken!';
}
The following code should do the trick:
$query=mysql_query("UPDATE users SET
player1='$name'
where id = '$id' AND Times = '$time' AND player1 IS NULL" );
if(mysql_affected_rows() == 1){
echo "Data for $name inserted successfully!";
}
else {
echo 'That spot is all ready taken!';
}
Note that you should use pdo or mysqli functions instead.
Try This.
while($row = $result->fetch_assoc) {
if($row['player1'] == NULL){
$update_player = ("UPDATE users SET player1 = '$name' where id = '$id' AND Times = '$time'")
}
I try to retrieve the latest added bookingID from table booking but I do not know how to get it.
The scenario: when user click 'submit' button, mysql query will generate a parentBookID and store userID, eventID in table booking.Then query will get the created parentBookID to insert into table bookingDetail, user will make multiple bookings and these detail will store in table bookingDetail with same parentBookID.
The problem is, I do not know to get the current parentBookID to be store in table bookingDetail from table booking.
<?php
session_start();
if ( !isset($_SESSION['AUTHORIZED_USERNAME']) || empty($_SESSION['AUTHORIZED_USERNAME']) ) {
header("location:index.php");
}else{
$user=$_SESSION['AUTHORIZED_USERNAME'];
$userID= $_SESSION['AUTHORIZED_CUSTNO'];
$event=$_SESSION['EVENT_ID'];
include('db.php');
//check submitted totalDay
if (isset($_POST['submit']) && isset($_POST['totalDay'])) {
//insert parentBookingID
$parent=mysql_query("INSERT into booking (custNo, eventID,dateBook) VALUES ('$userID','$event',NOW())");
//no logic
$queryParent="SELECT parentBookID from booking where custNo='$userID' and eventID='$event'";
$queryParentResult=mysql_query($queryParent);
while($parentBook = mysql_fetch_array($queryParentResult)){
$parentBookID=$parentBook['parentBookID'];
$totalDay=$_POST['totalDay'];
$allBooth="";
foreach ($totalDay as $d) {
echo $d;
$bookingInfo = $d;
$bookingInfo = explode(" ", $bookingInfo);
echo $bookingInfo[0]; // boothAlias
echo $bookingInfo[1]; // boothID
echo $bookingInfo[2]; // day
$result = mysql_query("SELECT * FROM bookingDetail WHERE boothID='$bookingInfo[1]' and day='$bookingInfo[2]' and username='$user'");
$num_rows = mysql_num_rows($result);
if ($num_rows) {
echo "Exist";
}else{
$str = "INSERT INTO bookingDetail (username, custNo, eventID, date, day, boothAlias, boothID, parentBookID) VALUES ('$user', '$userID','$event',NOW(),'$bookingInfo[2]','$bookingInfo[0]','$bookingInfo[1]','$parentBookID');";
$res = mysql_query($str);
if($res)
echo 'Success';
else
echo 'Failure';
$allBooth= substr($allBooth, 0, -2);
echo "<p>Booth(s): <strong>$allBooth</strong> <strong>$user</strong> <strong>$event</strong><strong>$userID</strong></p>\r\n";
}
}
}
header("refresh:5;url=mybooking.php");
echo "<img src='loading16.gif' style='margin-top:8px; float:left'/>";
echo 'You\'ll be redirected in about 5 secs. If not, click here.';
}else{
echo "You do not make any booking";
header("refresh:5;url=booking2.php");
echo "<img src='loading16.gif' style='margin-top:8px; float:left'/>";
echo 'You\'ll be redirected in about 5 secs. If not, click here.';
}
?>
Assuming you have an AUTO_INCREMENT column in booking, you could use mysql_insert_id(); to get it.
you may set a column like id or timestamp, so you can use max() or order by id desc (SQL language) to get what you want