douyouyi8878 2016-03-02 07:12
浏览 92
已采纳

如何在angularjs中插入和更新查询

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);
    }
}
  • 写回答

1条回答 默认 最新

  • doushadu0901 2016-03-02 07:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端