你好,谢谢你抽出时间阅读这篇文章。
我有一个客户数据库,并一直在尝试添加一个忠诚度积分系统,以便使订单总数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);发布于 2019-02-16 21:07:15
我想我会做一个选择,以检查是否已经有一个记录的给定用户名。如果有,获取它的点,然后执行一个更新,在检索到的总数中添加额外的点。如果没有记录,请执行插入。我想写php和查询,但我只是在我的电话atm。希望这能帮你以后检查一下
(更新) Hi Mark除了您可能希望保持它们分开的事实之外,因为订单可能会被取消或发生其他事情(取决于系统的“大小”,这可能是有效的点)。让我们检查一下如何使所要求的功能工作。
请注意,自PHP5.3以来,mysql_db_query已被弃用,并已从PHP7.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);
}这是你能处理的吗?这不是最优雅的解决方案,但在基础方面,这可能是您想要的:)。
https://stackoverflow.com/questions/54727610
复制相似问题