插入更新不会在表和数据库中更新

时间:2014-04-27 12:43:24

标签: php insert sql-update insert-update

在PHP中更新时代码运行没有错误但是当我输入一个数量时,它不会在我的表和我的数据库中更新,请帮助。顺便说一句,我在之前的程序中使用了一些代码,因此可能在这个过程中有些变量代码是不合适的。谢谢。

以下是代码:

load.php

<form method="POST" action="process-load.php">
<?php
require_once('connect/connect.php');
$id = mysql_escape_string($_GET['id']);
$sql = 'SELECT * FROM cards WHERE id='.$id.' LIMIT 0, 1';
$qry = mysql_query($sql);
$data = mysql_fetch_array($qry);
$html = '';

    $html .= '<div class="box">';
$html .= '<b> Card #: '.$data['cardno'].'</b><br />';
$html .= '<b>Current Balance: </b>'.$data['balance'].'<br />';
$html .= '<b>Enter Addition Load: </b><input type="text" name="load" size="5" /><br />';
$html .= '<input type="hidden" value="'.$_GET['id'].'" name="id" />';
$html .= '<input type="hidden" value="'.$data['balance'].'" name="bal" />';
$html .= '<input type="submit" value="Submit" name="submit" />';
$html .= '</div>';

echo $html;
?>
</form>

过程load.php

<?php
session_start(); //don't forget to start session or else session will not be red

if(isset($_POST['submit'])) {       
    require_once('connect/connect.php');

    $id = mysql_escape_string($_POST['id']);
    $bal = $_POST['bal'];
    $load = $_POST['load'];
    $select_sql = 'SELECT balance FROM cards WHERE id="'.$id.'" LIMIT 0, 1';
    $qry = mysql_query($select_sql);
    $data = mysql_fetch_array($qry);            
    $new_bal = $data['balance'] + $bal; 
    $sql_update = 'UPDATE cards SET balance="'.mysql_escape_string($new_bal).'" WHERE id="'.$id.'"';
    $qry2 = mysql_query($sql_update);
    $bill = $bal += $load;

    $_SESSION['profit'] += $bill; //add total bill always to your session

    if($qry2) { 
        ?>
        <script>            
            alert('Thank you.\n New Balance: <?php echo $bill; ?>');
            window.location.href = 'index.php?page=show';
        </script>
        <?php
    } else {
        ?>
        <script>
            alert('Failed to load card.';);
            window.location.href = 'index.php?page=show';
        </script>
        <?php
    }
    mysql_close($con);
}
?>

1 个答案:

答案 0 :(得分:0)

你需要回应这件事

echo $sql_update = 'UPDATE cards SET balance="'.mysql_escape_string($new_bal).'" WHERE id="'.$id.'"';

检查你在这里得到什么。