find if entry exists between two dates in two different columns?
what is wrong in the code?? I need to allow a new entry only if a record does not exist for the given time period. Like if 1-1-19 to 3-1-19 is there in database new entry for this date range should not happen.
<?php
if (isset($_POST['book'])) {
require 'db2.php';
$name=$_POST['name'];
$mail=$_POST['emial'];
$mob=$_POST['phone'];
$ddr=$_POST['add'];
$d11=$_POST['date'];
$d12=$_POST['date2'];
$selected_val=$_POST['type'];
if (empty($name)||empty($mail)||empty($mob)||empty($ddr)||empty($d11)||empty($d12)||empty($selected_val)) {
header("Location: ./booking.php?error=emptyfield");
exit();
}
elseif (!filter_var($mail, FILTER_VALIDATE_EMAIL)&& !preg_match("/^[a-zA-Z]*$/",$name)) {
header("Location: ./booking.php?error=entervalidnaneoremail");
exit();
}
elseif (!preg_match("/^[0-9]*$/", $mob)) {
header("Location: ./booking.php?error=invalidphone");
}
else {
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_row($result);
echo $row;
if ($row>0) {
header("Location: ./booking.php?error=Takendate");
exit();
}
else {
$sql="INSERT INTO book(name, email, no, adr, d1, d2, type) VALUES('$name','$mail','$mob','$ddr','$d11','$d12','$selected_val')" ;
mysqli_query($conn,$sql);
}
}
}
?>
php mysql
add a comment |
what is wrong in the code?? I need to allow a new entry only if a record does not exist for the given time period. Like if 1-1-19 to 3-1-19 is there in database new entry for this date range should not happen.
<?php
if (isset($_POST['book'])) {
require 'db2.php';
$name=$_POST['name'];
$mail=$_POST['emial'];
$mob=$_POST['phone'];
$ddr=$_POST['add'];
$d11=$_POST['date'];
$d12=$_POST['date2'];
$selected_val=$_POST['type'];
if (empty($name)||empty($mail)||empty($mob)||empty($ddr)||empty($d11)||empty($d12)||empty($selected_val)) {
header("Location: ./booking.php?error=emptyfield");
exit();
}
elseif (!filter_var($mail, FILTER_VALIDATE_EMAIL)&& !preg_match("/^[a-zA-Z]*$/",$name)) {
header("Location: ./booking.php?error=entervalidnaneoremail");
exit();
}
elseif (!preg_match("/^[0-9]*$/", $mob)) {
header("Location: ./booking.php?error=invalidphone");
}
else {
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_row($result);
echo $row;
if ($row>0) {
header("Location: ./booking.php?error=Takendate");
exit();
}
else {
$sql="INSERT INTO book(name, email, no, adr, d1, d2, type) VALUES('$name','$mail','$mob','$ddr','$d11','$d12','$selected_val')" ;
mysqli_query($conn,$sql);
}
}
}
?>
php mysql
Your code is insecure. See about prepared statements
– Strawberry
Jan 20 at 10:00
And presumably you mean '2019-01-03' !
– Strawberry
Jan 20 at 10:02
Also, you're logic is flawed. What if you B inserts the book while user A is checking?
– Strawberry
Jan 20 at 10:20
add a comment |
what is wrong in the code?? I need to allow a new entry only if a record does not exist for the given time period. Like if 1-1-19 to 3-1-19 is there in database new entry for this date range should not happen.
<?php
if (isset($_POST['book'])) {
require 'db2.php';
$name=$_POST['name'];
$mail=$_POST['emial'];
$mob=$_POST['phone'];
$ddr=$_POST['add'];
$d11=$_POST['date'];
$d12=$_POST['date2'];
$selected_val=$_POST['type'];
if (empty($name)||empty($mail)||empty($mob)||empty($ddr)||empty($d11)||empty($d12)||empty($selected_val)) {
header("Location: ./booking.php?error=emptyfield");
exit();
}
elseif (!filter_var($mail, FILTER_VALIDATE_EMAIL)&& !preg_match("/^[a-zA-Z]*$/",$name)) {
header("Location: ./booking.php?error=entervalidnaneoremail");
exit();
}
elseif (!preg_match("/^[0-9]*$/", $mob)) {
header("Location: ./booking.php?error=invalidphone");
}
else {
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_row($result);
echo $row;
if ($row>0) {
header("Location: ./booking.php?error=Takendate");
exit();
}
else {
$sql="INSERT INTO book(name, email, no, adr, d1, d2, type) VALUES('$name','$mail','$mob','$ddr','$d11','$d12','$selected_val')" ;
mysqli_query($conn,$sql);
}
}
}
?>
php mysql
what is wrong in the code?? I need to allow a new entry only if a record does not exist for the given time period. Like if 1-1-19 to 3-1-19 is there in database new entry for this date range should not happen.
<?php
if (isset($_POST['book'])) {
require 'db2.php';
$name=$_POST['name'];
$mail=$_POST['emial'];
$mob=$_POST['phone'];
$ddr=$_POST['add'];
$d11=$_POST['date'];
$d12=$_POST['date2'];
$selected_val=$_POST['type'];
if (empty($name)||empty($mail)||empty($mob)||empty($ddr)||empty($d11)||empty($d12)||empty($selected_val)) {
header("Location: ./booking.php?error=emptyfield");
exit();
}
elseif (!filter_var($mail, FILTER_VALIDATE_EMAIL)&& !preg_match("/^[a-zA-Z]*$/",$name)) {
header("Location: ./booking.php?error=entervalidnaneoremail");
exit();
}
elseif (!preg_match("/^[0-9]*$/", $mob)) {
header("Location: ./booking.php?error=invalidphone");
}
else {
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_row($result);
echo $row;
if ($row>0) {
header("Location: ./booking.php?error=Takendate");
exit();
}
else {
$sql="INSERT INTO book(name, email, no, adr, d1, d2, type) VALUES('$name','$mail','$mob','$ddr','$d11','$d12','$selected_val')" ;
mysqli_query($conn,$sql);
}
}
}
?>
<?php
if (isset($_POST['book'])) {
require 'db2.php';
$name=$_POST['name'];
$mail=$_POST['emial'];
$mob=$_POST['phone'];
$ddr=$_POST['add'];
$d11=$_POST['date'];
$d12=$_POST['date2'];
$selected_val=$_POST['type'];
if (empty($name)||empty($mail)||empty($mob)||empty($ddr)||empty($d11)||empty($d12)||empty($selected_val)) {
header("Location: ./booking.php?error=emptyfield");
exit();
}
elseif (!filter_var($mail, FILTER_VALIDATE_EMAIL)&& !preg_match("/^[a-zA-Z]*$/",$name)) {
header("Location: ./booking.php?error=entervalidnaneoremail");
exit();
}
elseif (!preg_match("/^[0-9]*$/", $mob)) {
header("Location: ./booking.php?error=invalidphone");
}
else {
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_row($result);
echo $row;
if ($row>0) {
header("Location: ./booking.php?error=Takendate");
exit();
}
else {
$sql="INSERT INTO book(name, email, no, adr, d1, d2, type) VALUES('$name','$mail','$mob','$ddr','$d11','$d12','$selected_val')" ;
mysqli_query($conn,$sql);
}
}
}
?>
<?php
if (isset($_POST['book'])) {
require 'db2.php';
$name=$_POST['name'];
$mail=$_POST['emial'];
$mob=$_POST['phone'];
$ddr=$_POST['add'];
$d11=$_POST['date'];
$d12=$_POST['date2'];
$selected_val=$_POST['type'];
if (empty($name)||empty($mail)||empty($mob)||empty($ddr)||empty($d11)||empty($d12)||empty($selected_val)) {
header("Location: ./booking.php?error=emptyfield");
exit();
}
elseif (!filter_var($mail, FILTER_VALIDATE_EMAIL)&& !preg_match("/^[a-zA-Z]*$/",$name)) {
header("Location: ./booking.php?error=entervalidnaneoremail");
exit();
}
elseif (!preg_match("/^[0-9]*$/", $mob)) {
header("Location: ./booking.php?error=invalidphone");
}
else {
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_row($result);
echo $row;
if ($row>0) {
header("Location: ./booking.php?error=Takendate");
exit();
}
else {
$sql="INSERT INTO book(name, email, no, adr, d1, d2, type) VALUES('$name','$mail','$mob','$ddr','$d11','$d12','$selected_val')" ;
mysqli_query($conn,$sql);
}
}
}
?>
php mysql
php mysql
asked Jan 20 at 9:46
shashikant kantelashashikant kantela
1
1
Your code is insecure. See about prepared statements
– Strawberry
Jan 20 at 10:00
And presumably you mean '2019-01-03' !
– Strawberry
Jan 20 at 10:02
Also, you're logic is flawed. What if you B inserts the book while user A is checking?
– Strawberry
Jan 20 at 10:20
add a comment |
Your code is insecure. See about prepared statements
– Strawberry
Jan 20 at 10:00
And presumably you mean '2019-01-03' !
– Strawberry
Jan 20 at 10:02
Also, you're logic is flawed. What if you B inserts the book while user A is checking?
– Strawberry
Jan 20 at 10:20
Your code is insecure. See about prepared statements
– Strawberry
Jan 20 at 10:00
Your code is insecure. See about prepared statements
– Strawberry
Jan 20 at 10:00
And presumably you mean '2019-01-03' !
– Strawberry
Jan 20 at 10:02
And presumably you mean '2019-01-03' !
– Strawberry
Jan 20 at 10:02
Also, you're logic is flawed. What if you B inserts the book while user A is checking?
– Strawberry
Jan 20 at 10:20
Also, you're logic is flawed. What if you B inserts the book while user A is checking?
– Strawberry
Jan 20 at 10:20
add a comment |
2 Answers
2
active
oldest
votes
I think you should use AND
instead of OR
.
add a comment |
First of all, your dates ($d11 and $d12
) should be in format yyyy-mm-dd
so they meet the standar date format for mysql.
Then you need to change this sentence:
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
and use an AND instead of OR like this:
$sql="SELECT * FROM book WHERE d1>'$d11' AND d2<'$d12'";
and last, you should use prepared statements to avoid SQL injection
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275219%2ffind-if-entry-exists-between-two-dates-in-two-different-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you should use AND
instead of OR
.
add a comment |
I think you should use AND
instead of OR
.
add a comment |
I think you should use AND
instead of OR
.
I think you should use AND
instead of OR
.
answered Jan 20 at 9:51
shingoshingo
1,9391520
1,9391520
add a comment |
add a comment |
First of all, your dates ($d11 and $d12
) should be in format yyyy-mm-dd
so they meet the standar date format for mysql.
Then you need to change this sentence:
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
and use an AND instead of OR like this:
$sql="SELECT * FROM book WHERE d1>'$d11' AND d2<'$d12'";
and last, you should use prepared statements to avoid SQL injection
add a comment |
First of all, your dates ($d11 and $d12
) should be in format yyyy-mm-dd
so they meet the standar date format for mysql.
Then you need to change this sentence:
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
and use an AND instead of OR like this:
$sql="SELECT * FROM book WHERE d1>'$d11' AND d2<'$d12'";
and last, you should use prepared statements to avoid SQL injection
add a comment |
First of all, your dates ($d11 and $d12
) should be in format yyyy-mm-dd
so they meet the standar date format for mysql.
Then you need to change this sentence:
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
and use an AND instead of OR like this:
$sql="SELECT * FROM book WHERE d1>'$d11' AND d2<'$d12'";
and last, you should use prepared statements to avoid SQL injection
First of all, your dates ($d11 and $d12
) should be in format yyyy-mm-dd
so they meet the standar date format for mysql.
Then you need to change this sentence:
$sql="SELECT * FROM book WHERE d1>'$d11' OR d2<'$d12'";
and use an AND instead of OR like this:
$sql="SELECT * FROM book WHERE d1>'$d11' AND d2<'$d12'";
and last, you should use prepared statements to avoid SQL injection
answered Jan 20 at 10:50
nachonacho
2,79811223
2,79811223
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275219%2ffind-if-entry-exists-between-two-dates-in-two-different-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your code is insecure. See about prepared statements
– Strawberry
Jan 20 at 10:00
And presumably you mean '2019-01-03' !
– Strawberry
Jan 20 at 10:02
Also, you're logic is flawed. What if you B inserts the book while user A is checking?
– Strawberry
Jan 20 at 10:20