Why Php explode is echoing but can't post - php

i have exploded timestamp2w to date and time. I can post the date to the database but the time isn't posting, but if i echo it, it is showing the time. i want to post that time to the db.
$timestamp3=$_POST['timestamp'];
$timestamp3x=explode(" ",$timestamp3);
$timestamp=$timestamp3x[0];
$timestamp3=$timestamp3x[1];
$datetime=$_POST['$timestamp3'];
$transactiondate =date("Y-m-d");
$userid=$_SESSION['userid'];
$equipmentid = $_GET['id'];
echo $timestamp3;
$sql="Insert into tbl_booking(equipmentid,book_to,book_from,transactiondate,user_booked,datetime)VALUES('$equipmentid','$timestamp2','$timestamp','$transactiondate','$userid','$datetime')";

i think you have time value in $timestamp3 and you are sending $timestamp2 to mysql query so change your query like below
$sql="Insert into tbl_booking(equipmentid,book_to,book_from,transactiondate,user_booked,datetime)VALUES('$equipmentid','$timestamp3','$timestamp','$transactiondate','$userid','$datetime')";

you're echoing $timestamp3 variable, but in the query I can see $timestamp2!

Related

mysql timestamp output as formatted Date not working

I have a crazy phenomenon in my php script. I have defined a column as a timestamp in a mysql table. I fill it with the function:
date("Y-m-d H:i:s");
The data in the table then look like this: 2017-04-19 17:08:45
When I query this column with mysqli as a unix timestamp again:
SELECT UNIX_TIMESTAMP (timestamp) as timestampUnix FROM posts
Then binding the result using bind_result to the variable $timestampUnix.
I can echo the variable with
echo $timestampUnix;
and it outputs a correct timestamp like this: 1492614559
however if i do the following:
$timestampUnix2 = $timestampUnix;
echo $timestampUnix2;
there is simply no echo output... What is the reason?
I tried this because I actually want echo only the date in an other format with:
date('d.m.Y', $timestampUnix)
and it gave me 01.01.1970 and i wondered why the timestamp must be 0 but it isnt since when i directly echo it it gives me a correct one.
however when i do
Date('d.m.Y', 1492614559)
it gives me the correct date.. no clue what is going on there!
i know there are many other questions about mysql php Date output, but no one have this issue as i think i cannot do something with the variable i got from the query.
thanks in advance!
edit: i attach the complete code in question:
---the query that inputs the data in the db----
$timestamp = date("Y-m-d H:i:s");
mysqli_query($mysqli,"INSERT INTO posts (timestamp)
VALUES ('$timestamp')");
---the query that fetches the data----
$results = $mysqli->prepare("SELECT UNIX_TIMESTAMP(timestamp) as timestampUnix FROM posts");
$results->execute(); //Execute prepared Query
$results->bind_result($timestampUnix); //bind variables to prepared statement
$postdate = date('d.m.Y',$timestampUnix)
echo $postdate;

PHP : comparing fetched date with today

i'm saving time for first login ,now when user logs in i enter time using NOW() function, that saves time in this format (data type is DATETIME.
2015-12-24 15:47:30
Now logic is like every login is first login so i've to check if there already exists an entry for today to check that i fetch time explode it and get time like this
$logintime= mysqli_query($connection,"SELECT loggedin from employees");
$loggedin_time= mysqli_fetch_assoc($logintime);
$Date = $loggedin_time['loggedin'];
$loggedin_time_converted= explode(" ",$yourDate) ;
$ConvertedDate = $loggedin_time_converted[0];
last line returns 2015-12-24 now i've date
$today= time();
$DateToday= date("Y-m-d",$today);
$DateToday also returns me same format and same date now i need your help me to compare these dates , if they are equel i dont uopdate database if they are not i will , Pleas help me how do i compare these values
You can do the test in MySQL
$result = mysqli_query($connection, "SELECT DATE(loggedin) = CURDATE() AS logged_in_today FROM employees");
$row = mysqli_fetch_assoc($result);
if (!$row['logged_in_today']) {
// code to update database
}
Wow, you've done all the hard stuff to get the problem to the point of being a simple comparison of 2 strings. This is all you need to do to get over the finish line ...
if ($ConvertedDate !== $DateToday) {
// update the database
}
You can use Php Built In function "Date Difference."
Code Seems Like As Follow:-
$today= time();
$DateToday= date("Y-m-d",$today);
$diff = date_diff($today,$DateToday);
echo "$diff days";
This will return values something like +12 days or something else.

how to search record based on date submitted in form

I am trying to search record in mysql based on date which is submitted hidden field in a form, but I haven't got any results from DB with given date, I know there is a record with the same date.
I have a following code:
$id = $_POST['pid']; //hidden field in form with sample value 1
$d = $_POST['d']; // hidden field in form with sample value 2014-12-17 18:25:58
$ch = $con->query('SELECT * FROM '.PATDMBILL.' WHERE pid='.$id.' AND pfid=0 AND date="'.$d.'"');
In DB date column is current time-stamp.
When I assign $d to this value '2014-12-17 18:25:58' it works but from submitting form it will not work.
So Where I am making a mistake ?
I hope I have cleared my situation properly.
Rewrite the code:
$id = $_POST['pid'];
$d = $_POST['d'];
$query = 'SELECT * FROM '.PATDMBILL.' WHERE pid='.$id.' AND pfid=0 AND date="'.$d.'"';
echo $query;
$ch = $con->query($query);
You will then be able to see if the query is actually formatted properly and fix any obvious issues. Unfortunately there isn't enough information to say what the solution is, but you should be able to copy and paste the echoed 'query' and paste it into the tool you use to build queries (eg phpmyadmin or sequel pro)
The problem may be occurring because of the time 18:25:58 . You can change the database field from timestamp to date and pass only date from $d using strtotime(date("Y-m-d", $d));

Comparison between a timestamp in MYSQL table with a PHP variable NOT Working - PHP MYSQL

I'm trying to filter out repeated values entering into a MySQL table, by comparing the input PHP variable with the timestamp of an entry already present in the table and only if they don't match, the input PHP variable is entered into the table.
$user1_date = mysql_real_escape_string($user1_date); // the date variable
$user1_temp1 = mysql_real_escape_string($user1_temp1);
$user1_temp2 = mysql_real_escape_string($user1_temp2);
$user1_temp3 = mysql_real_escape_string($user1_temp3);
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date)); //Typecasting PHP variable into timestamp
$sql_check = "SELECT * FROM user_details WHERE temp_date ='$user1_date'";
$result_check = mysql_query($sql_check);
$num_rows_check = mysql_num_rows($result_check);
if ($num_rows_check == 0) // To check if there is no entry in the table with the same date and time as input PHP variable
{
$sql_insert = "INSERT INTO data_hour (user_id, temp1, temp_date, temp2, temp3)
VALUES (1,'$user1_temp1', '$user1_date', '$user1_temp2', '$user1_temp3')";
$result_insert = mysql_query($sql_insert);
}
temp_date is a column in the table of type timestamp. Even when the $user1_date is the same as the temp_date(timestamp) column for one of the entries in the table, it considers it as not equal and is inserting it into the table and hence I'm getting repeated values. I'm guessing the WHERE temp_date = '$user1_date'is not working properly. Some troubleshooting that I have done included
Changing '$user1_date' to just $user1_date in the WHERE
statement
Changing the WHERE clause as follows WHERE temp_date = (date)'$user1_date'
It will be great if somebody can help me out with this!
A nice easy solution would be giving temp_date a UNIQUE INDEX in your Mysql Table, as that would not allow the same value to be inserted twice. This would also make your operations more efficient, as you wouldn't have to do SELECT * every time you want to insert something.
However, for what you're doing, I think I see your problem; there are some quirks in your logic so I'll try to dispel them here. (Hopefully?) this will make your program cleaner and you'll be able to pinpoint the error, if not eliminate it altogether.
Examining this piece of code:
// $user1_date doesn't have a value here! //
$user1_date = mysql_real_escape_string($user1_date);
...
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date));
Error 1 - You escape the string before ever setting a value.
What you are doing is that you are using mysql_real_escape_string() before $user1_date is ever defined.
Correction:
// Getting better, but not done. //
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date));
...
$user1_date = mysql_real_escape_string($user1_date);
Error 2 - You do not give the date() function appropriate parameters
The date() function in PHP expects a timestamp, which is just an int. You can easily get the time with time(), so that should rectify your problem
Correction:
// You use strtotime($user1_date), but you should use time() //
$user1_date = date("Y-m-d H:i:s", time());
...
$user1_date = mysql_real_escape_string($user1_date);
These are small mistakes, but they can be deadly. Like I said, you should assign temp_date to a UNIQUE INDEX in your MySQL table, but make sure to correct these errors listed as well.
Let me know how it goes!

Inserting a date to database

I am having a form with two textboxes where i am displaying the month calender like in any other online forms to select the date. The user selects the date. I want the user to select a date which is equal to or after the system date like tomorrow. I am comparing the dates and then trying to insert it into the database with two columns opening_date and closing dates. I don't even want the textbox to be left empty. I have the below code but isn't working out.
<?php
mysql_select_db("trials");
if(isset($_POST['next']))
{
$odate=date("Y-m-d");//system date
echo $odate;
if(($_POST['date1']>$odate)||($_POST['date1']==$odate)&&($_POST['date1']!=""))
{
$s=mysql_query("insert into registration_date (opening_date, closing_date) values('$_POST[date1]','$_POST[date2]')");
echo $s;
if(mysql_query($s))
{
echo "successful";
}
else echo "error".mysql_error();
}
else
echo "Enter A Valid Date";
}
?>
You're also using mysql_query twice once on the set of $s and then your doing mysql_query($s) later on which is in effect doing mysql_query(mysql_query("sql"))
$s=mysql_query("insert into registration_date (opening_date, closing_date) values('$_POST[date1]','$_POST[date2]')");
echo $s;
if(mysql_query($s))
Replace
if(($_POST['date1']>$odate)||($_POST['date1]==$odate)&&($_POST['date1']!=""))
with this
if(($_POST['date1']>$odate)||($_POST['date1']==$odate)&&($_POST['date1']!=""))
You have missed one quote for date1 and also try like
$open_date = $_POST['date1'];
$close_date = $_POST['date2'];
$s=mysql_query("INSERT INTO registration_date (opening_date, closing_date)
VALUES ('$open_date','$close_date')");
And try to avoid using mysql_* functions due to they are depricated.Instead use mysqli_* functions or PDO statements
get system date with function time() like this: $odate=time(); and trasform $_POST[date] in a number like this: strtotime($_post[date]). After this you can do this: $_POST[date]>$odate. in this way you will have two numbers to compare.

Categories