【问题标题】:Insert update won't update in table and in database插入更新不会在表和数据库中更新
【发布时间】:2014-04-27 12:43:23
【问题描述】:

在 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>

process-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);
}
?>

【问题讨论】:

  • 把你的代码放在重要的部分 a : echo "part1" : echo $select_sql."&lt;-sql"; : echo $qry."&lt;-qry"; : echo $new_bal."&lt;-newbal"; 等等。然后向我们展示输出。

标签: php insert sql-update insert-update


【解决方案1】:

你需要回显这个东西

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

检查你在这里得到了什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 2011-02-16
    • 2021-06-22
    • 1970-01-01
    • 2014-07-02
    相关资源
    最近更新 更多