如何更改数据库中的2列?

时间:2017-06-25 05:18:37

标签: php sql database

所以我一直试图找到一种方法来使用相同的信息更新1个数据库中的2列。例如,我有一个名为payout的列和一个名为totalpayout的列,我有一个更新支付的回发链接,但我试图想办法更新totalpayout。因此,当http://example.com/postback.php?affsub={theirid}&payout=10被访问时,它会更新支付和总支付。下面是更新支付的代码,我不知道如何使用它来更新totalpayout。

<?php

define("MYSQL_HOST", "localhost");
define("MYSQL_PORT", "3306");
define("MYSQL_DB", "db");
define("MYSQL_TABLE", "tbl");
define("MYSQL_USER", "user");
define("MYSQL_PASS", "pass");
$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
if ($mysqli->connect_errno) 
{
  echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$affsub =            $_GET['affsub'];
$payout =           $_GET['payout'];


if (!($stmt = $mysqli->prepare("UPDATE ".MYSQL_DB.".".MYSQL_TABLE." SET payout=payout+(?) WHERE affsub=(?) "))) 
{
  echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$stmt->bind_param('ds', $payout, $affsub );
if (!$stmt->execute()) 
{
  echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else
{
  printf("%d Row updated, added ".$payout." to locker ".$affsub." .\n", mysqli_stmt_affected_rows($stmt));
}
?>

1 个答案:

答案 0 :(得分:0)

您可以通过在SET语句之间添加逗号来更新两列。所以你可以使用SET payout=payout+(?), totalpayout=totalpayout+(?)

注意: 我假设totalpayout增加了$payout。根据您的算法对其进行修改,以增加totalpayout

<?php

define("MYSQL_HOST", "localhost");
define("MYSQL_PORT", "3306");
define("MYSQL_DB", "db");
define("MYSQL_TABLE", "tbl");
define("MYSQL_USER", "user");
define("MYSQL_PASS", "pass");
$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
if ($mysqli->connect_errno) 
{
  echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$affsub =            $_GET['affsub'];
$payout =           $_GET['payout'];


if (!($stmt = $mysqli->prepare("UPDATE ".MYSQL_DB.".".MYSQL_TABLE." SET payout=payout+(?), totalpayout=totalpayout+(?) WHERE affsub=(?) "))) 
{
  echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$stmt->bind_param('dds', $payout, $payout, $affsub );
if (!$stmt->execute()) 
{
  echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else
{
  printf("%d Row updated, added ".$payout." to locker ".$affsub." .\n", mysqli_stmt_affected_rows($stmt));
}
?>