1

我想用 PHP 和 mySQL 更新一个表。我需要从 mysql 数据库中已经存在的值中添加或减去我选择的任何数字。当我添加到“player_qty”时它可以正常工作,但当我减去/删除时不行。它给了我想要的信息,已经删除了这么多,但是当我检查数据库时,值是相同的——不是删除。

$register_query = "SELECT * FROM players WHERE player_id='$add_player_id' AND event_name = '$event_name'";
$register_check = mysql_query($register_query, $db); 
$register_found_users = mysql_num_rows($register_check);

if($_POST['button'] == "Add") {
    if($register_found_users>0){
    mysql_query("UPDATE players 
    SET  
    player_qty = player_qty + $add_player_qty,
    player_pickedup = '1', 
    event_user = '$event_user'  
    WHERE player_id = '$add_player_id'");
    $msg = $add_player_qty.' more ticket(s) added to '.$add_player_id.'.';
    } else {
    $insertSQL1 = "
    INSERT INTO players 
    (player_id, player_qty, player_adt, player_level, player_pickedup, event_name, event_type, event_end, event_user)
    VALUES 
    ('$add_player_id', '$add_player_qty', NULL, NULL, '1', '$event_name', '$event_type', '$event_end', '$event_user')";
    mysql_select_db($dbname, $db);
    $Result1 = mysql_query($insertSQL1, $db) or die(mysql_error()); 
    $msg = $add_player_id.' added to DB w/ '.$add_player_qty.' ticket(s)'; 
    }
}elseif($_POST['button'] == "Delete") {
    if($register_found_users>0){
    mysql_query("UPDATE players 
    SET  
    player_qty = player_qty - $add_player_qty,
    player_pickedup = '1', 
    event_user = $event_user 
    WHERE player_id = '$add_player_id'");
    $msg = $add_player_qty.' ticket(s) deleted from '.$add_player_id.'.';
    } else {
    $msg = $add_player_id.' not in database. Please add manually.'; 
    }
} else {
    //
}
      <!-- FORM TO ADD PLAYER TO DATABASE IF DOESN'T EXIST -->
      <form action="" method="post" enctype="multipart/form-data" name="form_add_player" id="form_add_player" style="display:<?php echo $css;?>;">

          <label for="add_player_id">PLAYER #: </label>
          <input type="text" name="add_player_id" id="add_player_id" value=""/>

          <label for="add_player_qty">QUANTITY: </label>
          <select name="add_player_qty" id="add_player_qty">
            <option value="1">1</option>
            <option value="2" selected>2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
          </select>

            <input name="event_end" type="hidden" id="event_end" value="<?php echo $row_get_type['event_end'];?>" />
            <input type="submit" name="button" value="Add" class="button">
            <input type="submit" name="button" value="Delete" class="button">          

      </form>
4

2 回答 2

1

在您的减法查询中,您没有设置'您的条目值之一 - 可能这会导致 mysql 错误,因此查询不会被执行。

mysql_query("UPDATE `players` 
 SET  
 `player_qty` = `player_qty` - $add_player_qty,
 `player_pickedup` = '1', 
 `event_user` = '$event_user'
 WHERE `player_id` = '$add_player_id'");
于 2012-05-10T15:14:16.347 回答
0

在所有查询中将 $add_player_qty 替换为 '$add_player_qty'

于 2012-05-10T15:16:27.480 回答