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.
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.
This question already has answers here:
Why does this PDO statement silently fail?
(2 answers)
Closed 1 year ago.
I'm trying to update an SQL database from GUI. If I fill out all the fields in the form it works, but if any of the fields is NULL or empty nothing will update. No error is thrown, but database won't update in phpmyadmin.
I want to be able to accept NULL and empty fields, including be able to erase content in a field.
I'm not a programmer so I'd appreciate explanations on how to deal with these empty fields.
<?php
include ("connection.php");
include ("foundation.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP UPDATE DATA USING PDO</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form name="frmUser" method="post" action="">
<input type="text" name="id" required placeholder="id"><br><br>
<input type="text" name="person" required placeholder="person"><br><br>
<input type="text" name="dob" required placeholder="dob"><br><br>
<input type="text" name="dod" required placeholder="dod"><br><br>
<input type="submit" name="update" required placeholder="Update Data">
</form>
</body>
</html>
<?php
if(isset($_POST['update']))
{
// get values from input text and number
$id = $_POST['id'];
$person = $_POST['person'];
$dob = $_POST['dob'];
$dod = $_POST['dod'];
// mysql query to Update data
$pdoquery = "UPDATE people SET id=:id, person=:person,dob=:dob,dod=:dod WHERE id='" . $_GET['id'] . "'";
$pdoQuery_run = $pdocbcon->prepare($pdoquery);
$pdoQuery_exec = $pdoQuery_run->execute(array(":person"=>$person,":dob"=>$dob,":dod"=>$dod,":id"=>$id));
if($pdoQuery_exec)
{
echo 'Uppdaterat';
}
else
{
echo 'FEL';
}
}
?>
ID value cannot be null so in front end it must be inserted and in database you need to allow null for all the other columns.
if(isset($_POST['update']))
{
// get values from input text and number
$id = $_POST['id'];
$person = $_POST['person'];
$dob = $_POST['dob'];
$dod = $_POST['dod'];
if(empty($id)){
echo("Missing Information! You must input ID");
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>
I have a php form that users submit their start / end dates. The form then needs to pull the results from .xml hosted on a different URL. The URL varies based on the dates entered by the user - dates become part of the URL as you can see below.
So, simply put... The user enters the dates. It calls the xml file via the URL (which changes slightly as the dates are entered into that URL) and it displays the results on a new page.
I would rather do it via PHP than AJAX if possible.
This is the frontend of my form:
<html>
<body>
<form action="test_get.php" method="get">
Start Date: <input type="text" id="start" name="start"><br>
End Date: <input type="text" id="end" name="end"> <br>
<input type="submit">
</form>
</body>
</html>
This is 'test_get.php':
<html>
<body>
<form onSubmit="return process();">
Start Date: <?php echo $_GET["start"]; ?><br>
End Date: <?php echo $_GET["end"]; ?>
</form>
</body>
<script>
function process()
{
var url="http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug," + document.getElementById("start").value + "," + document.getElementById("end").value;
location.href=url;
return false;
}
</script>
</html>
But it doesn't work. I also tried this:
<html>
<body>
<?php
$xml=simplexml_load_file("http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug," + document.getElementById("start").value + "," + document.getElementById("end").value"); or die("Error: Cannot create object");
echo $xml->field . "<br>";
echo $xml->data . "<br>";
?>
</body>
</html>
But again, no luck. Can anyone see what i'm missing?
I can also share what my .xml form looks like if needed. Any help is greatly appreciated - i've been tackling this for days! Thanks! :)
UPDATE: This is my updated code for test_get.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
$url = '"http://69.239.118.197:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug,'.$_GET['start'].','.$_GET['end'].'"';
$xml = simplexml_load_file($url);
echo $xml->field . "<br>";
echo $xml->data . "<br>";
echo var_dump($xml). "<br />";
?>
</body>
</html>
In the last block of code that you posted, you are using JavaScript outside of script tags, so this is definitely not going to work. Have you tried instead putting your $_GET variable into the query string?
Note* I pulled out the query string for more readability. It's hard for me to verify that this is working for you outside of your environment, but give it a shot.
You've got this:
$xml=simplexml_load_file("http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug," + document.getElementById("start").value + "," + document.getElementById("end").value"); or die("Error: Cannot create object");
I'm recommending this:
$url = '"http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug,'.$_GET['start'].','.$_GET['end'].'"';
$xml = simplexml_load_file($url);
*************EDIT*****************
So, after attempting to manipulate the data that simplexml_load_file returns I decided to try file_get_contents. The solution below returns the data value from the xml file in string format.
$_GET['start'] = '08/30/2017';
$_GET['end'] = '08/31/2017';
$xml = file_get_contents("http://69.239.118.197:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug,".$_GET['start'].','.$_GET['end'].'"');
echo $xml;
I'm building a business planner based on a blog model. I can't however do it over multiple pages. On page 1, when the user clicks "Next", it's like clicking "Post". The data is stored and added to the list of other business plans, or "posts". One the first page, the data is stored by "Insert"ing it into the database. I thought maybe I could simply "Update" the database on page 2 and so on to eliminate multiple listed "posts" or business plans. The data is being stored from the first page, but not the second page. If I add the data for each page using "INSERT INTO..." that works, but each pages is collected as a separate business plan, or multiple posts. Any advice is appreciated.
Here's Page 1:
<?php
session_start();
include_once("db.php");
if(isset($_POST['post'])) {
$title = strip_tags($_POST['title']);
$compName = strip_tags($_POST['compName']);
$createdBy = strip_tags($_POST['createdBy']);
$phone = strip_tags($_POST['phone']);
$title = mysqli_real_escape_string($db,$title);
$compName = mysqli_real_escape_string($db,$compName);
$createdBy = mysqli_real_escape_string($db,$createdBy);
$phone = mysqli_real_escape_string($db,$phone);
$date = date('l jS \of F Y h:i A');
$sql = "INSERT INTO plans (title, date, compName, createdBy, phone) VALUES('$title', '$date', '$compName', '$createdBy', '$phone')";
mysqli_query($db, $sql);
header("Location: post2.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<div class="container">
<head>
<title>Create Business Plan</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<h2>Create a new business plan</h2>
<form action="post1.php" method="post" enctype="multipart/form-data">
<br /><br />
<p>Plan Title: <input name="title" type="text" autofocus size="48"></p>
<p>Company Name: <input name="compName" type="text" autofocus size="48"></p>
<p>Business Type: <input placeholder="Ie. Inc. (USA) or Oy (Finland)" name="bizType" type="text" autofocus size="48"></p>
<p>Created By: <input name="createdBy" type="text" autofocus size="48"></p>
<p>Phone: <input name="phone" type="text" autofocus size="48"></p>
<br /><br />
<form action="<?php 'post2.php=?pid=$id'; ?>">
<input name="post" type="submit" value="Next">
</form>
<br /><br />
</form>
</body>
</div>
</html>
Here's Page 2:
<?php
session_start();
include_once("db.php");
if(isset($_POST['post'])) {
$marketPlan = strip_tags($_POST['marketPlan']);
$economics = strip_tags($_POST['economics']);
$products = strip_tags($_POST['products']);
$customers = strip_tags($_POST['customers']);
$marketPlan = mysqli_real_escape_string($db,$marketPlan);
$economics = mysqli_real_escape_string($db,$economics);
$products = mysqli_real_escape_string($db,$products);
$customers = mysqli_real_escape_string($db,$customers);
$date = date('l jS \of F Y h:i A');
$sql = "UPDATE plans SET marketPlan='$marketPlan', economics='$economics', products='$products', customers='$customers' WHERE id=$pid";
mysqli_query($db, $sql);
header("Location: post3.php"); //CHANGE LOCATION FOR NEXT PAGE
}
?>
<!DOCTYPE html>
<html lang="en">
<div class="container">
<head>
<title>Create Business Plan</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<h2>The Marketing Plan</h2>
<form action="post2.php" method="post" enctype="multipart/form-data">
<br /><br />
<h4>Market Research</h4>
<p>The marketing plan requires intensive research for the type of industry your business wants to enter. It is very dangerous to assume that you are already well informed about your intended market. Market research is vital to make sure you are up to date. Use the business planning process as your opportunity to uncover data and to question your marketing efforts.</p>
<textarea name="marketPlan" rows="15" cols="100"></textarea></p><hr />
<h4>Economics</h4>
<p>What is the total size of your market? What percent of market share will you have? (This is important only if you think you will be a major factor in the market.) What is currently in demand in your target market? What are the trends in target market—growth, consumer preferences, and in product development? Growth potential and opportunity for a business of your size.
<textarea name="economics" rows="15" cols="100"></textarea><hr />
<h4>Products</h4>
<p>In the <i>Products and Services</i> section, you described your products and services from your point of view. Now describe them from how your customers see them.</p><br /><textarea name="products" rows="15" cols="100"></textarea></p><hr />
<h4>Customers</h4>
<p>Identify your targeted customers, their characteristics, and their geographical location. This is known as customer demographics.</p>
<textarea name="customers" rows="15" cols="100"></textarea></p>
<input name="post" type="submit" value="Next">
</form>
</body>
</div>
</html>
do some thing like this, rather than inserting and updating the data to database, store the values in session variables, i mean
example :
$_SESSION["title"] = strip_tags($_POST['title']);
store every option in session variable like this until you reach the last page.
finally in the last page
insert it to the db. like this,
insert into table ('title',.....) values ($_SESSION["title"],....);
always use sessions when you want to pass data across multiple pages.
I think you are doing right only. First post have to insert those details in table, next post update what are fields you want to update.
in second page you have to get the id from your url
if (isset($_GET['pid']))
$pid = $_GET['pid'];
as you try to update an id that doesnt is defined
You must pass the ID of the newly created record to the next page; the most secure way is probably to store it in the session. So, on page one, change this:
mysqli_query($db, $sql);
To
$query = mysqli_query($db, $sql);
$_SESSION['new_plan_id'] = $query->insert_id;
Then, in your second page query the ID again
if (isset($_SESSION['new_plan_id']))
{
$pid = $_SESSION['new_plan_id'];
}
else
{
die('No valid session');
}
After a user finished his or her plan, remove the new_plan_id from the session. You may also want to add session_start() to a global include so it is always enabled.