Help inserting Current date into Database

andylpsx

New Member
Messages
29
Reaction score
1
Points
3
I know this is probably something very easy for most of you but I am learning PHP/PDO and trying to get a simple guest book working. The problem I am having is that I am not getting my date to insert into the database as I want it too. Basically it is just an html form that has name and a comment field, when the user fills those out and presses submit it goes to my php file which is supposed to insert into the database, the only problem is that I have not got it to insert anything at all, I've tried searching the internet for advice and help but nothing I found works. All of my question I've asked here have been answered so I figured I'd give it another go. Here are all the pieces of the code.

HTML:
HTML:
<HTML>
<head>
<title>Simple Comment Page To Use PDO</title>
</head>
<body>
<form action="guest.php" method="post">
<label>Enter Your Name</label><input type="text" name="uname" id="uname"></input>
<br>
<label>Enter Your Comment</label>
<input type="textbox" name="cmmt" id="cmmt"></input><br>
<input type="submit" value="Register" />
</form>
</body>
</html>

PHP:
PHP:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', 'dbAdmin', '###########');
$stmt = $dbh->prepare("insert into TestGuest(Name, Date, Comment) values (?,?,?)");
$stmt->bindParam(1, $Name);
$stmt->bindParam(2, $Date);
$stmt->bindParam(3, $Comment);
$Name = $_POST["uname"];
$Date = $_POST["date"];
$Comment = $_POST["cmmt"];
$stmt->execute();
echo "<p>Your Comment has been added.</p>";
?>

I have tried multiple values in the "date" area as well as $Date and the ...bindPara(2,$Date); area... I also switched out the ? where Values is. I can't get anything to work. I've literally tried over a hundred things with no results. Here is a picture of the database:
MwRW5Dt.png


I realize this is probably something relatively easy but I can't get it. Thanks in advance.
 

descalzo

Grim Squeaker
Community Support
Messages
9,375
Reaction score
327
Points
83
PHP:
$Date = $_POST["date"];

Where do you have a "date" field in your form?

If you just want today's date... use PHP to grab the system date and then format it
http://php.net/manual/en/function.date.php
as a string to satisfy mySQL
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-literals.html
and use that as your $Date

When developing/testing , save the return value from execute and test it. True == success and False == failure. On Failure, check the error info
http://pa1.php.net/manual/en/pdostatement.errorinfo.php
 
Last edited:

andylpsx

New Member
Messages
29
Reaction score
1
Points
3
PHP:
$Date = $_POST["date"];

Where do you have a "date" field in your form?

If you just want today's date... use PHP to grab the system date and then format it
http://php.net/manual/en/function.date.php
as a string to satisfy mySQL
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-literals.html
and use that as your $Date

When developing/testing , save the return value from execute and test it. True == success and False == failure. On Failure, check the error info
http://pa1.php.net/manual/en/pdostatement.errorinfo.php

I figured out most of what you said, the only thing I still cannot do is format it so SQL can read it. If I changed the Date field in the data base to varchar(10) and just input strings that look like 2013-12-5 would that work? The updated code I have looks like

Code:
<?php
$today = date("Y-m-d");
$dbh = new PDO('mysql:host=localhost;dbname=test', 'dbAdmin', '#######');
$stmt = $dbh->prepare("insert into TestGuest(Name, Date, Comment) values (?,?,?)");
$stmt->bindParam(1, $Name);
$stmt->bindParam(2, $Date);
$stmt->bindParam(3, $Comment);
$Name = $_POST["uname"];
$Date = $_POST[$today];
$Comment = $_POST["cmmt"];
$stmt->execute();
echo "<p>Your Comment has been added.</p>";
echo "\nPDOStatement::errorInfo():\n";
$arr = $stmt->errorInfo();
print_r($arr);
?>

I still cannot get anything to get added to the database and the error I am getting with the new error statement is:
PDOStatement::errorInfo(): Array ( [0] => 23000 [1] => 1048 [2] => Column 'Date' cannot be null )
 

descalzo

Grim Squeaker
Community Support
Messages
9,375
Reaction score
327
Points
83
Code:
<?php
$today = date("Y-m-d");
$dbh = new PDO('mysql:host=localhost;dbname=test', 'dbAdmin', '#######');
$stmt = $dbh->prepare("insert into TestGuest(Name, Date, Comment) values (?,?,?)");
$stmt->bindParam(1, $Name);
$stmt->bindParam(2, $today);  /*  Change here */
$stmt->bindParam(3, $Comment);
$Name = $_POST["uname"];
/* $Date = $_POST[$today];    Delete this */
$Comment = $_POST["cmmt"];
$stmt->execute();
echo "<p>Your Comment has been added.</p>";
echo "\nPDOStatement::errorInfo():\n";
$arr = $stmt->errorInfo();
print_r($arr);
?>

$_POST is a collection of values sent by your form.
$_POST["uname"] is what the user wrote in the "uname" field (note that it could be an empty string if they did not fill out that field)
$_POST["2013-12-5"] (ie $_POST[$today] if today is Dec 5) is probably empty (NULL)
Just use $today
 

andylpsx

New Member
Messages
29
Reaction score
1
Points
3
Code:
<?php
$today = date("Y-m-d");
$dbh = new PDO('mysql:host=localhost;dbname=test', 'dbAdmin', '#######');
$stmt = $dbh->prepare("insert into TestGuest(Name, Date, Comment) values (?,?,?)");
$stmt->bindParam(1, $Name);
$stmt->bindParam(2, $today);  /*  Change here */
$stmt->bindParam(3, $Comment);
$Name = $_POST["uname"];
/* $Date = $_POST[$today];    Delete this */
$Comment = $_POST["cmmt"];
$stmt->execute();
echo "<p>Your Comment has been added.</p>";
echo "\nPDOStatement::errorInfo():\n";
$arr = $stmt->errorInfo();
print_r($arr);
?>

$_POST is a collection of values sent by your form.
$_POST["uname"] is what the user wrote in the "uname" field (note that it could be an empty string if they did not fill out that field)
$_POST["2013-12-5"] (ie $_POST[$today] if today is Dec 5) is probably empty (NULL)
Just use $today

Thank you! It worked, I tried doing that one time but I didn't know tot take out the $Date. Thanks for helping me. I may be new to this but we all need to start somewhere.
 
Top