无法更新SQL数据库

时间:2013-07-09 14:49:48

标签: php mysql sql-update

所以我创建了一个编辑个人资料页面,一切都很顺利。但后来我想使用类似的代码来更新我的数据库中的另一个东西。它是一个非常奇怪的bugg,因为当我添加许多需要更新的值时,它拒绝工作。

<?php

session_start();

if(isset($_POST['submit'])){
$Creator = $_SESSION['username'];
$dbName = $_POST['Name'];
$dbBase_damage = $_POST['Base_damage'];
$dbDamage = $_POST['Damage'];
$dbPellets = $_POST['Pellets'];
$dbAttackspeed = $_POST['Attackspeed'];
$dbRS = $_POST['RS(first)'];
$dbRS2 = $_POST['RS(consecutive)'];
$dbLoaded = $_POST['Loaded'];
$dbReserve = $_POST['Reserve'];
$dbCritical = $_POST['Critical'];
$dbAbility = $_POST['Ability'];
$dbPiece_set = $_POST['Piece_set'];
$dbBonus_set = $_POST['Bonus_set'];
$dbDescription = $_POST['Description'];

if ($dbName && $dbBase_damage && $dbDamage && $dbAttackspeed && $dbLoaded && $dbCritical) {

mysql_connect ("localhost", "root", "") or die ("Could not connect");
mysql_select_db("weapons") or die ("Could not connect to the database");    

$wepquery = mysql_query("SELECT * FROM weapon WHERE Creator='$Creator' AND Name='$dbName'") or die ("The query could not be completed, please try again later!");
if (mysql_num_rows($wepquery) != 0) {

mysql_query ("

UPDATE weapon SET Base_Damage='$dbBase_damage', Damage='$dbDamage', 
Pellets='$dbPellets', Attackspeed='$dbAttackspeed', RS(first)='$dbRS', RS(consecutive)='$dbRS2', 
Loaded='$dbLoaded', Reserve='$dbReserve', Critical='$dbCritical', Ability='$dbAbility', Piece_set='$dbPiece_set', 
Bonus_set='$dbBonus_set' Description='$dbDescription' WHERE Creator='$Creator' AND Name='$dbName'

") or die ("Could not update!");
echo "It just werks!";

}else echo ("That username could not be found!");
}else echo ("Something went wrong!"); 
    }
    ?>

当我使用这段代码时,我得到一个错误,'Base_Damage'是未定义的索引。但是当我删除'Base_Damage'时,它根本无法更新。我已经仔细检查了数据库并用它创建了“武器创意”而没有任何问题。但由于某种原因,当我减少需要更新的值的数量时,它工作正常。如果你想知道“$ Creator = $ _SESSION ['username'];”,我会根据从“nav.php”登录的人获取Creator用户名 我的HTML代码:

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0" />
<title>TF2 NUT</title>
<link rel="stylesheet" href="css/style.css" />
</head>

<div id="Container">

<div id="header">
<h1>Welcome to TF2 NUT!</h1>
</div>

<div id="nav">

<?php

include_once "nav.php";

?>

</div>

<div id="Content">
<h2>Update your submissions</h2>
<form action="edit_sub.php" method="POST">
<table>
<tr>
    <td>
    Choose weapon to edit: 
    </td>
    <td>
    <input type="text" name="Name" id="Name" />
    </td>
</tr>
<tr>
    <td>
    Base damage: 
    </td>
    <td>
    <input type="text" name="Base_damage" id="Base_damage" />
    </td>
</tr>
<tr>
    <td>
    Damage: 
    </td>
    <td>
    <input type="text" name="Damage" id="Damage" />
    </td>
</tr>
<tr>
    <td>
    Pellets: 
    </td>
    <td>
    <input type="text" name="Pellets" id="Pellets" />
    </td>
</tr>
<tr>
    <td>
    Attackspeed: 
    </td>
    <td>
    <input type="text" name="Attackspeed" id="Attackspeed" />
    </td>
</tr>
<tr>
    <td>
    Reload speed: 
    </td>
    <td>
    <input type="text" name="RS(first)" id="RS(first)" />
    </td>
</tr>
<tr>
    <td>
    Reload(consecutive): 
    </td>
    <td>
    <input type="text" name="RS(consecutive)" id="RS(consecutive)" />
    </td>
</tr>
<tr>
    <td>
    Loaded: 
    </td>
    <td>
    <input type="text" name="Loaded" id="Loaded" />
    </td>
</tr>
<tr>
    <td>
    Reserve: 
    </td>
    <td>
    <input type="text" name="Reserve" id="Reserve" />
    </td>
</tr>
<tr>
    <td>
    Critical: 
    </td>
    <td>
    <input type="text" name="Critical" id="Critical" />
    </td>
</tr>
<tr>
    <td>
    Ability: 
    </td>
    <td>
    <input type="text" name="Ability" id="Ability" />
    </td>
</tr>
<tr>
    <td>
    Piece set: 
    </td>
    <td>
    <input type="text" name="Piece_set" id="Piece_set" />
    </td>
</tr>
<tr>
    <td>
    Bonus set: 
    </td>
    <td>
    <input type="text" name="Bonus_set" id="Bonus_set" />
    </td>
</tr>
</table>
Description:
<br />
<textarea style="resize: none;" rows="8" cols="40" id="Descripton" name="Description"></textarea>
<br />
<input type='submit' name="submit" value='Update' />
</form>
</div>

<div id="aside">

</div>

<div id="footer">

<h6>Responsible for the stuff on this site is Anton Magnusson</h6>
<h6>If I have done something wrong(I hope not ;_;) just contact me:   anton_peace@hotmail.com</h6>

</div>

</div>

任何人都可以提供帮助? :P

1 个答案:

答案 0 :(得分:2)

我猜你的if语句是不可读的,这就是为什么你不能更新

  • 首先您的名字name="Base_damage"与您的变量索引$dbBase_Damage = $_POST['Base_Damage'];
  • 不同

应该是

    $dbBase_Damage = $_POST['Base_damage']; // with small d

尝试重播这个

     if ($dbName && $dbBase_Damage && $dbDamage && $dbAttackspeed && $dbLoaded && $dbCritical)

修改

在您的查询中

替换此

     RS(first)='$dbRS', RS(consecutive)='$dbRS2'

通过

      `RS(first)`='$dbRS', `RS(consecutive)`='$dbRS2'

<强> EDIT2:

请替换

      or die ("Could not update!");

   or die(mysql_error()); // like that you can see what error or what goes wrong.

<强> EDIT3。

     Bonus_set='$dbBonus_set' , Description=
                             ^^//you missed coma here