添加值在插入mysql之前

时间:2013-03-30 07:51:05

标签: php mysql

我正在尝试使用从输入框中收集的值添加数据库中列的值。

例如:我目前在一列中有500,我想将500添加到从表单(文本框)中检索到的新值。

这是我的代码

<?php
     if (isset($_SESSION['user'])) {  
$author = $_SESSION['user'];
//echo $author;
// check if posted is sent   

if (isset($_POST['input'])) {
if (empty($_POST['name']) || empty($_POST['quantity'])) {
echo '<h4 class="alert_info">You Have Not Entered Any Value</h4>';
}      
else {
// retrievings foms data and declaring them as functions
$name = $_POST['name'];
$quantity = $_POST['quantity'];  
$amount = $_POST['amount'];
$category = $_POST['theItems'];    


// Allow apostrophe
$name2 = mysql_real_escape_string($name);
$quantity2 = mysql_real_escape_string($quantity);
$amount2 = mysql_real_escape_string($amount);
$category2 =  mysql_real_escape_string($category);
// inserting the posts 
$insert = "REPLACE INTO drinks SET
name='".$name2."', quantity=' ".$quantity2." ', amount=' ".$amount2." ', category=' ".$category2." ', date=CURDATE()";
if (@mysql_query($insert)) {
echo '<h4 class="alert_info">Drinks added</h4>';
} else {
echo 'Error adding event: ' .
mysql_error() . '';
}
} }
?>

3 个答案:

答案 0 :(得分:3)

我觉得你在找这样的东西?

<?php
$val = $_POST['quantity'];
$query = "UPDATE drinks SET quantity= quantity + $val WHERE id={put current id here}";
$result = mysql_query($query);
?>

答案 1 :(得分:1)

使用SELECT查询从列中提取当前值,将值添加到从文本框中收到的值,然后使用新值添加UPDATE

答案 2 :(得分:0)

你可以这样做:

update tableName set columnName = columnName+1