Hi I am trying to insert and update the query in function using Angularjs and returning the data in the form of JSON objects. In this query I am comparing year and email. If there is no data for current year and email the data should be inserted otherwise the data should be updated. But when I am trying that the data is not inserting when I write both conditions (Insert and Update). If I write only insert query it is accepting.
function addrentalproperty($data)
{
$rentalannualrent = $data->rentalProperty->rentalannualrent;
$rentalblock = $data->rentalProperty->rentalblock;
$rentalstreet = $data->rentalProperty->rentalstreet;
$rentalarea = $data->rentalProperty->rentalarea;
$rentaltown = $data->rentalProperty->rentalcity;
$rentalstate = $data->rentalProperty->rentalstate;
$rentalpincode = $data->rentalProperty->rentalpincode;
session_start();
$userInfo = $_SESSION['USER'];
$userEmailid = $userInfo->getEmailid();
if ($rentalannualrent != '' && $rentalblock != '' && $rentalstreet != '' && $rentalarea != '' && $rentaltown != '' && $rentalstate != '' && $rentalpincode != '') {
$query = mysql_query("UPDATE rental_details SET
rental_annual_rent = '$rentalannualrent',
rental_block = '$rentalblock',
rental_street = '$rentalstreet',
rental_area = '$rentalarea',
rental_town = '$rentaltown',
rental_state = '$rentalstate',
rental_pincode = '$rentalpincode',
WHERE email ='$userEmailid' AND currentyear=NOW()");
} else {
$query = mysql_query("INSERT INTO rental_details(email,rental_annual_rent,rental_block,rental_street,rental_area,rental_town,rental_state,rental_pincode,rental_ownership,currentyear)
values('$userEmailid','$rentalannualrent','$rentalblock','$rentalstreet','$rentalarea','$rentaltown','$rentalstate','$rentalpincode','$rentalownership',NOW())");
}
if ($query) {
$successJson = '{"success":"Rental Property Details Added Successfully."}';
print_r($successJson);
} else {
$failureJson = '{"error":"Problem While Saving Property Details."}';
print_r($successJson);
}
}
The issue is, NOW()
will provide the current date and time and it will not provide the year.
But you are using NOW()
to check against current year in your update query.
This could be an issue if you insert year and trying to update by checking current time.
Another thing is, if you insert current time in the current year field, then your update query will never work.
You should use YEAR(CURDATE())
to get the current year.
Use the below query and check.
$query=mysql_query("UPDATE rental_details SET
rental_annual_rent = '$rentalannualrent',
rental_block = '$rentalblock',
rental_street = '$rentalstreet',
rental_area = '$rentalarea',
rental_town = '$rentaltown',
rental_state = '$rentalstate',
rental_pincode = '$rentalpincode',
WHERE email ='$userEmailid' AND currentyear=YEAR(CURDATE())");
EDIT 1 :
It will not insert. Because of the below condition,
if($rentalannualrent!='' &&$rentalblock!='' &&$rentalstreet!='' &&$rentalarea!='' &&$rentaltown!='' &&$rentalstate!='' &&$rentalpincode!='')
{
All these values are required by the insert query. Obviously, all the variables should be well set and it should have some values.
If all these variables have any data, it will always go to UPDATE part. Your INSERT query will never be executed.
Ideally no data in database. so, UPDATE also will not work.
So, check your condition and change the logic.
EDIT 2 :
$userEmailid= $userInfo-> getEmailid();
$result=mysql_query("SELECT * FROM rental_details WHERE email ='$userEmailid' AND currentyear=YEAR(CURDATE())";
$records = mysql_num_rows($result);
if($records > 0) {
// UPDATE query
} else {
// INSERT query
}
PS: Do not use Mysql_. It is deprecated. Please switch to mysqli_ (or) PDO.