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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题