I want to set a variable that will auto increase always by 1 in form submit and if exist to do +1 if already ia in db.
For example :
if the form is submitted almost the same time by two different users .i have tried with primary key Id but some times it gets increased by two.
My code for inserting is
<?php
require('db.php');
date_default_timezone_set('Europe/Athens');
include("auth.php"); //include auth.php file on all secure pages
mysql_query("SET NAMES 'utf8'");
$status = "";
$signintime = date("Y-m-d H:i:s");
$customid =$_REQUEST['customid'];
$seat =$_REQUEST['seat'];
$matchtype= $_REQUEST['matchtype'];
$ticketprice=$_REQUEST['ticketprice'];
$barcode= $_REQUEST['barcode'];
$expdate= $_REQUEST['expdate'];
$expdate = date('Y-m-d H:i:s', time()+36000);
$submittedby = $_SESSION["username"];
$ins_query="insert into kozani(`customid`,`signintime`,`seat`,`matchtype`,`ticketprice`,`barcode`,`expdate`,`submittedby`)values('$customid','$signintime','$seat','$matchtype','$ticketprice','$barcode','$expdate','$submittedby')
ON DUPLICATE KEY UPDATE `customid` = '$customid',`signintime`='$signintime',`seat`='$seat',`matchtype`='$matchtype',`ticketprice`='$ticketprice',`expdate`='$expdate',`barcode`= IFNULL (`barcode`,'$barcode')";
Where customid is the value of vouvher
Thank you.
you can use update , you need to adapt this code (php)
if( question )
UPDATE `table` SET `parameter` = `parameter` +1 WHERE `parameter` =".$value.";
else
UPDATE `table` SET `parameter` = `parameter` +2 WHERE `parameter` =".$value.";
Related
I am trying to update my database using php however my update clause is not working.
This is the code:
<?php
if (isset($_POST['submit']))
{
date_default_timezone_set('Australia/Sydney');
$date = date("Y-m-d h:i:s");
$sqlUpdate = " UPDATE booking
SET checkedin = '1', checkin_datetime = '$date'
WHERE id = '$flightID'";
$result3 = mysqli_query($dbConn, $sqlUpdate) or trigger_error("Query Failed! SQL: $sqlUpdate - Error: ".mysqli_error($dbConn), E_USER_ERROR);
}
?>
In my database the flightID is an auto increment integer and the checkin is a tiny int.
I believe that it could be treating my variable as a string and not a integer which is why it is not updating in my database.
I have tried creating a variable to to hold the value of 1 and replace it with the hard coded value in the SQL which did not change anything.
Any ideas what the problem is and how to fix it?
here is a photo for my booking table
please note in my sql the 'flightID' variable is actually referring to the id column in the booking table
I have a MySQL database with a clients table and a person(used for testing) table. My HTML, PHP, and scripts work as expected in the person table. When using the same coding in my clients table I get the following error "1292: Incorrect date value: '' for column 'sale_date' at row 1.
My HTML code is as follows:
<label>Sale Date:  <input id="cSaleDate" class="cSale" type="date" name="cSaleDate"></label>
My PHP code:
$sale_date = mysqli_real_escape_string($db,$_POST['cSaleDate']);
if (isset($_POST['cSaleDate']))
{
include_once('client.php');
$saleDate = date('0000-00-00', strtotime($sale_date));
if (!empty($_POST['cSaleDate']))
{
$saleDate = date('Y-m-d', strtotime($sale_date));
}
}
$fields = 'fips_code, size_code, client_name, client_state, sale_date';
$values = "'$fips_code', '$size_code', '$client_name', '$client_state', '$saleDate'";
$sql = "INSERT INTO clients ($fields) VALUES ($values)";
The column for the sale date is set as:
Name: sale_date
Datatype: DATE
Default: NULL
As mentioned before, this code does work with dates in the "person" table, but not with the "clients" table. I have the date column setup the same on both tables. Any help or ideas on why this works for one table but not the other would be greatly appreciated.
Your code largely depends on $_POST['cSaleDate'] and from the snippet you've provided if you don't actually get that value then $saleDate would be undefined. That is my best guess at what the problem may be.
Here is a possible way to rewrite that snippet:
// It seems you want this as your default if there is no $_POST['cSaleDate']?
$saleDate = '0000-00-00';
// Now check if you should overwrite this default
if (isset($_POST['cSaleDate'])) {
include_once('client.php');
if ( ! empty($_POST['cSaleDate'])) {
$saleDate = date('Y-m-d', strtotime($_POST['cSaleDate']));
}
}
// Now do your escape and db insert
$saleDate = mysqli_real_escape_string($db, $saleDate);
$fields = 'fips_code, size_code, client_name, client_state, sale_date';
$values = "'$fips_code', '$size_code', '$client_name', '$client_state', '$saleDate'";
$sql = "INSERT INTO clients ($fields) VALUES ($values)";
Thanks all for the help. I ended up using a ternary statement in the insert, which allowed me to insert 'NULL' into the date field in the database if the input is left blank.
$sql = "INSERT INTO table (SaleDate) VALUES (" . ($saleDate == '' ? 'NULL' : "'$saleDate'") . ")"
i want to create a timestamp by which i can know which post is modified when and all. in mysql databse, i made a coloumn called lastmodified, with type as timestamp. now, when i am updating the data in db, i want to update the current timestamp in last modified. how to do so? also could anyone please tell me, if any function exits for comparing these timestamps.
$now = time();
$query = "update storydb set lastmodified = '$now' where story_id = '$story_id'";
mysqli_query($con, $query);
time() returns UNIX Timetamp in integer format e.g. 1223485636.
You want it in 2014-12-10 02:02:36
Use MySQL now() function instead of $now
$query = "update storydb set lastmodified = now() where story_id = '$story_id'";
now() is a MySQL function that returns current Time Stamp (including date).
No, its not unix timestamp that should be used in there, just a normal NOW() should suffice:
$query = "UPDATE storydb SET lastmodified = NOW() WHERE story_id = ?";
$update = $con->prepare($query);
$update->bind_param('s', $story_id);
$update->execute();
To insert current unix timestamp in data base
$time = time();
$qry = 'update db_name' set column_name = $time where condition;
mysql_query($qry);
i have a column in my database for the date a new record was added, it's set with the following code:
$dateset = date("Y-m-d");
$q = "INSERT INTO appts (date_set) VALUES ('$dateset')";
this is working about 80% of the time, is there any reason that randomly it will add the date as "0000-00-00"?
You should check type of field 'date_set'.
if type is DATE you have to use:
$dateset = date("Y-m-d");
if type is DATETIME you have to use:
$dateset = date("Y-m-d H:i:s");
//It is correct, but database push after time 00:00:00 (but it depends on the settings of the database)
//$dateset = date("Y-m-d");
if type is TIMESTAMP you have to use:
$dateset = time();
Try use sql:
$q = "INSERT INTO appts (date_set) VALUES (NOW())";
I think it would the best decision to use
$q = "INSERT INTO appts (date_set) VALUES (NOW())";
is there any reason that randomly it will add the date as "0000-00-00"?
No.
Either $dateset populated some other way or value got updated to 0's later on.
I have a query that I want to update a column with the current date time. The column I want to update is of the type datetime.
How can I get it and set it?
else{ //If the last update is NULL, It must be players first page load. So set datetime equal to NOW
$query = "UPDATE `stats` SET `last_ap_update` = WHERE `member_id` = {$_SESSION['SESS_MEMBER_ID']}";
$queryresult = mysql_query($insertqry);
}
Using NOW() in the query would provide this.
else{ //If the last update is NULL, It must be players first page load. So set datetime equal to NOW
$query = "UPDATE `stats` SET `last_ap_update` = NOW() WHERE `member_id` = {$_SESSION['SESS_MEMBER_ID']}";
$queryresult = mysql_query($insertqry);
}
But if this gets updated anytime the table updates, I would suggest changing the column to TIMESTAMP and this will automatically update to the current time for you anytime that record changes.
else{ //If the last update is NULL, It must be players first page load. So set datetime equal to NOW
$query = "UPDATE `stats` SET `last_ap_update` = '".gmdate("Y-m-d H:i:s")."' WHERE `member_id` = {$_SESSION['SESS_MEMBER_ID']}";
$queryresult = mysql_query($insertqry);
}
You can use any format you want instead of gmdate("Y-m-d H:i:s")
You can use the mysql function NOW() for this, or you can pase the $_SERVER['REQUEST_TIME'] in there so the query gets cached by mysql.