更新语句不适用于我的PHP代码

时间:2013-09-27 03:47:29

标签: php mysql sql sql-update

我正在为学校项目创建一个在线移动重装网站。但似乎我的更新声明不起作用......但我认为它写得正确,所以我无法弄清楚我的代码中有什么问题。

我的代码所做的是...当用户尝试重新加载他的帐户时,必须更新balance_table中的余额列。

这是我的代码: reloadaccount.php

<?php
if(!isset($_POST['send']))
{
    $usernumber = "09392316162";
    mysql_connect("localhost", "root", "");
    mysql_select_db("eloadretailer_db");
    $query0 = "SELECT * FROM `balance_table` WHERE `user_number` = $usernumber";
    $result = mysql_query($query0);
    echo"<form method='post' action='".$_SERVER['PHP_SELF']."'>";
    while($row = mysql_fetch_array($result))
    {
        $balance = $row['balance'];
    }
    echo"<input type='hidden' name='balance' value='".$row['balance']."' />";
    echo"<p>&nbsp;</p>";
    echo"<p>";
    echo"<label for='creditcardtype'>Credit Card Type: </label>";
    echo"<select name='creditcardtype' id='creditcardtype'>";
    echo"<option selected='selected'>Choose Credit Card</option>";
    echo"<option>Master Card</option>";
    echo"<option>Visa</option>";
    echo"</select>&nbsp;&nbsp;<img src='pics/creditcard.jpg' >";
    echo"</p>";
    echo"<p>";
    echo"<label for='creditcardnumber'>Credit Card Number: </label>";
    echo"<input type='text' name='creditcardnumber' id='creditcardnumber' />";
    echo"</p>";
    echo"<p>Expiration Date: &nbsp;&nbsp;";
    echo"<label for='month'>Month: </label>&nbsp;";
    echo"<select name='month' id='month'>";
    echo"<option selected='selected'>Month</option>";
    echo"<option>January</option>";
    echo"<option>February</option>";
    echo"<option>March</option>";
    echo"<option>April</option>";
    echo"<option>May</option>";
    echo"</select>";
    echo"<label for='year'>Year: </label>&nbsp;";
    echo"<select name='year' id='year'>";
    echo"<option selected='selected'>Year</option>";
    echo"<option>2020</option>";
    echo"<option>2019</option>";
    echo"<option>2018</option>";
    echo"<option>2017</option>";
    echo"<option>2016</option>";
    echo"<option>2015</option>";
    echo"<option>2014</option>";
    echo"<option>2013</option>";
    echo"</select>";
    echo"</p>";
    echo"<p>";
    echo"<label for='billingaddress'>Billing Address: </label>";
    echo"<textarea name='billingaddress' cols='40' id='billingaddress'>Billing Name, Street Address, City</textarea>";
    echo"</p>";
    echo"<p>";
    echo"<label for='zipcode'>Zip Code: </label>";
    echo"<input type='text' name='zipcode' id='zipcode' />";
    echo"</p>";
    echo"<p>";
    echo"<label>Amount to Load:";
    echo"<select name='amount' id='amount'>";
    echo"<option>Amount</option>";
    echo"<option value='100'>100</option>";
    echo"<option value='500'>500</option>";
    echo"<option value='1000'>1000</option>";
    echo"<option value='2000'>2000</option>";
    echo"<option value='3000'>3000</option>";
    echo"<option value='5000'>5000</option>";
    echo"</select>";
    echo"</label>";
    echo"</p>";
    echo"<p>&nbsp;</p>";
    echo"<p>";
    echo"<input type='submit' name='send' id='sendtransaction' value='Send Transaction' />";
    echo"</p>";
    echo"</form>";
}
else
{
    $connection = mysql_pconnect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("eloadretailer_db") or die(mysql_error());

    $usernumber = "09392316162"; // its data type is varchar because if its int, 0 cannot be place before number 9

    $balance = mysql_real_escape_string($_POST['balance']);

    $amount = mysql_real_escape_string( $_POST['amount']);
    $totalbalance =  $balance+$amount;
    echo"balance = $totalbalance"; // $totalbalance is working...

    //the data type of balance is int 
        //and user_number is varchar                        
    $query = "UPDATE `balance_table` SET `balance`= $totalbalance WHERE `user_number` = $usernumber"; // this code is not working.. i dont know why...

    mysql_query($query)or mysql_error();
}
?>

2 个答案:

答案 0 :(得分:0)

首先打印查询并在查询分析器上运行,如果查询在分析器上运行,则查询是正确的,我认为在哪里条件

     $query = "UPDATE `balance_table` 
           SET 
             `balance`= $totalbalance 
           WHERE 
             `user_number` = '".$_SESSION['cart']['session_id']."'";

答案 1 :(得分:0)

由于user_numbervarchar,您应将其括在引号中。

您的查询应该是

$query = "UPDATE `balance_table` SET `balance`= $totalbalance 
          WHERE `user_number` = '".$usernumber."'";
相关问题