私はテーブルebaycashbackからの 'earnings'をテーブルAccountsの 'cashback'の既存の値に追加しようとする初心者プログラマーです。PHPの現在の値を更新して追加するには?
はしたがって、私はアカウントにebaycashbackから40を追加したいです。問題は、私は別のテーブルから現在のキャッシュバックの値が必要なので、私は一緒に2つのテーブルを結合する必要があります。
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(E_ERROR);
try{
//Database connection
$conn = new mysqli("localhost", "xxxxxxxx", "xxxxxxxxx", "xxxxxxxx");
//select earnings, campaign id and cashback
$result = $conn->query("SELECT ec.earnings as add, ec.campaignid, ac.cashback as total
FROM ebaycashback ec, Accounts ac
WHERE ec.campaignid = ac.campaignID
GROUP BY ec.campaignID");
//retrieves and returns the next row assigned to $row
while($row = $result->fetch_array()){
//Add the new cashback into the current balance
$row['add'] + $row['total'] = $totalcashback;
//Update Accounts of the new cashback value
$res = $conn->query("UPDATE Accounts SET cashback = '".$totalcashback."' WHERE campaignid = ".$row['campaignid']);
//Check if $res was executed correctly, doesn't affect the whole code
if (!$res){
$json_out = "[" . json_encode(array("result"=>0)) . "]";
}
else {
$json_out = "[" . json_encode(array("result"=>1)) . "]";
}
}
}catch(Exception $e) {
$json_out = json_encode(array("result"=>0));
echo $json_out;
}
?>
手動でJSONストリングを試してみてください。配列やオブジェクトを作成し、それをあなたが望むもので埋めると、全体で 'json_encode()'となります! – RiggsFolly
分かりやすいコードの字下げが良い考えです。コードを読むのに役立ちます。もっと重要なのは**あなたのコードをデバッグするのに役立ちます** [コーディング標準を見てください](http://www.php-fig.org/psr/psr-2/ )あなた自身の利益のために。あなたはこのコード を数週間/数ヶ月で修正するように頼まれるかもしれません。そして、あなたは私に最後に感謝します。 – RiggsFolly
私はいくつかのコメントをあなたのコメントのために –