<?php
$dbc = mysqli_connect($servername, $username, $password, $database);
if (!$dbc) {
die("DATABASE CONNECTION FAILED:" . mysqli_error($dbc));
exit();
}
$datex = $_GET["mydate"];
$query2 = "select * from sales where ddate='$datex'";
$result=mysqli_query($dbc, $query2);
if ($result) {
$p_ltr = mysqli_real_escape_string($dbc, $_GET["p_ltr"]);
$p_rat = mysqli_real_escape_string($dbc, $_GET["p_rat"]);
$p_amt = mysqli_real_escape_string($dbc, $_GET["p_amt"]);
$query = "update sales set p_ltr='$p_ltr',p_rat='$p_rat',p_amt='$p_amt' where ddate='$datex'";
if (mysqli_query($dbc, $query)) {
echo "Records updated successfully";
exit();
} else {
echo "ERROR: Could3 not able to execute" .$query." ".mysqli_error($dbc);
}
} else {
//$date = mysqli_real_escape_string($dbc, $_GET["date"]);
$p_ltr = mysqli_real_escape_string($dbc, $_GET["p_ltr"]);
$p_rat = mysqli_real_escape_string($dbc, $_GET["p_rat"]);
$p_amt = mysqli_real_escape_string($dbc, $_GET["p_amt"]);
$query = "INSERT INTO sales(ddate,p_ltr,p_rat,p_amt)
VALUES ('$datex','$p_ltr','$p_rat','$p_amt')";
if (mysqli_query($dbc, $query)) {
echo "Records added successfully";
} else {
echo "ERROR: Could not able to execute" .$query." ".mysqli_error($dbc);
}
}
mysqli_close($dbc);
?>
It means this part of query
$datex = $_GET["mydate"];
$query2 = "select * from sales where ddate='$datex'";
$result=mysqli_query($dbc, $query2);
if ($result) {
is not working still.
Purpose of above query:
If use enters any date then query checks if date already exists
if yes then all records will be updated
if given date does not exists in table then a new record will be entered.
If I follow above codes and enter a date like '2023-12-12' more than 1 time then there will be no restriction or validation to avoid this record to be entered into table.
In my case I want to insert any date only 1 time. there will be no duplicated dates.
So before inserting record, I want to assure that given date is already exist or not exists.