Time format does not insert correctly into database - php

Code below adds date in correct format but hours/minutes/seconds are inserted as 00:00:00.
$timestamp[] = 'date("2017-12-31 21:01:50")';
$i because I have this inside a a loop.
$sql = "INSERT INTO posts (post_id, username, content, timestamp, likes)
VALUES ('$post_id[$i]', '$username[$i]', '$content[$i]', $timestamp[$i],
'$likes[$i]')";
Result:

MySQL's date function returns a date without a time portion, which is why it's getting truncated to midnight.
You should be able to ignore the call to date entirely, and just pass the raw string - it's already in a format that MySQL will understand:
$timestamp[] = '2017-12-31 21:01:50';
$sql = "INSERT INTO posts (post_id, username, content, timestamp, likes)
VALUES ('$post_id[$i]', '$username[$i]', '$content[$i]', '$timestamp[$i]', '$likes[$i]')";
(Note the added quotes around the $timestamp variable)
You should also look into using prepared statements, rather than building up your SQL string manually. It'd be both a security and readability benefit.

Related

How to get hours and minutes in this code mysql php

sql_exec (($sql = "insert into posts (user_id, body, picture_url, stamp) values ($userid, '". sql_escape ($body). "','$picture_url', minute(), hour())"));
sql_exec function
function sql_exec ($sql)
{
global $my_conn;
$result = mysqli_query($my_conn, $sql);
return $result;
}
My problem is with the minute() and hour() they are not working
Are you getting a column count error? You appear to be trying to insert 5 values in to 4 columns.
If you are looking to store a timestamp against each post you might find it easier, and a lot more practical for querying, to define the stamp column as
`stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
This will get you both date and time which will allow you sort the posts properly and the column default will mean you don't even have to supply a value for it, mysql will do it for you.
As far as my knowledge goes, PHP doesn't have any minute() nor hour() functions. What I do know, however, is that you can get both of them using PHP's date() function.
To get the current hour and minutes, you can use:
$hour = date('H');
$minute = date('i');
Please let me know if this helped you :)

How to retrieve date value from POST method?

I have the following date input box in an HTML code:
<input type="date" name="DOB" size="20">
The form data are passed to a different PHP file using the POST method. And there the date is saved to a variable.
$DOB=$_POST['DOB'];
When I echo this variable it prints the date value saved in it. However if I use this variable in a query, the value won't be read. The query does not return any values.
This is my query:
"SELECT site_no, name,NIC,gender,address,telephone FROM employee WHERE emp_no=$emp_no AND DOB=$DOB"
Try this
use single quotes with date(assumption you got date value in $DOB variable )
"SELECT site_no, name,NIC,gender,address,telephone FROM employee WHERE emp_no=$emp_no AND DOB='$DOB'"
EDIT
MySQL expects DATE and DATETIME literal values to be single-quoted as strings like '2001-01-01 00:00:00'
You need to enclose variables in single quotes. Use the code below
"SELECT site_no, name,NIC,gender,address,telephone FROM employee WHERE emp_no='$emp_no' AND DOB='$DOB'"
Hope this helps you
For visibility it's good practice to add quotes to your SQL. It's important to know what types you're working with and to escape the values, especially with something like this.
Your query with quotes and indentation for visibility:
SELECT
`site_no`, `name`, `NIC`, `gender`, `address`, `telephone`
FROM
`employee`
WHERE
`emp_no` = '$emp_no'
AND
`DOB` = '$DOB'
Best practice to save dates is either using DATE/DATETIME or timestamps. You'll want to verify the DOB format and input, strip unnecessary characters etc. Look up real_escape_string, time and date for PHP
Use single quotes for $DOB
"SELECT site_no, name,NIC,gender,address,telephone FROM employee WHERE emp_no=$emp_no AND DOB='$DOB'"

Using p4a, how do i insert a row into a MySQL database?

I'm using the p4a application framework, i need to insert rows into my database via fields that i have made previously, when the user presses submit, the database should update and thus other new rows should be able to to made etc.
I'm struggling to find how to input the data into the database, i can easily do it through putting the values into the sql statement but this is completely alien to me,
The code is:
public function submit()
{
$location = $this->location->getNewValue();
$date = $this->date->getNewValue();
$merono = $this->merono->getNewValue();
$sql = $db->query("INSERT INTO 'meetingrooms'(location, date, merono)
VALUES
($location, $date, $merono)");
p4a_db::singleton()->newRow($sql, array($location));
$this->load();
location, date and merono are all set in the fields i have created before this function and it should work as i have previously done the same for a login page, so i know the first section should be getting the variables. and as i have accessed the db previously i know that it is connecting, so it must be to do with the MySQL statement.
Thanks,
Steve.
on your query, i found out that you are enclosing the table name with single quote, if you want to escape tableName or columnName use backtick instead,
INSERT INTO `meetingrooms`(location, date, merono)
VALUES ($location, $date, $merono)
but since your tableName is not a reserved word or contains any invalid characters, you can get rid of the backtick.
If you are inserting values on the table which are not numeric, wrap it with single quotes,
INSERT INTO meetingrooms (location, date, merono)
VALUES ('$location', '$date', '$merono')
I finally managed to figure it out (even though this question was only active for a few mins XD)
the SQL statement was wrong for a start (Thanks to John Woo for the help (y)) now the statement goes:
query("INSERT INTO meetingrooms(location, date, merono)
VALUES
('$location', '$date', '$merono')");
this successful statement allows for the variables placed into $location $date and $merono to be inserted into the table plus the extra addition to the start of the statement
goes as:
p4a_db::singleton()->
this calls the P4A database extension which in this pop-up class i have made, is not accessible,
so the full function now goes:-
public function submit()
{
$location = $this->AreaName->getNewValue();
$date = $this->date->getNewValue();
$merono = $this->merono->getNewValue();
p4a_db::singleton()->query("INSERT INTO meetingrooms(location, date, merono)
VALUES
('$location', '$date', '$merono')");
Thanks for the help,
Steve.

Compare string datatype to a date datatype PHP MYSQL

In my MySQL database, I have this Time data type as one of my values: 06:00:00. I have the following query that checks the time as one of the conditions that has to be satisfied
$time = "06:00:00";
$getdetails=SELECT First_Name, Last_Name, EMAIL
FROM parents
WHERE Email_Receive_Time = $time;
$results=mysql_query($getdetails);
However I do not get any results. On further research I have seen that it is because I am comparing a STRING type value ($time) to a TIME type value (value in my database). Is there a way i can compare the two without changing my database structure to a varchar? All help will be appreciated.
MySQL is perfectly capable of comparing a string to a TIME value. You just need to have the proper query syntax. In your case, you need to quote the comparison value:
$time = "06:00:00";
$getdetails = "SELECT First_Name, Last_Name, EMAIL
FROM parents
WHERE Email_Receive_Time = '$time'";
$results=mysql_query($getdetails);
And if it is user-supplied, well you should escape it.
You are wrong.
Your mistake is much simpler, it has nothing to do with data formats, but with query format.
Ask yourself what does mean 06:00:00 in terms of SQL syntax.
Btw, running query this way will help you A LOT:
$results=mysql_query($getdetails) or trigger_error(mysql_error()." in ".$getdetails);
always run all your queries this way and get in touch with every error occurred
This will work when comparing against TIME type of field:
To compare against DATETIME or TIMESTAMP, I'd suggest running the $time variable through strotime() first.
$time = "06:00:00";
$getdetails = "SELECT First_Name,
Last_Name,
EMAIL
FROM parents
WHERE Email_Receive_Time = '$time'";
$results = mysql_query($getdetails);
Try using STR_TO_DATE function, it should work for you.
Thanks
Ravi Mudaliar

Format Date field type

i have a csv file that i'm interest and the date format is 20-Nov-2010 how do I format it for insertion into mysql accepted format for a field with Date as its type?
date("Y-m-d", strtotime("20-Nov-2010"));
Andreas' answer is a bit terse - in PHP this will return a string which you can splice
into your query:
$d=date("Y-m-d", strtotime("20-Nov-2010"));
$qry="INSERT INTO sometable (adate) VALUES('$d')";
But if you omit the punctuation, you can ad it without the quotes:
$d=date("Ymd", strtotime("20-Nov-2010"));
$qry="INSERT INTO sometable (adate) VALUES($d)";
A drawback of this approach is that strtotime() tends to always return some sort of date value - and sometimes not what you expect.
However you could do the parsing in MySQL, which is a lot more strict:
$d='20-Nov-2010';
$qry="INSERT INTO sometable (adate) VALUES(STR_TO_DATE($d, '%d-%b-%Y'))";
But remember to check for errors when mysql tries to parse the date.

Categories