【问题标题】:How can I insert and update a number within the same mysql record? [duplicate]如何在同一个 mysql 记录中插入和更新一个数字? [复制]
【发布时间】:2019-07-10 16:13:39
【问题描述】:

您好,感谢您抽出宝贵时间阅读这篇文章

我有一个客户数据库,并且一直在尝试添加一个忠诚度积分系统,以便总订单 x 价值 = 总忠诚度积分

我让它工作,以便在收到订单时用积分更新 table_loyalty 并且工作正常

$points = $row["price"] * 1000;

$insert = mysql_db_query($db, "INSERT into table_loyalty (username, orderno, points) VALUES ('$username', '$this_orderno', '$points')", $connection);
check_mysql($insert); 

但是,我最希望能够做但似乎无法让它发挥作用(尝试了几种不同的方法)是有一个运行总计,以便每个额外的订单建立 $points 而不是添加单独的记录

我不是编码员,我确信这是显而易见的,但希望能提供任何帮助。

我试过了,但没用:

$points = $row["points"];

$newpoints = $row["price"] * 1000;

$update = mysql_db_query($db, "update table_loyalty set points='$points'+'$newpoints' WHERE username='$username'", $connection);
check_mysql($update);

} else {

$insert = mysql_db_query($db, "INSERT into table_loyalty (username, orderno, points) VALUES ('$username', '$this_orderno', '$newpoints')", $connection);
check_mysql($insert);

【问题讨论】:

  • 最好单独记录每个订单的积分并合计,因为任何订单都可以修改、取消、全部或部分退货。
  • @Shadow - 好点....
  • 请将您的代码包装在代码块中。示例:<mycode/>

标签: php mysql insert updates


【解决方案1】:

我想我会做一个选择来检查是否已经有给定用户名的记录。如果有,获取它的分数,然后执行更新,将额外的分数添加到其检索到的总数中。如果没有记录,请插入。我很想写 php 和查询,但我只是在我的手机 atm 上。希望这有助于稍后检查

(更新)嗨,马克,除了您可能希望将它们分开的事实之外,因为订单可能会被取消或发生其他事情(这可能是有效点,具体取决于您的系统有多大。让我们检查如何使请求的功能正常工作。

请注意 mysql_db_query 自 PHP 5.3 以来已被弃用,并已从 PHP 7.0.0 中完全删除。因此,如果您希望能够长期完成这项工作,我建议您使用http://php.net/manual/en/book.mysqli.php

我的建议如下:

$selectQuery = "SELECT points FROM table_loyalty WHERE username=" . $username . ";"

$selectResult = mysql_db_query($db, $selectQuery, $connection);

// if no results could be found
if (mysql_num_rows($selectResult) == 0) {
  //Presuming this is the order price?
  $newpoints = $row["price"] * 1000;

  $insertQuery = "INSERT into table_loyalty (username, orderno, points) VALUES ('$username', '$this_orderno', '$newpoints')";
  $insertResult = mysql_db_query($db, $insertQuery, $connection);
  // I personally have no idea what the check_mysql() function does, I presume its a function of you own making? With that I'm making the assumption that it handles the result in some way? 
  check_mysql($insertResult);

//results have been found
} else {
  $existingPoints = 0;
  while ($row = mysql_fetch_assoc($result)) {
    //There is a catch here. If used in this way you need to make sure that every username only has 1 entry in the table_loyalty. So updating only 1 row per user also means you can ONLY have 1 record per user.
    $existingPoints = $row["points"];
  }

  $newPoints = $row["price"] * 1000;
  $totalPoints = $existingPoints + $newPoints;
  $updateQuery = "update table_loyalty set points='$totalPoints' WHERE username='$username'";

  $update = mysql_db_query($db,updateQuery , $connection);
  check_mysql($update);
}

这是你可以使用的东西吗?这不是最优雅的解决方案,但在基本情况下,这可能是您想要的:)。

【讨论】:

  • 谢谢(我已经用我尝试过的代码更新了我的帖子)
  • 更新了我的答案,希望对您有所帮助
猜你喜欢
  • 1970-01-01
  • 2010-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 2016-05-18
相关资源
最近更新 更多