如果ID已存在,则更新?

时间:2018-01-29 14:24:20

标签: php sql

这是我当前的IF,ELSE代码:

        if ($mysqli->query($query)) {
            $success .= "<li>" . $name . "</li>";
            $success_count++;
        } else {
          //echo "Error: " . $query . "<br>" . $mysqli->error;
          $fail .= "<li>" . $name . "<br>" . $mysqli->error . "</li>";
          $fail_count++;
        }

循环代码上方是变量声明:

  $success = $fail = "";
  $success_count = $fail_count = 0;

这是ID(和NAME)声明:

      $id = isset($Row[0]) ? $Row[0] : '';
      $name = isset($Row[1]) ? $Row[1] : '';

如何修改INSERT代码,以便在数据库中已存在ID时,我希望它更新现有数据。我知道它应该是这样的:

    if ($mysqli->query($query)) {
        $success .= "<li>" . $name . "</li>";
        $success_count++;
    }
    else {
      $id == TRUE //this is the code I am confused with
        UPDATE
          //update code here
      else {
      //echo "Error: " . $query . "<br>" . $mysqli->error;
      $fail .= "<li>" . $name . "<br>" . $mysqli->error . "</li>";
      $fail_count++;
    }
   }

0 个答案:

没有答案