开发者

mysql does multiple inserts on one query

开发者 https://www.devze.com 2023-03-26 21:33 出处:网络
I have a command on a开发者_StackOverflow中文版 web page, which calls a controller class which executes a model function of inserting some data into a database, its pretty straight forward except ever

I have a command on a开发者_StackOverflow中文版 web page, which calls a controller class which executes a model function of inserting some data into a database, its pretty straight forward except everytime I press the button the data is being inserted three times instead of just once.

function run_no_submenu_job($job_name,$client_name,$server_id)
{
    $this->Jobmodel->insert_client_history($userID,$firstname,$lastname,$clientname,$data['time']);
}

and the model function looks like this:

$query=$this->db->query("INSERT INTO clientaccesshistory (jobid, clientid, firstname, lastname, clientname, menu, submenu, starttime) VALUES ('$time','$userID','$firstname','$lastname','$clientname','Monitor/Verify', '$this->job_name',current_timestamp() )");

When i look in the database the information has been added three times. Sometimes the time stamp is 1 second apart, and sometimes not. Sometimes this behaviour does not happen and it just inserts it once as expected. Not sure if this is a connection with mysql since its on my local pc.

thanks


Are you debouncing the button press? In other words, is the function being called on "Button Down" or on "Button Click"?

The difference is that a "Click" will only happen once, but depending on how long you take to click the button it will register as "Down" for multiple update cycles, thus calling the insert multiple times. If you don't have access to a mouse click event, try calling the function only when the button's value changes from up to down.

If the case above, the HTML would be

<input type="button" onClick=run_no_submenu_job>

You can't provide arguments to functions this way, however. It's been a long time since I used Javascript so I am unsure exactly how to solve your problem. Hopefully someone else can expand on this issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消