CONTROLLER
$date=date("Y-d-m");
$succes=$this->pl->insert_ad($brandID,$model,$price,$year,$picture,$resized,$fuels,$id_usera,$date);
Model
public function insert_ad($brand,$model,$price,$year,$picture,$resized,$fuels,$date)
{
$sql1="INSERT INTO `ads`(`model`, `year`, `price`, `photo`, `date`, `resized_photo`, `id_fuel`, `id_brand`)
VALUES ('".$model."','".$year."','".$price."','".$picture."','".$date."','".$resized."','".$fuels."','".$brand."')";
$this->db->query($sql1);
return $this->db->insert_id();
}
I print it with print_r($date) and it displays 2016-03-16, I really dont now why it wont insert like everything else.
Table structure
I just tried this minutes ago and it worked! ^_^
$d = date("Y-m-d");
$t = date("h:i:s");
$this->db->set($this->col_prefix.'customer_id', $_POST['customer_id']);
$this->db->set($this->col_prefix.'total_amount', $_POST['total_amount']);
$this->db->set('date', "'".$d."'");
$this->db->set('time_in', "'".$t."'");
$this->db->set('status', 1);
$this->db->insert('sln_transactions');
but haven't tried it yet in array :) hope this could help.
Please check data-type of 'date' column in database table. Might be possible issue.
Try these functions :
date('Y/m/d');
date('h-m-s');
date('Y/m/d:h-m-s');
I used these in particular to get the date and time. Might help you as well.
Make your query like this. Use DATE(".$date.")
$sql1="INSERT INTO `ads`(`model`, `year`, `price`, `photo`, `date`, `resized_photo`, `id_fuel`, `id_brand`)
VALUES ('".$model."','".$year."','".$price."','".$picture."',DATE(".$date."),'".$resized."','".$fuels."','".$brand."')";
Change your insert_ad function as below:
$sql1="INSERT INTO `ads`(`model`, `year`, `price`, `photo`,`resized_photo`, `id_fuel`, `id_brand`,`date`) VALUES ('".$model."','".$year."','".$price."','".$picture."','".$resized."','".$fuels."','".$brand."',NOW())";
$this->db->query($sql1);
return $this->db->insert_id();
Also remove the date parameter in controller function call and model function definition.
Please try this:
Controller:
$date=date("Y-d-m");
$succes=$this->pl->insert_ad(array(
'model'=>$model,
'id_brand'=>$brandID,
'price'=>$price,
'photo'=>$picture,
'date'=>$date,
'resized_photo'=>$resized,
'id_fuel'=>$id_usera,
'year'=>$year));
Model:
public function insert_ad($data) {
$this->db->insert('ads', $data);
return $this->db->insert_id();
}
Change From
$date=date("Y-d-m");
To
$date=date("Y-m-d");
And remvoe ' from integer type
public function insert_ad($brand,$model,$price,$year,$picture,$resized,$fuels,$date)
{
$sql1="INSERT INTO `ads` (`id_ad`, `model`, `year`, `price`, `photo`, `date`, `resized_photo`, `id_fuel`, `id_brand`) VALUES (NULL, '$model', '$year', '$price', '$picture', '".date('Y-m-d')."', '$resized', '$fuels', '$brand')";
$this->db->query($sql1);
return $this->db->insert_id();
}
**ANSWER **
*The correct formats of date * $date=date("Y-m-d"); *and * $date = date("Y-d-m"); as well
public function insert_ad($brand,$model,$price,$year,$picture,$resized,$fuels,$date)
{
$sql1="INSERT INTO `ads` (`id_ad`, `model`, `year`, `price`, `photo`, `date`, `resized_photo`, `id_fuel`, `id_brand`) VALUES (NULL, '$model', '$year', '$price', '$picture', '".date('Y-m-d')."', '$resized', '$fuels', '$brand')";
$this->db->query($sql1);
return $this->db->insert_id();
}
Try by using type varchar in your database structure for date.
CONTROLLER
$date=date("Y-m-d");
Database date format is Y-m-d not Y-d-m
this work perfect for me
in controller function
$this->model_invoice->invoice_create();
in model
$invoice_date = strtotime($this->input->post('invoice_date_time'));
$date=date("Y-m-d", $invoice_date);
$data = array
(
'invoice_date_time' => $date,
);
$this->db->insert('your_table_name', $data);
try this
in your Controller delete your variable $data
and also in your model delete the variable
model
public function insert_ad($brand,$model,$price,$year,$picture,$resized,$fuels)
{
$sql1="INSERT INTO `ads`(`model`, `year`, `price`, `photo`, `resized_photo`, `id_fuel`, `id_brand`)
VALUES ('".$model."','".$year."','".$price."','".$picture."','".$date."','".$resized."','".$fuels."','".$brand."')";
$this->db->query($sql1);
return $this->db->insert_id();
}
and also in your db change
type as DATETIME and
PREDETERMINATED as CURRENT_TIME
and run it
hope this works!
I have a date field with CURRENT_TIMESTAMP attribute. and I am trying to insert date in the database but the value storing as '0000-00-00 00:00:00'.
My code:
<?php
if (isset($_POST['submit'])) {
$amount = $_POST['amount'];
$sql = "insert into `od_request` (`username`, `amount`, `status`, `date`) values ('$login_session', '$amount', 'Request', 'NOW()')";
$retval = mysql_query($sql);
if($retval) {
$success= "OD Request Sucessfully Sent!";
} else {
$error = "Sorry! Make sure you have entered all the Fields Correctly.";
}
}
?>
Use this request :
INSERT INTO `od_request` (`username`, `amount`, `status`, `date`) VALUES('$login_session', '$amount', 'Request', NOW())
You don't need to put ' around NOW() because is an SQL function
Syntax to insert date is wrong:
use NOW() not 'NOW()'
$sql = "insert into `od_request` (`username`, `amount`, `status`, `date`) values ('$login_session', '$amount', 'Request', NOW())";
date("Y-m-d H:i:s") is the MySQL datetime format
eg.
$sql = "insert into `od_request` (`username`, `amount`, `status`, `date`) values ('$login_session', '$amount', 'Request', '".date("Y-m-d H:i:s")."')"
what about this?
$sql = "insert into 'od_request' ('username', 'amount', 'status', 'date') values ('$login_session', '$amount', 'Request', NOW())"
I want to write script function to insert multiple same query.
I want to insert multiple same records to table for example I want to insert it ten times but with different date for each insertion .
mysql_query("INSERT INTO `table` (name,age,address,phone,dat)
VALUES ('$name','$email','$address','$phone','$date')")
or die (mysql_ereror());
mysql_query("
INSERT INTO `table` (name,age,address,phone,dat)
VALUES
('$name','$email','$address','$phone','$date1'),
('$name','$email','$address','$phone','$date2'),
('$name','$email','$address','$phone','$date3'),
('$name','$email','$address','$phone','$date4'),
('$name','$email','$address','$phone','$dateN')
")
or die (mysql_ereror());
http://dev.mysql.com/doc/refman/5.6/en/insert.html
Try this code.
$date1 = date_create("2015-09-12");
$date2 = date_create("2015-09-20");
$interval = date_diff($date1, $date2);
$dateDifference = $interval->format('%a days');
$query = "INSERT INTO `table` (name,age,address,phone,dat) VALUES ";
for($i=0; $i<=$dateDifference;$i++)
{
$date = date_create("2015-09-12");
$newDate = date_add($date, date_interval_create_from_date_string($i.' days'));
$query .= "('$name', '$age', '$address', '$phone', '".date_format($newDate, 'd-m-Y')."'), ";
}
$query = substr($query, 0, -2);
mysql_query($query);
I'm trying to pass a date parameter to a php file so it could insert it into a MySQL table. This is how the date is being transferred:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss%20yyyy-MM-dd");
String currentDateandTime = sdf.format(new Date());
And this is the part in the php file that tuches the date:
$date = $_GET['dt'];
$dateFirst = substr($date, 0, 8);
$dateSecond = substr($date, 11, 10);
$dateComplete = $dateFirst . ' ' . $dateSecond;
$sql=mysql_query("INSERT INTO User
VALUES (NULL, '$uName', '$pass', '$mail', '$disName', 0, '$dateComplete')");
The date field in the table is dateTime. The procedure doesn't fall but the date in the table looks like this: "00:00:00 0000-00-00". Can someone please tell me what am I doing wrong? Thank you in advance!
Switch the order of the concatenation:
$dateComplete = $dateSecond . ' ' . $dateFirst;
The way it is currently written you will be passing '11:07:19 2014-07-09' which is not a date format the MySQL understands.
You can use the mysql function NOW() or just insert the formatted php date
$sql=mysql_query("INSERT INTO User
VALUES (NULL, '$uName', '$pass', '$mail', '$disName', 0, NOW())");
OR
$dateComplete = date('Y-m-d H:i:s');
$sql=mysql_query("INSERT INTO User
VALUES (NULL, '$uName', '$pass', '$mail', '$disName', 0, '$dateComplete')");
This works...
mysqli_query
($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', now())
")
This doesn't work....
mysqli_query
($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', date('Y-m-d H:i:s', strtotime('+5 hour')))
")
The mydate column in MySQL is of datetime format.
Any ideas why it's not working? I'm currently using now(), but I want it to show the time in my timezone, not the server's timezone.
I'd suggest storing the date in a variable first.
$date = date('Y-m-d H:i:s', strtotime('+5 hour'));
If both your MySQL and PHP servers are operating on the same time-zone and have their clocks properly synchronized, you wont have an issue.
and then execute the query like so:
mysqli_query($con, "
INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', '$date')
")
I hope this helps!
strtotime() is not MySQL function, its PHP function, you need to write it for being executes...
"INSERT INTO myDB (id, name, mydate)
VALUES ('$id', '$name', '".date('Y-m-d H:i:s', strtotime('+5 hour'))."'