简体   繁体   English

提交具有相同输入字段的表单(与上次相同)时,如何在数据库中增加值

[英]How to increment a value in database when the form with same input field (as last time) is submitted

I want a value (not the the id value BTW) to increment when the user submits the same value in input field. 当用户在输入字段中提交相同的值时,我希望增加一个值(而不是id值BTW)。

For example lets say the value is in "number" column in db table and each time a user submits the form with lets suppose "hello" in input field the integer value in "number" column increments by a specific value. 例如,假设该值位于db表的“ number”列中,并且每次用户提交带有let的表单时,假设在输入字段中的“ hello”中,“ number”列中的整数值将增加特定值。

You have to use trigger functions for this task, because in table there possible only one autoincrement key 您必须为此任务使用触发功能,因为在表中可能只有一个自动增量键

If you use postgres you can use 如果您使用postgres,则可以使用

create or replace function increment_version() returns trigger as $$
declare 
    new_version integer;
begin
    select max(version) into new_version from YOU_TABLE_NAME where CONDITIONS_YOU_NEED;
    new.version = new_version + 1;
    return new;

create trigger increment_version after insert on YOUR_TABLE_NAME
for each row execute increment_version();

Also you can use rules to complete this task 您也可以使用规则来完成此任务

create rule increment_version as on insert to YOUR_TABLE
do update YOUR_TABLE set version=versino+1 where id = new.id;

But note, that in case of error trigger will give you an exception, but rule - not 但请注意,如果发生错误触发器,则会给您一个例外,但是规则-不

rules in pg https://www.postgresql.org/docs/8.2/static/rules-update.html triggers in pg https://www.postgresql.org/docs/9.6/static/plpgsql-trigger.html 在皮克规则https://www.postgresql.org/docs/8.2/static/rules-update.html触发PG https://www.postgresql.org/docs/9.6/static/plpgsql-trigger.html

If you use mysql you can also use triggers but the syntax is a bit different https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html 如果您使用mysql,也可以使用触发器,但是语法略有不同https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html

create trigger version after insert on YOUR_TABLE for each row begin
begin
    select max(version) into new_version from YOU_TABLE_NAME where CONDITIONS_YOU_NEED; 
    set new.version = new_version;
end;

I found the code. 我找到了代码。 This is in PHP 5.(something). 这在PHP 5中。
Here is the code: 这是代码:

if(isset($_POST["Form input"])) {

$input=$_POST["input"];
$ip = getenv("REMOTE_ADDR");
$date=date("d M Y -- H");
$qry_get="select * from `table` where col = '".$input."'";
$qry_get_run=mysql_query($qry_get);
if ($qry_get_run) {
  $ret=mysql_num_rows($qry_get_run);
  if ($ret==0) {


    $qry_ins="INSERT INTO `table` (`col 1`, `col 2`, `col 3`) VALUES ('val1', 'val2', 'val3')";
    $qry_ins_run=mysql_query($qry_ins);
    if ($qry_ins_run) {
      echo "success message";
    }else{
      echo mysql_error();
    }
  }else{
     while ($get=mysql_fetch_assoc($qry_get_run)) {
        $val2=$get['col 2'];
        $id=$get['id'];



        $up_val=$val2+3;


        $qry = "update table_name set `col 2`='".$up_val."' where id='".$id."' ";
        $qry_run=mysql_query($qry);

     }
   }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM