Passing dates from PHP to fetch data from Microsoft Access - php

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.

Related

A database is not updated if some values are not filled in the form [duplicate]

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");

How can i post the content of a date input?

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.

save a date into phpmyadmin mysql

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>

Storing data on multiple pages with PHP

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.

PHP search script to pull customer data from mysql

I am trying to build a simple search script in my own customer portal, I am trying to build it to search by their phone ESN. I am not very experienced with php, so I am probably missing something small, or making a small mistake.
anyway, when you type in any esn of a phone and hit search, it shows no results at all. here is the simple script I am working with
<?php
$dbserv='********';
$dbuser='********';
$dbpass='********';
$dbdata='********';
$link = mysql_connect($dbserv, $dbuser, $dbpass);
if (!$link) { die('Could not connect: ' . mysql_error());}mysql_select_db($dbdata);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="esn" /><br />
<input type="submit" value="Submit" />
</form>
<?php
if (!empty($_REQUEST['esn'])) {
$esn = mysql_real_escape_string($_REQUEST['esn']);
$sql = "SELECT * FROM phones where phoneserial LIKE '%".$esn."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo 'ESN: ' .$row['phoneserial'].'<br>';
}
}
?>
</body>
</html>
I noticed I made an error earlier in this line
$esn = mysql_real_escape_string($_REQUEST['esn']);
I had it like this instead
$term = mysql_real_escape_string($_REQUEST['esn']);
and then when you typed a 14 digit esn starting with A10000, it would actually show every esn in our database instead of the one that was searched for and i don't know why.
Forgive me if this is lame, still trying to learn
If $esn was undefined, you'd be searching for a pure wildcard (%%), which would match every possible record. Not that surprising when you look at this code:
$sql = "SELECT * FROM phones where phoneserial LIKE '%".$esn."%'";
Just remove the $esn since you didn't define $esn and instead used $term.
You really shouldn't use mysql_ functions anymore. Learn how to use mysqli or PDO and prepared statements rather than escape string functions.
if u want to find only ONE, strict match, you should replace your query with this
$sql = "SELECT * FROM phones where phoneserial = '{$esn}'";
Don't use mysql, use Mysqli
MySQL extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
Final well form code is
<?php
$dbserv='********';
$dbuser='********';
$dbpass='********';
$dbdata='********';
$link = mysqli_connect($dbserv, $dbuser, $dbpass,$dbdata);
if (!empty($_POST['esn'])) {
$esn = mysqli_real_escape_string($link$_POST['esn']);
$sql = "SELECT * FROM phones where phoneserial LIKE '%".$esn."%'";;
$r_query = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($r_query))
{
echo 'ESN: ' .$row['phoneserial'].'<br>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="esn" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>

Categories