I want to insert date into database using php here is my php code and html input date tag
<form action="try.php" method="post">
<input type="date" name="date" id="date" />
<input type="submit" name="submit" id="sub" />
</form>
<?php
if(isset($_Post['submit'])){
$see = $_POST['date'];
$insert_c = "insert into test(date) values ($see)";
$run = mysql_query($insert_c);
}
}
?>
i have a suggestion for you. you can use mysqli_query insted of mysql_query.
i think if you use mysql_query that is really nota good practice.
here the date will be in the format of mm/dd/yy so you need to give the same type in the database to i mean in phpmyadmin. or you can save the date in database by making the date field as varchar..
INSERT INTO test.date (id, date) VALUES ('1', '22/07/2017');
one more thing while you executing the query you have to pass the connection object too
example: mysql_query( $query, $conn );
I use the following code
NOTE: Whenever your redirecting to the same page in the form action please use the following code <?php echo $_SERVER['PHP_SELF'] ?> this specifies that you want to redirect to the same page. Instead of leaving the action blank or putting the same name as that of the current page.
One problem might be check your column data type it must of type date
For Eg the table must look something like this
CREATE TABLE IF NOT EXISTS `test` (
`created_date` date NOT NULL
)
And the following code does the job for inserting into that.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
/* Her I am including my db_connect file which has db connection resource */
include_once ('db_connect.php');
/* User filter_input instead of $_POST */
$createdDate = filter_input(INPUT_POST,'createdDate');
/* Query to insert into test table */
$result = mysqli_query($link, "INSERT INTO test (created_date) VALUES ('".$createdDate."')");
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Insert Date</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label>Created Date<label/>
<input type="date" name="createdDate"><br>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Related
I am trying to get data from Microsoft access database by passing value from two datepickers by the name of from and to. I am creating a php website but i just cant get ms access to return me the data i want.
This is my current code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Date Query</title>
</head>
<body>
<form action="" method="POST">
<label for="from">From :</label>
<input type="date" id="from" name="from">
<label for="to">To :</label>
<input type="date" id="to" name="to">
<button type="submit" name="submit">Submit</button>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$from=$_POST['from'];
$to=$_POST['to'];
$conn = odbc_connect("Ingot_Daily_Report", "", "");
$query = "SELECT * FROM Output_Data WHERE data_Day BETWEEN '$from' AND '$to'";
$sql = odbc_exec($conn, $query);
while($row=odbc_fetch_array($sql)){
echo $row['data_Day'];
echo "<br>";
echo $row['KPI_Daily_Target'];
echo "<br><br>";
}
}
?>
The between clause is not returning me any data but when i remove the between clause, the sql fetches me all the data available in the database table. Is there a syntax or operator that i should use or any changing of date format to retrieve the correct set of data ?
My data_Day column is in shortText format. I want the returning data to be in between those two dates.
After some hard findings, i added the # sign before and after my php variable in the SQL query. The reason behind this is because Microsoft Access seems to only recognize data in between # signs as date format.
Before
$query = "SELECT * FROM Output_Data WHERE data_Day BETWEEN '$from' AND '$to'";
After
$query = "SELECT * FROM Output_Data WHERE data_Day BETWEEN #$from# AND #$to#";
And I used date() and strtotime() function to change my format of date from the datepicker
date("Y/m/d",strtotime(##php variable with date value stored inside##));
Lastly, i change my column data format into date/time under short date in order for these codes to work.
The problem is in insert query.If we insert a single data in the db it is inserted for multiple time in that the primary key(auto increment) is increased but the value give by the user didn't store in db why?
<html>
<head>
<title>comment</title>
</head>
<body>
<?php
require('db.php');
?>
<form action="db.php" method="get">
<input type="textarea" name="textarea" rows="4" value="" >
<input type="submit" name="submit" value="submit">
</form>
<?php
$comment = isset($_GET['textarea']) ? $_GET['textarea'] : '';
$sql="INSERT INTO comment(comments) VALUES('$comment')";
mysqli_query($con,$sql);
?>
</body>
</html>
the actual table name is comment and the column name is comment_id and comments.
if a user ask some question in textarea(in HTML design) that should be inserted in comments column.
You should not submit the form to the same file you are requiring on line 7.
Rather submit the form to itself. Try this:
<html>
<head>
<title>comment</title>
</head>
<body>
<?php
require('db.php');
if(!isset($_GET['submit']))
{
?>
<form method="get">
<input type="textarea" name="textarea" rows="4" value="" >
<input type="submit" name="submit" value="submit">
</form>
<?php
} else {
$comment = isset($_GET['textarea']) ? $_GET['textarea'] : '';
$sql="INSERT INTO comment(comments) VALUES('$comment')";
mysqli_query($con,$sql);
}
?>
</body>
</html>
Also like #aynber mentioned in the comments, take advantage of prepared statements and bind_param, to secure your app a bit.
i have a form in html that contains an input type=date, and a php code to post the informations in a database,
php:
if(isset($_POST['Ajouter'])){ // Fetching variables of the form which
travels in URL
$date = $_POST['date'];
$requete="insert into absences(IdAbs, DateAbs, IdEmp ) values ('1', '$date',
'3')";
$query = mysqli_query($db,$requete);
html :
<!Doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="testabs.php" method="post">
<input id="date" type="date" name="date">
<input type="Submit" value="Ajouter" name="Ajouter">*
</form>
</body>
</html>
i don't know how to post the type=date, can anyone help me with this?
If you are getting an undefined index it probably means the date isn't being posted. Try:
if(isset($_POST['date']))
{
$date= $_POST['date'];
}
else
{
$date= "2020-02-02";
}
Or if using PHP7+
$date= (isset($_POST['date']) ? $_POST['date'] : "2020-02-02");
If you end up with 2020-02-02 in your database, you have a post issue.
To change the format to suit SQL date field:
$inputDate= new dateTime($_POST['date']);
$date= $inputDate->format('Y-m-d');
You still need to add a suitable check to see if the date actually exists in the first place.
I am running WAMP 2.5 on Win7 and created a webpage to insert records into a mySQL database. This worked perfectly.
Then I needed to use SQL Server 2014 (remote server on LAN) instead of mySQL (local in WAMP), and while everything seems ok, i am not getting the records created in the table, yet no error either.
Account used to log in with is db owner.
Any help would be great.
Thanks
Len
Code is as follow (I call sqlconnect.php for db connection) and get confirmation that connection was successful.
File is called insert-data.php so it calls itself if form is empty and Submit is selected.
<?php
if(!empty($_POST['mcpbarcode'])) {
header("Location insert-data.php");
include('sqlconnect.php');
$mcpbarcode = $_POST['mcpbarcode'];
$sqlinsert = "INSERT INTO mcpbarcode (mcpbarcode)
VALUES ('$mcpbarcode')";
sqlsrv_query($conn,$sqlinsert);
if (!$sqlinsert) {
die('error inserting new record');
}
$newrecord = "New record added";
}
?>
<html>
<head>
<title> Insert Barcode into Database</title>
</head>
<h3><font color = red>This window must be open and active</font></h3>
<img src="header.jpg" alt="anyname" style="width:800px;height:165px;">
<body>
<h1> Insert barcode into Database</h1>
<form method="post" action="insert-data.php">
<input type="hidden" name="submitted" value="true"/>
<label>Barcode:<input type="text" name="mcpbarcode" autofocus /></label>
<br /><br />
<input type="submit" value="Add new record" />
</form>
<?php
$newrecord="";
echo $newrecord
?>
</body>
</html>
See if it's working like that:
If mcpbarcode column is a string type (varchar, char etc):
$sqlinsert = "INSERT INTO mcpbarcode (mcpbarcode)
VALUES ('".$mcpbarcode."')"
OR if mcpbarcode column is int, float, etc:
$sqlinsert = "INSERT INTO mcpbarcode (mcpbarcode)
VALUES (".$mcpbarcode.")"
but you should have an error on mysql also ...
first time posting on here so I apologise for any bad habits.
I recently started an online youtube tutorial on php and how to create a blog.
I ended up getting occupied with other things and have come back to try an finish what I started and 2 things have happened. 1: my tutorials have been deleted off youtube(must of been a copyrright issue) and second I've completely forgot the method used. I assume if I was a seasoned coder this would be easy to decipher but I'm having no luck after trying for days now.
This code is for the submission form for my blog. The blog is working in the sense of if I manually input my HTML into the SQL database but all I seem to get if I use this form is a refresh of the submission page with all the information gone. No information is added to the database.
Anybody have an idea?I had a good search around the site but I ran into a dead end due to my lack of knowledge on what I was actually searching for (lots of solutions regarding javascript)
All help will be appreciated.
Sincerely
SGT Noob
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
if (isset($_SESSION['username'])) {
$username = ($_SESSION['username']);
}
else {
header('Location: ../index.php');
die();
}
if (isset($_POST['submit']))
if ($_POST['submit']) {
$title = $_POST['Post_Title'];
$content = $_POST['Post_Content'];
$date = $_POST['Post_Date'];
include_once("../admin/connection.php");
$sql = "INSERT INTO `posts` (Post_Title, Post_Content, Post_Date)
VALUES ('$title','$content','$date')";
mysqli_query($dbcon,$sql);
echo "Post has been added to the database";
}
else {
header('Location: index.php');
die();
}
?>
<html>
<div>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="//cdn.ckeditor.com/4.5.9/standard/ckeditor.js"></script>
<title> Insert Post </title>
</head>
<div>
<body>
<div id= 'cmssubmissionform'>
<form action="" method="post">
<p>
<h1> Post Title</h1>
<input type="text" name= "Post_Title"/>
</p>
<h1> Post Date</h1>
<input type="text" name= "Post_Date"/>
</p>
<p>
<h1>Post Content </h1>
<textarea name="Post_Content"></textarea>
<script>
CKEDITOR.replace( 'Post_Content' );
</script>
</p>
<p>
<input type = "submit" value="submit" />
</p>
</form>
</div>
</div>
</div>
</body>
try this
in your from change to
<input type = "submit" value="submit" name="submit"/>
You forgot to put the name attribute
The php
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
include_once("../admin/connection.php");
if (isset($_SESSION['username'])) {
$username = ($_SESSION['username']);
}
else {
header('Location: ../index.php');
die();
}
if (isset($_POST['submit'])){
$title = $_POST['Post_Title'];
$content = $_POST['Post_Content'];
$date = $_POST['Post_Date'];
$sql = "INSERT INTO `posts` (`Post_Title`, `Post_Content`, `Post_Date`)
VALUES ('$title','$content','$date')";
if(mysqli_query($dbcon,$sql)){
echo "Post has been added to the database";
}else{
header('Location: index.php');
die();
}
}
?>
Note I also changed your SQL statement to
INSERT INTO `posts` (`Post_Title`, `Post_Content`, `Post_Date`)
VALUES ('$title','$content','$date
Notice the back ticks for the table fields
Replace
if (isset($_POST['submit']))
if ($_POST['submit']) {
with
if (isset($_POST['submit'])) {
and then replace
<input type = "submit" value="submit" />
with
<input type ="submit" name="submit" value="submit" />
However, at this stage I would highly suggest starting over with a different tutorial.
Also, as has been mentioned in the comments, whenever you take arguments from a user and put them into a database query, you must absolutely make sure that the strings do not manipulate the query (imagine someone wrote 0'; DROP TABLE `blog`; -- in the date field (and "blog" were the name of your blog post table). That would be quite catastrophic, wouldn't it?
So when you handle input data, either use the prepare and bind methods of the mysqli package, or pass the strings through the mysqli_real_escape_string() function first.