I'm trying to insert the current date into a database for each entry within an array. I've found documentation for inserting the current date, but nothing that explains how to do it within an array.
<?php
$connect = new PDO("mysql:host=localhost;dbname=testing",
"root", "");
$query = "
INSERT INTO tbl_test
(full_name, id_number, email, pin_rank, team_name)
VALUES (:full_name, :id_number, :email, :pin_rank, :team_name)
";
for($count = 0; $count<count($_POST['hidden_full_name']); $count++)
{
$data = array(
':full_name' => $_POST['hidden_full_name'][$count],
':id_number' => $_POST['hidden_id_number'][$count],
':email' => $_POST['hidden_email'][$count],
':pin_rank' => $_POST['hidden_pin_rank'][$count],
':team_name' => $_POST['hidden_team_name']
);
$statement = $connect->prepare($query);
$statement->execute($data);
}
?>
I would like the current date to display in the last column within the table. Any help would be appreciated.
Assuming you've some kind of date column (date, datetime, etc...) and it's named in my example date_time, just do the following:
$query = "
INSERT INTO tbl_test
(full_name, id_number, email, pin_rank, team_name, date_time)
VALUES (:full_name, :id_number, :email, :pin_rank, :team_name, NOW())
"
Note, if you've got anything other than a date field, you'll get the time along this as well.
I'm not a big fan of the DB NOW because the DB uses it's own timezone settings separate from PHP.
So you can change the TimeZone in PHP and not in MySql and wind up with dates that are wrong (found that out the hard way one time).
And as that has been shown already, I'll show you how to do it with just PHP.
$query = "
INSERT INTO tbl_test
(full_name, id_number, email, pin_rank, team_name, created)
VALUES (:full_name, :id_number, :email, :pin_rank, :team_name, :created)
";
$today = (new DateTime)->format('Y-m-d H:i:s');
#$today = date('Y-m-d H:i:s'); //procedural
for($count = 0; $count<count($_POST['hidden_full_name']); $count++)
{
$data = array(
':full_name' => $_POST['hidden_full_name'][$count],
':id_number' => $_POST['hidden_id_number'][$count],
':email' => $_POST['hidden_email'][$count],
':pin_rank' => $_POST['hidden_pin_rank'][$count],
':team_name' => $_POST['hidden_team_name'],
':created' => $today
);
$statement = $connect->prepare($query);
$statement->execute($data);
}
You can of course get the date and time, within the loop if you need second precision but it's less efficient, due to multiple calls to DateTime.
Related
I have been trying this query and researching but I can not understand why it does not work.
The IP record(VARCHAR) is saved in the table on the database, but I can not save the record DATETIME, when the record is inserted.
$sql = "INSERT INTO ips (ip, fecha) VALUES (:ip, :CURRENT_TIMESTAMP)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':ip' => htmlentities($_POST['ip']),
':CURRENT_TIMESTAMP' => htmlentities(CURRENT_TIMESTAMP)
));
Use PHP's time function:
$sql = "INSERT INTO ips (ip, fecha) VALUES (:ip, :CURRENT_TIMESTAMP)";
$t = time();
$today = date("Y-m-d", $t);
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':ip' => htmlentities($_POST['ip']),
':CURRENT_TIMESTAMP' => $today
I am trying to insert timestamp to my MySQL database using PHP. But there is shown an error off
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter
number: parameter was not defined in
/opt/lampp/htdocs/e-exam/user.message.php on line 22
But
Here line 22 is ':sent_at' => $timestamp
$timestamp = date('Y-m-d G:i:s');
//message sending
$sql3 = "INSERT INTO message(title, content, sender_id, sent_at ) VALUES (:title, :content, :sender_id, :sent_at)";
$stmt3 = $pdo->prepare($sql3);
$stmt3->execute(array(
':title' => $_POST['title'],
':content' => $_POST['content'],
':email' => $_SESSION['active_user'],
':sent_at' => $timestamp
));
Please help me to solve this error
If you only want to insert the sent_at ( current time ) , there is easier way, by using NOW() at first you create the table.
the other way is by change the line into this
"INSERT INTO message(title, content, sender_id, sent_at ) VALUES (:title, :content, :sender_id, NOW())";
take a lot at NOW() in the query.
you can try the query
select NOW()
it will result something like this 2018-07-04 18:17:49
in mysql you can use data type datetime
I understand that such questions have been downvoted or closed, however I've been through all the previously asked questions and couldn't find any solutions. The code is supposed to accept input from a HTML form and insert it into the MySQL database and be displayed on another page, but somehow I end up with query failed statement and redirected to the Login page.
<?php
if (isset($_POST['eTransport_ID'])){
$id = ($_POST['eTransport_ID']);
}
if (isset($_POST['Phone_No'])){
$phone = ($_POST['Phone_No']);
}
if (isset($_POST['Amount'])){
$amount = ($_POST['Amount']);
}
$DATEE = date("Y-m-d");
$TIMEE = date("h:i:sa");
$query = "INSERT INTO recharge_history(Date, Time, Amount, Agent_No, eTransport_ID) VALUES ('$DATEE', '$TIMEE', '$amount', 'WEB', '$id')";
$added = mysqli_query($conn, $query);
if(!$added){
die("Database query failed.");
}
else{
echo "Data Entered Successfully";
}
mysqli_close($conn);
?>
MySQL Table
$query = "INSERT INTO recharge_history(Date, Time, Amount, Agent_No, eTransport_ID) VALUES ('$DATEE', '$TIMEE', '$amount', 'WEB', '$id')";
Can you write query like this
$query = "INSERT INTO recharge_history(Date, Time, Amount, Agent_No, eTransport_ID) VALUES ($DATEE, $TIMEE, $amount, $phone, $id)";
This is BAD code which has severe SQL injection vulnerabilities so please look into that separately (your question isn't about security)
I'm assuming 'Date' is a DATE column
I'm assuming 'Time' is a TIME column
It looks as though Agent_No may be an INT column, but you're inserting a string. I've substituted for a "1" so you'll have to look at that separately.
If my assumptions are correct, just change the query line to exactly this (ie: copy and paste it):
$query = "INSERT INTO recharge_history (`Date`, `Time`, `Amount`, `Agent_No`, `eTransport_ID`) VALUES (NOW(), NOW(), $amount, 'WEB', $id)";
I have this code for adding new products to my database, but It does'nt work at all.
I want that date be inserted automaticly by code not manually, and because of that I used now() in my mysql, but now I use PDO and I have now idea, how could I do it. Here is my code:
<?php
require"../scripts/config.php";
if(isset($_POST['product_name'])){
$product_name = $_POST['product_name'];
$product_category= $_POST['product_category'];
$product_quality= $_POST['product_quality'];
$product_size = $_POST['product_size'];
$product_price = $_POST['product_price'];
$product_description= $_POST['product_description'];
$q = "INSERT INTO tuotteet (name, category, quality, size, price, description, date_add)
VALUES(:name, :category, :quality, :size, :price, :description, NOW())";
require "../scripts/connect.php";
$query = $connect->prepare($q);
$result = $query->execute(array(
":name" => $product_name,
":category" => $product_category,
":quality" => $product_quality,
":size" => $product_size,
":price" => $product_price,
":description" => $product_description
));
}
?>
You are using NOW() in SQL which is good approach, but you are also trying to pass in the :date_add parameter which doesn't exist in the SQL as a placeholder. Don't bind value on :date_add when you execute().
You need to check for and handle errors around your database accesses.
And you're also missing a single quote at if (isset($_post['product_name])) which should have been if (isset($_post['product_name']))
I am currently learning OOP, but I can't figure out how to insert date into database.
I have tried:
$time=date('now');
$time=date("timestamp");
$time=date("Y-m-d H:i:s");
$time=date("m/d/y g:i A");
When I submit I get error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2014-03-11 14:18:07'' at line 1
I can't figure this out. Before I did use:
$result = mysql_query ("INSERT INTO test (title,url,time) VALUES ('$title','$url',now())");
I tried to use now() , but it does not seem to work. :/
Update:
Insert:
public function insert($cat,$title,$article,$author,$image,$time)
{
$sql=mysql_query("INSERT INTO blog_posts(cat, title, article, time, author, image) VALUES('$cat', '$title', '$article', '$author', '$image, '$time'");
if(!$sql)
{
echo mysql_error();
}
}
proccess file:
$cat=$_POST['cat'];
$title=$_POST['title'];
$article= $_POST['article'];
$author=$_POST['author'];
$image=$_POST['image'];
$time= date( 'Y-m-d H:i:s' );
$n=new db();
$n->connect();
$n->insert($cat,$title,$article,$author,$image,$time);
You're missing a quote:
'$author', '$image, '$time'"
^^^^^
HERE
INSERT INTO blog_posts(cat, title, article, time, author, image) VALUES('$cat', '$title', '$article', '$author', '$image', '$time'"
Your parameters are also out of order.
Columns: cat, title, article, time, author, image
Values: '$cat', '$title', '$article', '$author', '$image', '$time'
Forget about the mysql_* functions.
If you want to create future-proof applications you will use PDO. This is OOP.
$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$db->exec('SET NAMES utf8'); // Set this transfer's charset to utf-8
$title = 'here is a title';
$url = 'here is a URL';
$time= date( 'Y-m-d H:i:s' ); // I dont know what type the time column is. (date, datetime, timestamp)-> ??
$prepare = $db->prepare("INSERT INTO test (title, url, time) VALUES (:title, :url, :time)");
$prepare->bindParam(':title', $title);
$prepare->bindParam(':url', $url);
$prepare->bindParam(':time', $time);
$prepare->execute(); // Run the query, in this case, insert!
If the time column is of type date or datetime, you can use NOW() or CURDATE() like so:
$prepare = $db->prepare("INSERT INTO test (title, url, time) VALUES (:title, :url, NOW())");
theres nothing to do with objects in this code.
and the first snippet of code is bascily overwriting all over
so its left with the latest. what exactly are you trying to achieve?
could you post some more of your code?
if you wish to use mysql OBJECT oriented, use MySQLi or PDO driver.