Hello i have a table in sql called premium now when "user" with premium (in the table that there is 1 number is premium and 0 is not) then in the code before you i have an avant that every hour when clicked need to get something in return in this case this is a table called userMana Now i want premium name Will receive more (that is paid money) then is this way correct?
The first part I tried and after that I used the bottom part
if (isset($_GET['daily'])) {
if ($user->checkHours($user->hourEvent)) {
$time = date("Y-m-d H:i:s", time());
$mysqli->prepare("UPDATE `users` SET `hourEvent` = ?, `userMana` = `userMana` + 10, `userCitizens` = `userCitizens` + 1 WHERE userId = ?")->execute([$time, $user->userId]);
header("Location: /אזור-משחק/בסיס");
} else if ($user->premium > 0) {
if ($user->checkHours($user->hourEvent)) {
$time = date("Y-m-d H:i:s", time());
$mysqli->prepare("UPDATE `users` SET `hourEvent` = ?, `userMana` = `userMana` + 101, `userCitizens` = `userCitizens` + 1 WHERE userId = ?")->execute([$time, $user->userId]);
header("Location: /אזור-משחק/בסיס");
}
}
}
################################ Separator ###########################
if (isset($_GET['daily'])) {
if ($user->checkHours($user->hourEvent)) {
$time = date("Y-m-d H:i:s", time());
$mysqli->prepare("UPDATE `users` SET `hourEvent` = ?, `userMana` = `userMana` + 10, `userCitizens` = `userCitizens` + 1 WHERE userId = ?")->execute([$time, $user->userId]);
header("Location: /אזור-משחק/בסיס");
}
if ($user->premium > 0 && $user->checkHours($user->hourEvent)) {
$time = date("Y-m-d H:i:s", time());
$mysqli->prepare("UPDATE `users` SET `hourEvent` = ?, `userMana` = `userMana` + 11, `userCitizens` = `userCitizens` + 1 WHERE userId = ?")->execute([$time, $user->userId]);
header("Location: /אזור-משחק/בסיס");
}
}
The first one will not work properly.
The else if condition will only be tested when $user->checkHours($user->hourEvent) returns false. But the nested if block is only executed when this same expression returns true. Unless the result of this changes between these two tests, the second one can never succeed. So premium users will never get a bonus.
The second version will give two bonuses to premium users. They'll first get the same bonus of 10 that everyone gets, then they'll get an additional 11 bonus because they're premium users. I think it would be clearer to write it like this:
$normal_bonus = 10;
$premium_bonus = 21;
if (isset($_GET['daily']) && $user->checkHours($user->hourEvent)) {
$bonus = $user->premium ? $premium_bonus : $normal_bonus;
$mysqli->prepare("UPDATE `users` SET `hourEvent` = ?, `userMana` = `userMana` + ?, `userCitizens` = `userCitizens` + 1 WHERE userId = ?")->execute([$time, $bonus, $user->userId]);
header("Location: /אזור-משחק/בסיס");
}
This removes lots of duplicate code between the cases, and only executes one query to add to userMana.
Related
I have a booking form where the users can book a certain facility, on a certain date and between two times. I have come to a point where my PHP code will prevent the user from booking the same pitch on the same date and from the same start or end time.
However the user can go between a time and book the same facility on the same date as another user. e.g. if one user has booked from 9 to 12 on the 1st May another user can come in and book the 10 or 11 slot.
Is there any way of preventing the user from booking a time between the time another user has already booked???
<?php
include "config.php";
//Booking point
if(isset($_POST['booking']))
{
//get values for variables
$pitchID = $_POST['pitchID'];
$start_date = $_POST['start_date'];
$start_hour = $_POST['start_hour'];
$end_hour = $_POST['end_hour'];
$booking_age = $_POST['booking_age'];
$pitch_size = $_POST['pitch_size'];
$light_tokens = $_POST['light_tokens'];
/* $q = $db->prepare("SELECT * FROM booking WHERE start_date = ?, start_hour = ?, end_hour=?, pitchID=?");
$query = $q->execute(array($start_date, $start_hour, $end_hour, $pitchID));
$count = $q->rowCount(); */
$q = $db->prepare("SELECT * FROM booking WHERE start_date = ? AND start_hour = ? AND pitchID = ?");
$query = $q->execute(array($start_date, $start_hour, $pitchID));
$count = $q->rowCount();
if($count == 0){
$query = $db->prepare("INSERT INTO booking SET pitchID = ?, start_date = ?, start_hour = ?, end_hour = ?, booking_age = ?, pitch_size = ?, light_tokens = ?, userID='$userID'");
$query = $query->execute(array($pitchID,$start_date,$start_hour,$end_hour,$booking_age,$pitch_size,$light_tokens));
if($query){
echo "Your booking has been made";
header("Location:home2_template.html");
return;
} else {
echo "Fail";
} //else fail
} else {
echo "This booking already exists";
} //else count
} //if booking
?>
If you only can reserve one day, try this
select * from booking
where (start_date='new_date'
and (
(start_hour <='new_start_hour' and end_hour>='new_start_hour')
or
(start_hour<= 'new_end_hour' and end_hour>='new_end_hour')
)
) and pitchID = ?
I have a booking form set up where the user can book different facilities but I can't get my head around how to prevent the user from booking a date that is before today or how to prevent them from double booking.
I did try and work with a datepicker but I am using the HTML5 standard datepicker and also using Twitter Bootstrap but I don't know how to restrict dates from the user.
Below includes my full form and the PHP included in it.
<?php
include "config.php";
//Booking point
if(isset($_POST['booking']))
{
//get values for variables
$pitchID = $_POST['pitchID'];
$start_date = $_POST['start_date'];
$start_hour = $_POST['start_hour'];
$end_hour = $_POST['end_hour'];
$booking_age = $_POST['booking_age'];
$pitch_size = $_POST['pitch_size'];
$light_tokens = $_POST['light_tokens'];
$q = $db->prepare("INSERT INTO booking SET pitchID = ?, start_date = ?, start_hour = ?, end_hour = ?, booking_age = ?, pitch_size = ?, light_tokens = ?");
$query = $q->execute(array($pitchID,$start_date,$start_hour,$end_hour,$booking_age,$pitch_size,$light_tokens));
$count = $q->rowCount();
if($count == 0)
{
echo "Your booking has been made";
header("Location:home2_template.html");
return;
}else {
echo "That booking already exists";
}
}
?>
To check if a date is before a certain date you could convert your dates to timestamp and check if one is lower than the other
$start_date = $_POST['start_date'];
if( strtotime($start_date) < time()) {
// Date is before today
} else {
// Date is after today
}
Hello I am trying to implement after they pay it checks how much they paid and inputs different variables in sql database with if methods.
The problem:
The problem is all the if methods return to this first option(inserts paid=1, and 30days) not sure what the problem is, is my if method broken?what's wrong please explain! thanks!
if(number_format($amount, 2) == 8.00)
{
$mysqli = new mysqli(******);
$stmt = $mysqli->prepare("UPDATE `as_users` SET paid='1', reg_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) WHERE username = ?");
$stmt->bind_param('s', $username);
$stmt->execute();
} elseif (number_format($amount, 2) == 10.00)
{
$mysqli = new mysqli(******);
$stmt = $mysqli->prepare("UPDATE `as_users` SET paid='2', reg_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) WHERE username = ?");
$stmt->bind_param('s', $username);
$stmt->execute();
} elseif (number_format($amount, 2) == 100.00)
{
$mysqli = new mysqli(******);
$stmt = $mysqli->prepare("UPDATE `as_users` SET paid='2', reg_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL 365 DAY) WHERE username = ?");
$stmt->bind_param('s', $username);
$stmt->execute();
}
Edit: I tried with $amount and also tried using the names of the array from dropdown menu I have
array("Basic Package-Monthly", "8.00", "Month", "1", "0", "0"),
array("Premium Package-Monthly", "10.00", "Month", "1", "0", "0"),
array("Premium Package-Annually", "100.00", "Year", "1", "0", "0"),
Why worry about the conditional statement, let the switch handle it. Leverage your prepared statement fully.
$mysqli = new mysqli('******');
$paid = false;
$recurring = false;
switch(number_format($amount, 2)):
case '8.00':
$paid = 1;
$recurring = 30;
break;
case '10.00':
$paid = 2;
$recurring = 30;
break;
case '100.00':
$paid = 2;
$recurring = 365;
break;
endswitch;
$stmt = $mysqli->prepare("UPDATE `as_users` SET paid=?, reg_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL ? DAY) WHERE username = ?");
$stmt->bind_param('dis', $paid, $recurring, $username);
$stmt->execute();
try to use
number_format($amount, 2, '.', '') instead of number_format($amount, 2) for returning english notation without thousands separator.
i.e
english notation without thousands separator
$number = 1234.56;
$english_format_number = number_format($number, 2, '.', '');
1234.56
and to compare two float values use BC math function
$b = 10.00;
$a = number_format($amount, 2, '.', '');
bccomp($a, $b)==0 // returns true if both values are 10.00
I have the following code: its purpose is very clear => at login update db with +1 logins for totallogins and record the time of the very last login. The only downside is that it wont work.
<?php
date_default_timezone_set('Europe/Amsterdam');
if (isset($_POST['formsubmitted'])) {
$Timesloggedin = "SELECT * FROM members.Timesloggedin WHERE Email='$email'";
$time = date("Y-m-d H:i:s");
$query1 = "UPDATE members SET Timesloggedin = $Timesloggedin + 1, Lastloggedin = $time WHERE Email ='$email'";
$result_insert_loggedins = mysql_query($query1);
if (!$result_insert_loggedins) {
echo 'Query failed';
}
if (mysql_affected_rows($dbc) == 1)
{
//If the Insert Query was successfull.
}
?>
$query1 = "UPDATE members SET Timesloggedin = Timesloggedin + 1, Lastloggedin = '$time' WHERE Email ='$email'";
Remember that $time is a string and needs the quotes. Also, $Timesloggedin would be an object (if you actually ran the query which you don't) so just remove the $ and it will just increment the field.
Also, you don't even need the first query. Nor do you need the date calculation. Just use mysql's NOW()...
$query1 = "UPDATE members SET Timesloggedin = Timesloggedin + 1, Lastloggedin = NOW() WHERE Email ='$email'";
you can just use
Timesloggedin = Timesloggedin + 1
Instead of the variable.
i have the following problem with a login script. at the moment i refresh my site and would like to change mysql into mysqli. i have a working code, that works with mysql like it should to. now i get in trouble with changing that into mysqli, which doesn't work.
here is the original mysql code:
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
session_register('id');
$_SESSION['id'] = $id;
mysql_query("UPDATE tableA SET time=now(), x4=x4 + 1 WHERE id='$id'");
mysql_query("DELETE FROM tableB WHERE (NOW() - INTERVAL 1 DAY) > Date AND ID='$id'");
$result = mysql_query("SELECT COUNT(*) AS val FROM tableB WHERE ID='$id'");
$count = mysql_fetch_assoc($result);
var_dump($count);
if ($count [val] <xy){
mysql_query("INSERT INTO tableB (Date, ID) VALUES (now(),'$id') ");
mysql_query("UPDATE tableA SET x7=x7 + 1 WHERE id='$id'");
and here is the mysqli version, that wont work and i dont know why:
$time = gmdate("M d Y H:i:s", time());
$id = '".$row["id"]."';
$get_id = "SELECT id FROM tableA WHERE x1='".$x1."' AND x2='".$x2."'";
$result = mysqli_query($db, $get_id);
if ($result === false) {
printf("Errormessage 1");
exit();
}
$row = $result->fetch_array(MYSQLI_ASSOC);
$update = "UPDATE tableA SET time=now(), x4=x4 + 1 WHERE id='".$row["id"]."'";
$result2 = mysqli_query($db, $update);
if ($result2 === false) {
printf("Errormessage 2");
exit();
}
$reset = "DELETE FROM tableB WHERE (NOW() - INTERVAL 1 DAY) > Date AND ID='".$row["id"]."'";
$result3 = mysqli_query($db, $reset);
if ($result3 === false) {
printf("Errormessage 4");
exit();
}
$count = "SELECT COUNT(*) AS val FROM tableB WHERE ID='".$row["id"]."'";
$result4 = mysqli_query($db, $count);
if ($result4 === false) {
printf("Errormessage 5");
exit();
}
$sum = $result4->fetch_assoc($count);
var_dump($sum);
if ($count [val] <xy){
$insert = "INSERT INTO TableB (Date, ID) VALUES(?,?) ";
if($query = $db->prepare($insert)){
$query->bind_param('ss', $time, $id);
$query->execute();
$update_x = "UPDATE tableA SET x7=x7 + 1 WHERE id='".$row["id"]."'";
$result5 = mysqli_query($db, $update_x);
if ($result5 === false) {
printf("Errormessage 5");
exit();
You haven't mentioned the actual error but it seems that problem occurs from here:
$row = $result->fetch_array(MYSQLI_ASSOC);
you didnot use a loop here like your mysql version of code.
You are attempting to insert $time into what appears to be a DATETIME column, based on your old mysql version, but it is improperly formatted.
$time = gmdate("M d Y H:i:s", time());
Based on your use of NOW() in the old code, we assume TableB.Date to be a DATETIME type:
mysql_query("INSERT INTO tableB (Date, ID) VALUES (now(),'$id') ");
So in your new code, since you don't use NOW() for the TableB insert, you should be creating $time as YYYY-MM-DD:
// Should be YYYY-MM-DDD H:i:s for MySQL
$time = gmdate("Y-m-d H:i:s", time());
// It gets inserted into TableB here
$insert = "INSERT INTO TableB (Date, ID) VALUES(?,?) ";
if($query = $db->prepare($insert)){
$query->bind_param('ss', $time, $id);
$query->execute();
Or, just use MySQL's NOW() in the new code unless you have a reason to specify the time in PHP code:
$insert = "INSERT INTO TableB (Date, ID) VALUES(NOW() ,?) ";
if($query = $db->prepare($insert)){
$query->bind_param('s', $id);
$query->execute();