here is the script code of my page.
i select any date and i get the data by selecting date from the database.
i want to initially set the date to today when i open my page.
then i get the data of today and of current time.
but how ???
i give you an example: this is example link.
here you can see timestamp box to show results of current date and time
i want to do it exactly like this.
but how?
<link rel="stylesheet" type="text/css" href="tcal.css" />
<script type="text/javascript" src="tcal.js"></script>
<form action="index.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" />
<input type="submit" value="Search">
</form>
<table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4">
<thead>
<tr>
<th> Birtday </th>
<th> Name </th>
<th> Gender </th>
</tr>
</thead>
<tbody>
<?php
include('connect.php');
if (isset($_GET["d1"])) { $d1 = $_GET["d1"]; } else { $d1="0000-00-00"; };
$result = $db->prepare("SELECT * FROM birthday WHERE date = :a");
$result->bindParam(':a', $d1);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['gender']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
replace
if (isset($_GET["d1"])) { $d1 = $_GET["d1"]; } else { $d1="0000-00-00"; };
with
if (isset($_GET["d1"])) { $d1 = $_GET["d1"]; } else { $d1=date('"Y-m-d"); };
or even shorter
$d1 = isset($_GET["d1"]) ? $_GET["d1"] : date('"Y-m-d");
to set the current date as the initial date.
Your other question is "How do i get a calendar to select a date" right? If so, just google "How do i get a calendar to select a date with php". Or search for PHP Calendar select.
This is not a free "write my code for me" website by the way. Sorry.
To get current time and date, you can use the date function. For example:
$d1 = date('Y-m-d H:i:s');
It will give you something like:
"2014-03-14 08:52:30"
Related
I am trying to query a database and display the results. The query is based on information from an html form. However, when I enter a name such as john in the form, for which the table has 2 entries with that name, I get 0 results. I don't know what the problem is.
Here is the html form:
<form action="cust_details_search.php" method="post">
Name :
<input type="text" name="name_search" id="name_search" >
Email :
<input type="email" name="email_search" id ="email_search" >
Phone no. :
<input type="phone" name="phone_search" id="phone_search" >
Address :
<input type="text" name="address_search" id="address_search" >
City :
<input type="text" name="city_search" id="city_search" >
State :
<input type="text" name="state_search" id="state_search" >
<br> <br>
Country :
<input type="text" name="country_search" id="country_search">
Product Enquired for :
<input type="text" name="prod_search" id="prod_search">
<input type="submit" value="Submit">
</form>
And the php file:
<?php
$server = "127.0.0.1";
$dbUsername = "root";
$dbPassword = "";
//create connection
$dbconn = new mysqli($server, $dbUsername, $dbPassword, $dbname);
$name_search = $_POST['name_search'];
$email_search = $_POST['email_search'];
$phone_search = $_POST['phone_search'];
$address_search = $_POST['address_search'];
$city_search = $_POST['city_search'];
$state_search = $_POST['state_search'];
$country_search = $_POST['country_search'];
$prod_search = $_POST['prod_search'];
$run_query = mysqli_query($dbconn,
"SELECT *
FROM CustomerDetails
WHERE (Name LIKE '%.$name_search.%')
OR (`Phone no.` LIKE '%.$phone_search.%')
OR (`Address` LIKE '%.$address_search.%')
OR (`City` LIKE '%.$city_search.%')
OR (`State` LIKE '%.$state_search.%')
OR (`Country` LIKE '%.$country_search.%')
OR (`Product Enq. For` LIKE '%.$prod_search.%')
OR (`Email` LIKE '%.$email_search.%')");
?>
<html>
<head>
<title>Search Resutls</title>
<style>
body {
background-color: rgb(131,41,54);
}
h1 { color:#FFFFFF
}
h2 { color:#FFFFFF
}
p { color:#FFFFFF
}
</style>
</head>
<body>
<center>
<h2> Customer Details </h2>
<table style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone no. </th>
<th>Address </th>
<th>City </th>
<th>State </th>
<th>Country</th>
<th>Product Enquired for </th>
<th>Follow up details </th>
</tr>
</thead>
<tbody>
<?php
while($result = mysqli_fetch_assoc($run_query)) {
?>
<tr>
<td><?php echo $result['Name'] ?> </td>
<td><?php echo $result['Email'] ?></td>
<td><?php echo $result['Phone no.'] ?></td>
<td><?php echo $result['Address'] ?></td>
<td><?php echo $result['City'] ?></td>
<td><?php echo $result['State'] ?></td>
<td><?php echo $result['Country'] ?></td>
<td><?php echo $result['Product Enq. For'] ?></td>
<td><?php echo $result['Follow Up'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</center>
</body>
</html>
Any help is appreciated. Thank you in advance!
You are using the PHP concatenator . but you dont need to in a double quoted string. $variables are automatically expanded in a double quoted string so try
$run_query = mysqli_query($dbconn,
"SELECT *
FROM CustomerDetails
WHERE (Name LIKE '%$name_search%')
OR (`Phone no` LIKE '%$phone_search%')
OR (`Address` LIKE '%$address_search%')
OR (`City` LIKE '%$city_search%')
OR (`State` LIKE '%$state_search%')
OR (`Country` LIKE '%$country_search%')
OR (`Product Enq For` LIKE '%$prod_search%')
OR (`Email` LIKE '%$email_search%')");
ALso some of your column names had a . in them? I assume these column names do not actually contain dots. However if the do, I suggest you remove them by editing your schema.
Your script is wide open to SQL Injection Attack
Even if you are escaping inputs, its not safe!
Use prepared parameterized statements in either the MYSQLI_ or PDO API's
I have inserted all the payments done from my database into a table and all their information they I was meant to show to the visitor. However, I'd like to add this feature that by the time that the visitor select each row they can visit all the products they've bought on that day. In other words I want a modal to pops out and show the invoice for that purchase (Meaning that all the products that were bought at the same day). However how do I get which date has been selected?
<?php
$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid;");
?>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Brand</th>
<th>Price</th>
<th>Quantity</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_assoc($myQuery)){
?>
<tr data-toggle="modal" data-id="1" data-target="#orderModal">
<td><?php echo $row["ProductID"] ?></td>
<td><?php echo $row["ProductName"] ?></td>
<td><?php echo $row["Brand"] ?></td>
<td><?php echo $row["Price"] ?></td>
<td><?php echo $row["Quantity"] ?></td>
<td><?php echo $row["Date"] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
There are several ways to go about it. First, you can use PHP to select the date:
$date = date('Y-m-d');
$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date = '$date';");
Or you can use MySQL's CURDATE() method:
$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date = CURDATE();");
This will work if your date format is in Y-m-d (2016-06-03) format. If it's in another format, you're going to have to do a bit of DATE_FORMAT manipulation. This will only work for "today". Otherwise, go with the first method and pass in the desired date.
As an aside, mysql_* functions are deprecated, and removed as of PHP7. I'd really recommend using PDO or mysqli, and using it to bind your parameters so you don't have to worry about mysql injection.
Use Jquery DATEPICKER to choose date and submit form on your target file where query is written
Get the date on that page like this:
$date = date('Y-m-d');
if(iseet($_POST['date']))
{
$date = date("Y-m-d", strtotime($_POST['date']));
}
$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date = '$date' ");
Here is the code for datepicker
$(function() {
$( "#datepicker" ).datepicker();
});
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<form action="yourfile.php" method ="post">
<p>Date: <input type="text" name="date" id="datepicker"></p>
<input type="submit" value="Submit">
</form>
Use a date selector for selecting the date and after that make a ajax call to you php function with that selected date and then fire a query like SELECT * FROMpaymentWHERE UserID = $uid and Date = $date; and the popup a model with returned data from ajax call.
mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date between '$date1' and '$date2'");
$date1 is the date of purchase with hour, minutes and seconds set to 0s
$date2 is date of purchase with hours set to 23, minutes to 59 and seconds to 59
if $date is 2015-05-05 16:34:22, $date1 is 2015-05-05 00:00:00 and $date2 is 2015-05-05 23:59:59 , same day :)
I have database table schedule with id, date, time, cabinet columns.
Also I have table cabinets with id, cabinetNumber, title columns.
I should generate table, but it should look like queue table.
cabinet 8,9,10,11,12,13,14,15,16
102 - - - + - - + - -
103 - + - - - - + - -
if time is busy it will be +, if free -.
I need modify my mysql query. And should be possible to change date (usege datepicker and Ajax) and change table rows values depend of date.
But if date does not exist, query will be empty and table will be without rows. How can i fixed it?
MySQL query :
$q = $_GET['date'];
$query = mysql_query("SELECT date,cabinet,
SUM(IF(ROUND(`time`)=8,1,0)) as h8,
SUM(IF(ROUND(`time`)=9,1,0)) as h9,
SUM(IF(ROUND(`time`)=10,1,0)) as h10,
SUM(IF(ROUND(`time`)=11,1,0)) as h11,
SUM(IF(ROUND(`time`)=12,1,0)) as h12,
SUM(IF(ROUND(`time`)=13,1,0)) as h13,
SUM(IF(ROUND(`time`)=14,1,0)) as h14,
SUM(IF(ROUND(`time`)=15,1,0)) as h15
FROM `schedule` WHERE date = '".$q."'
GROUP BY cabinet") or die(mysql_error());
?>
table :
<table class="table table-hover">
<tr class=".info"><td>Cab</td><td >8:00</td><td >9:00</td><td >10:00</td><td >11:00</td><td >12:00</td><td >13:00</td><td >14:00</td><td >15:00</td></tr>
<!-- -->
<?php while($data=mysql_fetch_array($query)) :?>
<tr>
<?php $cabinet = $data['cabinet']; ?>
<td><?=$cabinet ?></td>
<?php for($j=8; $j<=15; $j++) : ?>
<?php
$busy = $data['h'.$j];
?>
<?php if($busy>0 && $data['date']===$q ): ?>
<td class="busy"></td>
<?php else: ?>
<td class="free">
<form action="1.php" method="post">
<input type="hidden" name="time" value="<?= $j;?>" />
<input type="hidden" name="cabinet" value="<?= $cabinet;?>" />
<input type="submit" style="free" value="" name="sub"/>
</form>
</td>
<?php endif?>
<?php endfor ?>
</tr>
<?php endwhile ?>
</table>
And jQuery / Ajax:
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
});
$( ".date" ).on('change', function(){
var date = $('#datepicker').val();
$.ajax({
type:'get',
url:'table.php',
data : {
'date' : date
},
success: function(data) {
$('#result').html(data);
}
});
});
</script>
I had solwe my problem. I added new query query2 from cabinets table.
<?php $query2 = mysql_query("select cabinetNum from cabinets") ?>
<table class="table table-hover">
<tr class=".info"><td>Cab</td><td >8:00</td><td >9:00</td><td >10:00</td><td >11:00</td><td >12:00</td><td >13:00</td><td >14:00</td><td >15:00</td></tr>
<!-- -->
<?php while($data2=mysql_fetch_array($query2)): ?>
<?php $data=mysql_fetch_array($query) ?>
<tr>
<?php $cabinet = $data2['cabinetNum']; ?>
<td><?=$cabinet ?></td>
<?php for($j=8; $j<=15; $j++) : ?>
<?php
$busy = $data['h'.$j];
?>
<?php if($busy>0 ): ?>
<td class="busy"></td>
<?php else: ?>
<td class="free">
<form action="1.php" method="post">
<input type="hidden" name="time" value="<?=$j?>" />
<input type="hidden" name="cabinet" value="<?=$cabinet?>" />
<input type="hidden" name="date" value="<?=$q?>" />
<input type="submit" class="free" value="" name="sub"/>
</form>
</td>
<?php endif?>
<?php endfor ?>
</tr>
<?php endwhile ?>
</table>
The key is to keep the queries simple, use PHP to process and display the data. I will not go in to the altering of data with datapicker and ajax as you need to get the displaying correct first. Please take a look at this code and understand what it does and why it solves the problem of when there is no data for a date. You dont seem to have added the cabinet name in your table, so I kept it as an id. You can do a simple join in the query to get the cabinet name.
PHP code
<?php
//Using the object orientated style from this page
//http://php.net/manual/en/mysqli-stmt.bind-param.php
//Connect to database
$link = new mysqli("localhost", "root", "", "scheduler");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//Use todays date unless specified
$date = date("Y-m-d");
if (isset($_GET['date'])) {
$date = $_GET['date'];
}
$cabinet_activity = array();
//First do a query to get all distinct cabinets, whether they have activity for the date or not
if ($cabinets = $link->prepare("SELECT DISTINCT cabinet FROM schedule")) {
$cabinets->execute();
$result = $cabinets->get_result();
while ($this_row = $result->fetch_assoc()) {
$cabinet_activity[$this_row['cabinet']] = array(); //Initialise the busy array for the next query
}
}
if ($stmt = $link->prepare("SELECT id, HOUR(time) as hour, cabinet FROM schedule WHERE date = ?")) {
$stmt->bind_param('s', $date);
$stmt->execute();
$result = $stmt->get_result();
while ($this_row = $result->fetch_assoc()) {
$id = $this_row['id'];
$hour = $this_row['hour'];
$cabinet_id = $this_row['cabinet'];
//Add to cabinet to cabinet activity list (should not be necessary now)
/*
if (!array_key_exists($cabinet_id, $cabinet_activity)) {
$cabinet_activity[$cabinet_id] = array();
}
*/
//Add the activity time to the busy hours
if (!in_array($hour, $cabinet_activity[$cabinet_id])) {
$cabinet_activity[$cabinet_id][] = $hour;
}
}
}
$min_hours = 8;
$max_hours = 16;
//Display information:
?>
<table border="1">
<thead>
<tr>
<td>Cabinet</td>
<?php
for($hour = $min_hours; $hour <= $max_hours; $hour++) {
?><td><?php print $hour; ?></td><?php
}
?>
</tr>
</thead>
<tbody>
<?php
//For each cabinet
foreach($cabinet_activity as $cabinet_id => $busy_hours) {
?><tr>
<td><?php print $cabinet_id; ?></td>
<?php
for($hour = $min_hours; $hour <= $max_hours; $hour++) {
if (in_array($hour, $busy_hours)) {
?><td>+</td><?php
} else {
?><td>-</td><?php
}
}
?>
</tr><?php
} ?>
</tbody>
</table>
I'm having trouble searching the date field of mysql database.. I have a html form..that allows the user to choose 3 different ways to search the database.. field 1 is student_id, field 2 is lastname, field 3 is date. Well when i run the program and choose student id, I get the proper result back, when i do the same using last name I get the proper result back, but when i use date..I do not get any return. I happen to know what the result should be because i see it in the database..and besides that its the same data record as the student id, and last name. I think it might have something to do with format, but I can't figure it out..
I do not know aJax so please don't suggest ajax code right now.
here is the html code.
[code]
-- start javascript -->
<script type="text/javascript">
/*<![CDATA[ */
function check(){
if(document.lastname.last.value == "" || document.lastname.last.value == null)
{
alert("no last name entered");
return false;
}
}
function checkdate() {
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
if(!(date_regex.test(testDate)))
{
return false;
}
}
function fieldSwap(image){
var sb = document.getElementById('sb');
if(sb.value == ""){
sb.style.background = "url(images/"+image+") no-repeat";
}
}
function buttonSwap(image){
var sb = document.getElementById('sb');
sb.src = "images/"+image;
}
function validate(){
var x = document.information.search.value;
if (x.length<10 || x.length>10){
alert("student id is incorrect");
return false;
}
}
/*]]> */
</script>
<!-- end javascript -->
</head>
<body>
<div id="form_wrap"><!-- start form wrap -->
<div id="form_header">
</div>
<div id="form_body">
<p>Search for a certification request (Enter one of the following):</p>
<form action="search.php" method="POST" name="information" id="information" onsubmit="return(validate()or return(checkdate())">
<div class="field">
<select name="type">
<option value="student_id">Student ID</option>
<option value="last_name">Last name</option>
<option value="examDate">Exam date</option>
</select>
<input name="typeValue" value="" />
<input type="submit" value="Search" />
</form>
</div>
</div>
</div><!-- end form wrap -->
</body>
</html>
<form action = "" method = "POST">
<div class="field">
<label for = "first_name"> first_name</label>
<input type = "text" name = "first_name" id = "first_name">
</div>
<div class = "field">
<label for = "last_name"> last_name </label>
<input type ="text" name = "last_name" id = "last_name">
</div>
<div class = "field">
<label for = "bio"> bio </label>
<textarea name = "bio" id = "bio"></textarea>
</div>
<input type = "submit" value = "Insert">
</form>
[/code]
Here is the PHP code
[code]
$records = array();
$typeValue = $_REQUEST['typeValue'];
//If they did not enter a search term we give them an error
if ($typeValue == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
// We perform a bit of filtering
//$typevalue = strtoupper($search);
$typeValue = strip_tags($typeValue);
$typeValue = trim ($typeValue);
$value = $_POST['typeValue'];
if($_POST['type'] == "student_id")
{
//Query with $value on student_id
if($result = $db->query("SELECT * FROM records WHERE student_id LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
elseif($_POST['type'] == "last_name")
{
//Query with $value on last_name
if($result = $db->query("SELECT * FROM records WHERE last_name LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
elseif($_POST['type'] == "examDate")
{
//Query with $value on date
if($result = $db->query("SELECT * FROM records WHERE examDate LIKE '$typeValue'" )){
if($result->num_rows){
while($row = $result->fetch_object()){
$records[] = $row;
}
$result->free();
}
}
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
//$anymatches=$result;
//if ($anymatches == 0 )
//{
//echo "Sorry, but we can not find an entry to match your query...<br><br>";
//}
//And we remind them what they searched for
//echo "<b>Results For:</b> " .$typeValue;
//}
?>
<!DOCTYPE html>
<html>
<style type="text/css">
th{text-align: left;}
table, th, td{ border: 1px solid black;}
</style>
<head>
<title>Search Result</title>
</head>
<body>
<h3> Results for <?php echo $typeValue ?> </h3>
<?php
if(!count($records)) {
echo 'No records';
} else {
?>
<table style="width:100%">>
<th>
<tr>
<th>student_id</th>
<th>First name</th>
<th>Last name</th>
<th>email</th>
<th>Major</th>
<th>Exam Name</th>
<th>Taken class</th>
<th>Prepare</th>
<th>MeasureUp Key</th>
<th>Exam Date</th>
<th>Request Made On</th>
</tr>
</thead>
<tbody>
<?php
foreach($records as $r){
?>
<tr>
<td><?php echo $r->student_id; ?></td>
<td><?php echo $r->first_name; ?></td>
<td><?php echo $r->last_name; ?></td>
<td><?php echo $r->email; ?></td>
<td><?php echo $r->major; ?></td>
<td><?php echo $r->examName?></td>
<td><?php echo $r->taken_class; ?></td>
<td><?php echo $r->prepare; ?></td>
<td><?php echo $r->measureUpKey; ?></td>
<td><?php echo $r->examDate; ?></td>
<td><?php echo $r->request_made; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
</html>
<html>
<head></head>
<body>
<br/>
Return to Search
</body>
</html>
[/code]
I Believe you are using like to get the Exam Dates for that session or month so you can use this
Using DATE_FORMAT function
SELECT * FROM records WHERE DATE_FORMAT(examDate, '%Y %m') = DATE_FORMAT('$typeValue', '%Y %m') ORDER BY examDate
or may be you are looking for a specific date than
SELECT * FROM records WHERE examDate = '$typeValue'
I am building an application that gives user the opportunity to click on the day of the calendar and when there is an activity registered on that day, it shows a table with the activity registered...my problem is that when there is no activity on that day, I wont it to return the string : There is no activity.. I try to make it using if($nrofrows>0)like below but all the time it returns me the string :there is not activity, even I have activity on that day.
Please can you help me ? where is my error? Thanks in advance...
<body>
<?php
mysql_connect("127.0.0.1","root","") or die("Smund te lidhet me serverin");
mysql_select_db("axhenda") or die("Kjo databaze nuk u gjet");
session_start();
$perdoruesi=$_SESSION['user_id'];
$result= mysql_query("SELECT * FROM Aktiviteti where Data= '$_POST[dataoutput]' and Perd_Id='$perdoruesi'");
$nrofrows= mysql_num_rows($result);
if($nrofrows>0)
{
?>
<div class="title"> Aktivitetet per daten <?php print ("$_POST[dataoutput]"); ?></div>
<form name="form1" method="post" action="delete.php">
<table >
<th>
<th ><strong>Emri </strong></th>
<th ><strong>Pershkrimi </strong></th>
<th><strong>Ora</strong></th>
</th>
<?php
while ($row=mysql_fetch_array($result)) {
?>
<tr>
<td ><input name="checkbox[]" type="checkbox" value="<?php echo $row['Id_Akt']; ?>"></td>
<td style="font-size:0.9em"><?php echo $row['Emri']; ?></td>
<td ><?php echo $row['Pershkrimi']; ?></td>
<td><?php echo $row['Ora']; ?></td>
</tr>
<?php
}
?>
</table>
<input class="button" name="delete" type="submit" value="Delete" style="margin-left:40%; margin-top:100px; width:15%">
</form>
<?php
}
else {
echo "<span id='errorformat'>There is no activity on this day!<span>";}
?>
</body>
</html>
Might be a problem with the query you can add this to see:
$result= mysql_query("SELECT * FROM Aktiviteti where Data= '$_POST[dataoutput]' and Perd_Id='$perdoruesi'") or die ("query error" .mysql_error());
mysql_num_rows($result) returns false when there is an error so you might want to add this:
if($nrofrows && $nrofrows>0)
try this
if ( mysql_num_rows($result) > 0 ) {
}