PHP-更新时添加值

时间:2019-01-30 05:55:43

标签: php mysqli

更新值时如何添加值?例如,例如,每次我更新值时,它是递增还是相加?

当前输出-我的当前输出是我正在更新值,但是每次我更新值时,它只是在更改值而不添加它。例如,例如,我更新值1000,它将更改为1000,而在我再次将值更新为200之后,该值将更改为200,但应为{ {1}}

问题-我的问题是我每次更新值时都无法添加值

预期输出-每次更新值时,我都需要添加或使其递增

这是我的代码和数据库更新查询语句

1200

这是触发更新值的按钮

  <?php

  //CONNECTION
  $connection =mysqli_connect("localhost","root","");
  $db = mysqli_select_db($connection,'inflightapp');

  //END CONNECTION

  //SUBMIT THE INPUT AND SEARCH THE LIKE VALUE
  if (isset($_POST['search'])) {
  $txtCode = $_POST['id'];
  //GET THE INPUT BELOW AMOUNT

  //REMOVE THE SPECIAL CHARACTER QR CODE: | TO READ AND SEARCH FOR SIMILAR VALUE
  $hashCase =  $txtCode;
  $str = ltrim( strstr($hashCase,":"),":");
  $strFinalString = trim(substr($str,0,strpos($str, "|")));
  //END OF REMOVING SPECIAL CHAR


  //QUERY TO SEARCH THE VALUE
  $query = "SELECT * FROM scratch_cards WHERE code LIKE '%$strFinalString%' ";
  $query_run = mysqli_query($connection,$query);
  //END

  //FETCH THE DATA INTO MY DATABASE 'scratch_cards'
  while($row = mysqli_fetch_array($query_run))
  {
   $txtValue = $row['amount'];
  ?>
      <input type="hidden" name="amount[<?php echo $row['id'] ?>]" value="<?php echo $row['amount'] ?>">
      <h2><i><u>You have received: <?php echo '<b style="color:#90EE90">'.$row['amount'].'</b>'  ?> Coins</u></i></h2>
  <?php



    //QUERY TO UPDATE THE VALUE
  $query2 = "UPDATE shopusers SET ewallet = '$txtValue' WHERE id = 12";
  $query_run2 = mysqli_query($connection,$query2);
  //END
  }

   //END OF FETCHING DATA
  }

  ?>

3 个答案:

答案 0 :(得分:1)

$query2 = "UPDATE shopusers SET ewallet = ewallet + " . (0 + $txtValue) . " WHERE id = 12";

实际上,更好的写法是:

$stmt = $db->prepare("UPDATE shopusers SET ewallet = ewallet + ? WHERE id = 12");
$stmt->bind_param("i", $txtValue);
$stmt->execute();

我之前给您的内容应该可以使用,但是您应该对所有内容使用prepare来确保要传递给数据库的数据的格式正确。为了防止SQL注入。

mysqli::prepare

答案 1 :(得分:0)

您需要做的就是

//QUERY TO UPDATE THE VALUE

$query2 = "UPDATE shopusers SET ewallet = '$txtValue' WHERE id = 12";

答案 2 :(得分:0)

您必须尝试此更新查询。

$query2 = "UPDATE shopusers SET ewallet = ewallet+'$txtValue' WHERE id = 12";

这可能对您有最大帮助