记录插入,但不更新mysql

时间:2018-04-12 02:08:01

标签: php mysql

我在尝试更新数据库时遇到问题。如果它不存在我可以插入,但如果存在则不会更新记录。我认为没有找到表格的当前值。有什么想法吗?

<?php
function add_log($input_output, $l_sales, $l_enroll, $l_offers)
{
    global $database;
    $date           =   date('m-d-Y');
    $l_sales        =   safety_filter($l_sales);
    $l_enroll       =   safety_filter($l_enroll);
    $l_offers       =   safety_filter($l_offers);
    $cusersup       =   get_the_current_user('u_manager');
    $cuseropm       =   get_the_current_user('u_opsmanager');
    $cuserid        =   get_the_current_user('id');
    $cuser          =   get_the_current_user('user_name');

    if($input_output == 'input')
    {
        $query_call = mysql_query("SELECT 1 FROM $database->log WHERE l_date='$date' AND l_user_name='$cuser'");
        if(mysql_num_rows($query_call) > 0)
        {
            while($list_calls = mysql_fetch_assoc($query_call))
            {
                $old_calls = $list_calls['l_calls'];
                $old_sales = $list_calls['l_sales'];
                $old_enroll = $list_calls['l_enroll'];
                $old_offers = $list_calls['l_offers'];
            }


            $update = mysql_query("UPDATE $database->log SET
            l_call=[$old_calls] + [1],
            l_sales=[$old_sales] + [$l_sales],
            l_enroll=[$old_enroll] + [$l_enroll],
            l_offers=[$old_offers] + [$l_offers]
            WHERE l_date='$date' AND l
            _user_name='$cuser'");
            if(mysql_affected_rows() > 0){return true;  }
            else { if($update == true){ return true; } else { return false; } }
        }
        else
        {

            mysql_query("INSERT INTO $database->log (l_date, l_user_name, l_calls, l_sales, l_enroll, l_offers) VALUES ('$date', '$cuser', '1', '$l_sales', '$l_enroll', '$l_offers')");
            if(mysql_affected_rows() > 0){return true; }
            else{ return false; }
        }   
    }
}
    ?>

1 个答案:

答案 0 :(得分:0)

我认为问题在于你选择1(&#34; SELECT 1 FROM ...&#34;),所以你永远不会从表中得到任何值(只是数字&#34; 1&#34)。这告诉你有什么东西,但实际上并没有返回任何值。

尝试选择所有字段(&#34; SELECT * FROM ...&#34;),或者至少列出进行更新所需的字段。