-2

I'm using a time method: $time= date('h:i:s');

What I want is to put this time into database in mySQL, I used: $query = mysqli_query($conn, "INSERT INTO tab ('ltime') VALUES ($time)"); but it's not working

Where tab is a table and a ltime is a Column with time Type.

What am I doing wrong?

Regards

5 Answers5

1

Your query will goes like this.

$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ('$time')");
Subhash Shipu
  • 343
  • 1
  • 4
  • 15
0

Your query should be like this

$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ($time)");
pravindot17
  • 1,199
  • 1
  • 15
  • 32
0

Columns and tables should have backticks and not single quotes

INSERT INTO `tab` (`ltime`) VALUES ($time)
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46
0
$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ($time)");

Don't use '' when assigning the column name

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
0

The first way specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)VALUES (value1, value2, value3, ...);

If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query

INSERT INTO table_name VALUES (value1, value2, value3, ...);

In your case:

$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ($time)");

there is no need to single quote with column name:

Reference: https://www.w3schools.com/sql/sql_insert.asp

Ahmad Hassan
  • 371
  • 4
  • 22