Form Submitting Incorrect Information to MySQL Database - php

I've created a form that submits data to a MySQL database but the Date, Time, Year and Month fields constantly revert to the exact same date (1st January 1970) despite the fact that when I submit the information to the database the form displays the current date, time etc to me. I've already set it so that the time and date fields automatically display the current time and date. Could someone please help me with this.
Form:
<html>
<head>
<title>Ultan's Blog | New Post</title>
<link rel="stylesheet" href="css/newposts.css" type="text/css" />
</head>
<body>
<div class="new-form">
<div class="header">
<img src="images/edit-home-button.png">
</div>
<div class="form-bg">
<?php
if (isset($_POST['submit'])) {
$month = htmlspecialchars(strip_tags($_POST['month']));
$date = htmlspecialchars(strip_tags($_POST['date']));
$year = htmlspecialchars(strip_tags($_POST['year']));
$time = htmlspecialchars(strip_tags($_POST['time']));
$title = htmlspecialchars(strip_tags($_POST['title']));
$entry = $_POST['entry'];
$timestamp = strtotime($month . " " . $date . " " . $year . " " . $time);
$entry = nl2br($entry);
if (!get_magic_quotes_gpc()) {
$title = addslashes($title);
$entry = addslashes($entry);
}
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('tmlblog');
$sql = "INSERT INTO php_blog (timestamp,title,entry) VALUES ('$timestamp','$title','$entry')";
$result = mysql_query($sql) or print("Can't insert into table php_blog.<br />" . $sql . "<br />" . mysql_error());
if ($result != false) {
print "<p class=\"success\">Your entry has successfully been entered into the blog. </p>";
}
mysql_close();
}
?>
<?php
$current_month = date("F");
$current_date = date("d");
$current_year = date("Y");
$current_time = date("H:i");
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="month" value="<?php echo $current_month; ?>" />
<input type="text" name="date" value="<?php echo $current_date; ?>" />
<input type="text" name="year" value="<?php echo $current_year; ?>" />
<input type="text" name="time" id="time" size="5"value="<?php echo $current_time; ?>" />
<input class="field2" type="text" id="title" value="Title Goes Here." name="title" size="40" />
<textarea class="textarea" cols="80" rows="20" name="entry" id="entry" class="field2"></textarea>
<input class="field" type="submit" name="submit" id="submit" value="Submit">
</form>
</div>
</div>
</div>
<div class="bottom"></div>
<!-- //End Wrapper!-->
</body>
</html>
</html>
For some reason the posts are being submitted without a timestamp and are reverting to a default timestamp.

Sounds like your form is giving your database a very low amount of "Ticks". Notice you have an with name="date" and id="date" three times. This is most likely your problem. Change those names and ids, do use month, year, date (according to your "postback").

You have three text boxes named date. Since $_POST['month'] and $_POST['year'] are not set, strtotime() cannot figure out the correct timestamp and the SQL query looks like this:
INSERT INTO php_blog (timestamp,title,entry) VALUES ('','Title Goes Here.','')
Change the names of the input elements to what your PHP code is expecting:
<input type="text" name="month" value="<?php echo $current_month; ?>" />
<input type="text" name="date" value="<?php echo $current_date; ?>" />
<input type="text" name="year" value="<?php echo $current_year; ?>" />
After you've done this, strtotime() will be able to parse the date and insert the correct value into the query.
INSERT INTO php_blog (timestamp,title,entry) VALUES ('1272738360','Title Goes Here.','')

It could be that strtotime fails to recognize the format you are submitting your date in.
Try a classic YYYY/MM/DD format:
$timestamp = strtotime("$year/$month/$date $time");

Looks like strtotime don't understand the format you feeding to it.
Use mktime instead.
Anyway you have to always debug your code, i.e. print out the query to see if anything goes wrong.
Or consider to use mysql-supported type colymn, date or datetime
You may be confused an unix timestamp which is just number ans mysql timestamp type column which is string of 2010-05-01 00:00:00

Related

Check date is today's date or yesterday's date

I want to insert data into table when selected date is Current Date or Yesterday's Date.
html code:
<input type="text" id="datepicker1" name="reportdate" class="gui-input" placeholder="Select Date" required="Select date" value="<?php echo date('d/m/Y'); ?>">
php code:
if(isset($_POST['submit'])){
$report_date=date('Y-m-d',strtotime($_POST['reportdate']));
$check_date=date('d',strtotime($_POST['reportdate']));
if($check_date!=date('d') or $check_date!=date('d')-1){
echo "<h2>You can only submit dcr of current date or yesterday.</h2>";
}
else{
$client=$_POST['CLIENT_NAME'];
//$cars_string = implode(', ', $_POST['cars']);
$productname_string=implode(', ',$_POST['productname']);
$remarks=$_POST['remark'];
$sql="insert into d_c_r (bm,date_time,client,promoted_product,remark) values ('".$_SESSION['id']."','$report_date',$client,'$productname_string','$remarks')";
$res=mysql_query($sql) or die(mysql_error());
}
}
Working Code Updated:
Working Demo: http://phpfiddle.org/main/code/fxh8-gnkw
<html>
<body>
<form action="" method="post" name="myform">
<input type="date" id="datepicker1" name="reportdate" class="gui-input" placeholder="Select Date" required="Select date" value="">
<button type="submit" name="submit"> Submit</button>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']) && !empty($_POST['reportdate'])){
$today_date = date('d-m-Y'); // you get current date
$date_tday = new DateTime($today_date);
$date_tday->modify('-1 day');
$get_yesterday_date_from_current = $date_tday->format('d-m-Y'); // you get Yesterday date that from current date
$reportdate = $_POST['reportdate']; // you get report date that user is selected
$report_date_format = new DateTime($reportdate);
echo $report_date = $report_date_format->format('d-m-Y'); //you get report date that user is selected in d-m-Y
/*
$date = new DateTime($report_date);
$date->modify('-1 day');
echo $get_yesterday_date = $date->format('d-m-Y'); // you get Yesterday date that user is selected
*/
if($report_date !== $today_date && $report_date !== $get_yesterday_date_from_current){
echo "<h2>You can only submit dcr of current Date or Yesterday.</h2>";
}
else{
echo "Inser Your Data OR What You WAnt To Do HERE";
}
}
?>

Editing SQL Database using PHP

While editing a specific record using PHP code given below, all records in the database are edited simultaneously to the some garbage values. Here "db" is the Database. I am new to PHP and SQL. Please help
<?php
/*
EDIT.PHP
Allows user to edit specific entry in database
*/
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($reportno, $dateofreceipt, $title, $type, $issuingagency, $markedto, $date, $remarks, $isdate, $issuedto, $returndate)
{
?>
<!DOCTYPE HTML PUBLIC >
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<form action="edit.php" method="post">
<div>
<p><strong>Report No.:</strong> <?php echo $reportno; ?></p>
<strong>Date of receipt: *</strong> <input type="date" name="dateofreceipt" value="<?php echo $dateofreceipt; ?>"/><br/>
<strong>Report Title: *</strong> <input type="text" name="title" value="<?php echo $title; ?>"/><br/>
<strong>Report Type: *</strong> <input type="text" name="type" value="<?php echo $type; ?>"/><br/>
<strong>Issuing agency: *</strong> <input type="text" name="issuingagency" value="<?php echo $issuingagency; ?>"/><br/>
<strong>Marked to: *</strong> <input type="text" name="markedto" value="<?php echo $markedto; ?>"/><br/>
<strong>Date: *</strong> <input type="date" name="date" value="<?php echo $date; ?>"/><br/>
<strong>Remarks: *</strong> <input type="text" name="remarks" value="<?php echo $remarks; ?>"/><br/>
<strong>Issuing Date: *</strong> <input type="date" name="isdate" value="<?php echo $isdate; ?>"/><br/>
<strong>Issued To: *</strong> <input type="text" name="issuedto" value="<?php echo $issuedto; ?>"/><br/>
<strong>Return Date: *</strong> <input type="date" name="returndate" value="<?php echo $returndate; ?>"/><br/>
<p>* Required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$reportno = $_POST['reportno'];
$dateofreceipt = mysql_real_escape_string(htmlspecialchars($_POST['dateofreceipt']));
$title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
$type = mysql_real_escape_string(htmlspecialchars($_POST['type']));
$issuingagency = mysql_real_escape_string(htmlspecialchars($_POST['issuingagency']));
$markedto = mysql_real_escape_string(htmlspecialchars($_POST['markedto']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$remarks = mysql_real_escape_string(htmlspecialchars($_POST['remarks']));
$isdate = mysql_real_escape_string(htmlspecialchars($_POST['isdate']));
$issuedto = mysql_real_escape_string(htmlspecialchars($_POST['issuedto']));
$returndate = mysql_real_escape_string(htmlspecialchars($_POST['returndate']));
//renderForm($reportno, $dateofreceipt, $title, $type, $issuingagency, $markedto, $date,$remarks, $isdate, $issuedto, $returndate, $error);
// save the data to the database
mysql_query("UPDATE `db` SET `Report No.`='[$reportno]',`Date of receipt`='[$dateofreceipt]',`Report Title`='[$title]',`Report Type`='[$type]',`Issuing agency`='[$issuingagency]',`Marked to`='[$markedto]',`Date`='[$date]',`Remarks`='[$remarks]',`Issuing date`='[$isdate]',`Issued to`='[$issuedto]',`Return Date`='[$returndate]' WHERE `Report No.`= '$id'")
// once saved, redirect back to the view page
header("Location: view.php");
}
// query db
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM db WHERE `Report No.`= '$id'")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$reportno = $row['Report No.'];
$dateofreceipt = $row['Date of receipt'];
$title= $row['Report Title'];
$type= $row['Report Type'];
$issuingagency= $row['Issuing agency'];
$markedto= $row['Marked to'];
$date= $row['Date'];
$remarks=$row['Remarks'];
$isdate= $row['Issuing date'];
$issuedto= $row['Issued to'];
$returndate= $row['Return Date'];
// show form
renderForm($reportno, $dateofreceipt, $title, $type, $issuingagency, $markedto, $date, $remarks ,$isdate, $issuedto, $returndate, '');
}
?>
Try this query for updation. Also dont forget to add semicolons after statements. Use mysqli_* Functions instead of mysql_*
mysqli_query("UPDATE `db` SET `Date of receipt`='$dateofreceipt',`Report Title`='$title',`Report Type`='$type',`Issuing agency`='$issuingagency',`Marked to`='$markedto',`Date`='$date',`Remarks`='$remarks',`Issuing date`='$isdate',`Issued to`='$issuedto',`Return Date`='$returndate' WHERE Report No = $reportno");
Several issues here:
The mysql api in PhP is deprecated. Don't bet on it working for longer. Use the mysqli api instead.
In your query the "where 1 part is completly superflous. 1 means true and where 1 means all records, at which point you can leave the WHERE out completly. You probably wanted to use WHERE somekey = 1, which is different.
try this
mysql_query("UPDATE db SET Report No.=".'$reportno'.",Date of receipt=."'$dateofreceipt'.",Report Title=."'$title'.",Report Type=."'$type'.",Issuing agency=."'$issuingagency'.",Marked to=."'$markedto'.",Date=."'$date'.",Remarks=."'$remarks'.",Issuing date=."'$isdate'.",Issued to=."'$issuedto'.",Return Date=."'$returndate'." WHERE Report No.= ."'$id'."")

PHP: The form values are not correctly passed to the handler script

I am using the hidden.php to pass two values to the "hidden_handler.php" for them to be displayed on a webpage. The two variables being used are $user and $time in the hidden.php . However, when they are passed over to the hidden_handler.php, the two values are shown as
" $user" and "$time"and not their assigned values , which are "Hunt" and the actual time.
I have tried to figure out for quite a while but could not find out what is the cause of it. Thanks in advance for any valuable feedback.
Below are the two files with their code.
hidden.php
<?php
date_default_timezone_set(' UTC ');
$time = date(' H:i, F j');
$user = 'Hunt';
?>
<form action = "hidden_handler.php" method = "POST">
<fieldset>
<legend>Send us your comments</legend>
<textarea rows="5" cols="20" name="comment">
</textarea>
<input type="hidden" name="user" value="$user">
<input type="hidden" name="time" value="$time">
</fieldset><p><input type="submit" ></p></form>
?>
hidden_handler.php
<?php
if (!empty($_POST['comment'])) {
$comment = $_POST['comment'];
} else {
$comment = NULL;
echo 'You must enter a comment';
}
$time = (!isset($_POST['time']) ) ? NULL : $_POST['time'];
$user = (!isset($_POST['user']) ) ? NULL : $_POST['user'];
if (( $comment != NULL ) &&
( $time != NULL ) && ( $user != NULL )) {
echo "<p>Comment received :\" $comment\" <br>
From $user at $time </p>";
}
?>
When the hidden.php is run and I key in the comment as "test"
while $user and $time are hard-coded [ $user = 'Hunt' and $time = date(' H:i, F j') ].
And the result is as shown below:
Comment received :" test"
From $user at $time
you should use:
<input type="hidden" name="user" value="<?php echo $user;?>">
<input type="hidden" name="time" value="<?php echo $time?>">
Using just quotes in HTML sees the values inside as strings, they are not interpreted as PHP variables.
When you
<input type="hidden" name="user" value="$user">
<input type="hidden" name="time" value="$time">
you are doing it in HTML, not in PHP, so those values are taken literally - the variables are not substituted.
You need to
<?php
echo "<input type=\"hidden\" name=\"user\" value=$user>"
echo "<input type=\"hidden\" name=\"time\" value=$time>"
?>

Get date from input form within PHP

I'm trying to retrieve a date from a date input form and I just can't get it to work properly. The code I'm using now only returns an error and this date: 1970-01-01. That is not correct and I would like to know how I can approach this. This will be used later to query a database table. I want the date to be basically just a string with this format: "yyyy-mm-dd"
Code:
HTML
<form name="DateFilter" method="POST">
From:
<input type="date" name="dateFrom" value="<?php echo date('Y-m-d'); ?>" />
<br/>
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" />
</form>
PHP
$new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
echo $new_date;
~~~ EDIT ~~~
Fixed Solution for anyone wondering how it's done:
HTML
<form name="Filter" method="POST">
From:
<input type="date" name="dateFrom" value="<?php echo date('Y-m-d'); ?>" />
<br/>
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" />
<input type="submit" name="submit" value="Login"/>
</form>
PHP
$new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
echo $new_date;
Validate the INPUT.
$time = strtotime($_POST['dateFrom']);
if ($time) {
$new_date = date('Y-m-d', $time);
echo $new_date;
} else {
echo 'Invalid Date: ' . $_POST['dateFrom'];
// fix it.
}
<?php
if (isset($_POST['birthdate'])) {
$timestamp = strtotime($_POST['birthdate']);
$date=date('d',$timestamp);
$month=date('m',$timestamp);
$year=date('Y',$timestamp);
}
?>

Getting the date in mysql using input type date

I want to get the date in mysql using input type="date" .. please help for the code...
here is my code:
<form action="sales.php" method="get">
<input type="date" name="d1" value="" required="required" />
<input id="btn" type="submit" value="Search">
</form>
Here is my connection:
<?php
if (isset($_GET[ "d1"])) {
$do=$_GET[ "d1"];
} else {
$do=0;
};
$result=$ db->prepare("SELECT * FROM sales WHERE date=:a");
$result->bindParam(':a', $do);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
Thanks in advance
Inside that sales.php script print out:
$date_value = $_GET['d1'];
echo $date_value;
$myslq_date = date ('Y-m-d H:i:s', strtotime ($date_value));
But check for browser compatibility for that input type - not sure how it's supported.

Categories