任何Echo值如何直接存储在数据库中

时间:2015-04-14 06:15:42

标签: php mysql

我为办公室工作创建了一个简单的业务管理设置。首先,我从数据库中获取所有信息。除数量输入外,一切顺利。

在这个所有支付做计算总和并做Echo。但我希望这个echo值检测另一个表中数据库中的自动存储。

那么如何完成呢?

这是程序代码。

<tbody>
<?php

    $respectivestud = mysql_query("select * from client where client_id");
    $i = 1;
    while($r_client = mysql_fetch_array($respectivestud))
    {
        ?>
        <tr>
            <td><?php echo $i; ?></td>  
            <td><?php echo $r_client['name']; ?></td>
            <td><?php echo $r_client['project_name']; ?></td>
            <td><?php echo $r_client['cost']; ?></td>
            <td><?php echo $r_client['payment_1']; ?></td>
            <td><?php echo $r_client['payment_2']; ?></td>
            <td><?php echo $r_client['payment_3']; ?></td>
            <td><?php echo $r_client['payment_4']; ?></td>
            <td><?php echo $r_client['payment_5']; ?></td>
            <?php $total = $r_client['payment_1'] + $r_client['payment_2'] + $r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
            <td> <?php echo $total ; ?></td>
            <?php $remain = $r_client['cost'] - $total;?>
            <td><?php echo $remain; ?></td>             
        **<td>**

3 个答案:

答案 0 :(得分:0)

首先用mysqli_ *或Pdo语句替换mysql_ *。因为mysql_ *现在已经被删除了。这是引用mysqlipdo 将剩余的ammount存储在变量中。然后在更新查询中使用此变量。喜欢

<td><?php echo $remaining_amount=$remain; ?></td>

然后在db

中更新
    $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
    $sql = "UPDATE MyGuests SET tablename='".$remaining_amount."' WHERE id=yprimarykey";

    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }

答案 1 :(得分:0)

 <td><?php echo $r_client['payment_5']; ?></td>
 <?php $total = $r_client['payment_1'] + $r_client['payment_2'] +$r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
 <td> $sql=mysqli_query($connection,"insert into `tablename`(`columnname`)values('".$total."')");</td>

无论您要插入什么值,都可以这样做...

答案 2 :(得分:0)

请确保您编写有效的PHP代码。您没有关闭,而您没有正确关闭标签。这应该是正确的。

<tbody>
<?php

    $respectivestud = mysql_query("select * from client where client_id");
    $i = 1;
    while($r_client = mysql_fetch_array($respectivestud))
    {
        ?>
        <tr>
            <td><?php echo $i; ?></td>  
            <td><?php echo $r_client['name']; ?></td>
            <td><?php echo $r_client['project_name']; ?></td>
            <td><?php echo $r_client['cost']; ?></td>
            <td><?php echo $r_client['payment_1']; ?></td>
            <td><?php echo $r_client['payment_2']; ?></td>
            <td><?php echo $r_client['payment_3']; ?></td>
            <td><?php echo $r_client['payment_4']; ?></td>
            <td><?php echo $r_client['payment_5']; ?></td>
            <?php $total = $r_client['payment_1'] + $r_client['payment_2'] + $r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
            <td> <?php echo $total ; ?></td>
            <?php $remain = $r_client['cost'] - $total;?>
            <td><?php echo $remain; ?></td>             
        </tr>
<?php
    }
?>
</tbody>

要保存数据,您可以使用:

$sql=mysqli_query($connection,"insert into `tablename`(`columnname`)values('".$total."')");