使用php计算中的变量更新mysql数据库表

时间:2018-07-15 20:09:24

标签: php mysql sql-update

我正在使用php进行添加,我想将输出更新为数据库中的一列。

这就是我所拥有的:

$api = "https://blockchain.info/ticker";

$json = file_get_contents($api);
$data = json_decode($json, TRUE);
$rate = $data["USD"]["sell"];
$symbol = $data["USD"]["symbol"];
$query = $db->query("SELECT * FROM bit_buysell WHERE status='1' ORDER BY id");
if($query->num_rows>0) {
    while($row = $query->fetch_assoc()) {
        echo ''.$row[name].' '.$row[currency].''.$row[price].'';
    }
}

echo "<br><center> <b>1 BTC = " . $rate . $symbol . "</b></center></div>";
?></li>

<?php
$query = $db->query("SELECT * FROM bit_buysell WHERE id='1' ORDER BY id");
if($query->num_rows>0) {
    while($row = $query->fetch_assoc()) {
        echo ''.$row[prce].'';

        $first_number = $rate ;
        $second_number = $row[price];
        $sum_totalbuy = $first_number * $second_number;

        print ($sum_totalbuy);
    }
}      

我已经能够成功添加比特币价格行情和我的自定义值,该值会在print ($sum_totalbuy);

中返回我想要的结果

因此,我想通过更改自动引用$sum_totalbuy

的表来更新数据库

这就是我所做的-

mysql_query("UPDATE bit_rates SET id='38' WHERE rate_from='$sum_totalsell'");

它没有用,我也有,我不知道该怎么做

这是通过仪表板手动更新它的代码,因为我必须输入rate from和rate to:

<div class="card-body">
<form action="" method="POST">
    <div class="form-group">
        <label>List with exchange rates</label>
        <select class="form-control" name="rid">
            <?php
            $query = $db->query("SELECT * FROM bit_rates ORDER BY id");
            if($query->num_rows>0) {
                while($row = $query->fetch_assoc()) {
                    echo '<option value="'.$row[id].'">'.gatewayinfo($row[gateway_from],"name").' '.gatewayinfo($row[gateway_from],"currency").' ('.$row[rate_from].' '.gatewayinfo($row[gateway_from],"currency").') = '.gatewayinfo($row[gateway_to],"name").' '.gatewayinfo($row[gateway_to],"currency").' ('.$row[rate_to].' '.gatewayinfo($row[gateway_to],"currency").')</option>';
                }
            } else {
                echo '<option>No gateways</option>';
            }
            ?>
        </select>
    </div>
    <div class="form-group">
        <label>New exchange rate</label>
        <div class="input-group">
            <span class="input-group-addon" id="basic-addon1">Rate from</span>
            <input type="text" class="form-control" name="rate_from" placeholder="1" aria-describedby="basic-addon1">
            <span class="input-group-addon" id="basic-addon1">=&nbsp;&nbsp; Rate to</span>
            <input type="text" class="form-control" name="rate_to" placeholder="0.95" aria-describedby="basic-addon1">
        </div>
    </div>
    <button type="submit" class="btn btn-primary" name="btn_update_rate"><i class="fa fa-check"></i> Update</button>
</form>

请帮助我修复它。$sum_totalbuy

我需要将要更新的print ($sum_totalbuy);存储在rate_from中,我将处理rate_to $sum_totalbuy

1 个答案:

答案 0 :(得分:0)

我终于开始工作了,这就是我所做的。 我必须完全重新连接到数据库,然后进行查询更新。

 <?php
$server = "host"; 
$user = "user"; 
$pass = "pass"; 
$db = "db";
// Create connection  
$conn = mysqli_connect($server, $user, $pass, $db);    
// Check connection    
if (!$conn) { 
    die("Could not connect: " . mysqli_connect_error());
}
$sql = "UPDATE bit_rates SET rate_from='$sum_totalsell' WHERE id=38";  
    if (mysqli_query($conn, $sql)) {
    echo "Sell Rate Update successful.";
} 
$sql = "UPDATE bit_rates SET rate_to='$sum_totalbuy' WHERE id=37";  
    if (mysqli_query($conn, $sql)) {
    echo "Buy Rate Update successful.";
} 
else {
    echo "Could not update: " . mysqli_error($conn);
}
mysqli_close($conn);  
?>

那直接更新了我的sql表

谢谢大家的时间

我在这里https://www.bitdegree.org/learn/mysql-update-syntax/

找到了帮助