2012-10-24 46 views
0

在我的代碼中,我試圖用來自表單中可修改文本字段的值更新我的數據庫。但是,當查詢運行時,它會擦除​​與用戶正試圖編輯的記錄相關的所有字段(將它們變爲「」)。任何人都可以看到爲什麼呢?使用PHP將信息更新到MySQL數據庫中

<?php 
$dbuser = 'test'; 
$dbpass = 'test'; 
$host = '127.0.0.1'; 
$db = 'test'; 
mysql_connect($host, $dbuser, $dbpass) or die(mysql_error()); 
mysql_select_db($db) or die(mysql_error()); 

$result = mysql_query("SELECT * from characters"); 
$id = $_REQUEST['combo']; 
while($row = mysql_fetch_array($result)) 
{ 
    if($id == $row['_Key']) 
    { 
     echo "<form action=\"webpagetests.php\" method=\'post\'>"; 
     echo "<strong>Player ID:</strong> " . $row['_Key'] . "</br>"; 
     echo "<strong>Steam Name:</strong> " . $row['_SteamName'] . "</br>"; 
     echo "<strong>Steam ID:</strong>  " . $row['_SteamID'] . "</br></br>"; 

     echo "Name: </br><input name = \"name\" type=\"text\" size=\"25\" value=\"" . $row['_Name'] . "\"></br></br>"; 
     echo "Cash: </br><input name = \"cash\" type=\"text\" size=\"25\" value=\"" . $row['_Cash'] . "\"></br></br>"; 
     echo "Flags: </br><input name = \"flags\" type=\"text\" size=\"25\" value=\"" . $row['_Flags'] . "\"></br></br>"; 
     echo "Gender:</br> <input name = \"gender\" type=\"text\" size=\"25\" value=\"" . $row['_Gender'] . "\"></br></br>"; 
     echo "Model:</br> <input name = \"model\" type=\"text\" size=\"50\" value=\"" . $row['_Model'] . "\"></br></br>"; 
     echo "Faction: </br><input name = \"faction\" type=\"text\" size=\"25\" value=\"" . $row['_Faction'] . "\"></br></br></br>"; 
     echo "Recognised Names: </br><input name = \"names\" type=\"text\" size=\"50\" value=\"" . $row['_RecognisedNames'] . "\"</br>"; 
     echo "<input name=\"submit2\" type=\"submit\" value=\"Update\" />"; 
     echo "</form>"; 
    } 
} 
if (isset($_POST['submit2'])) 
{ 
    //$name = mysql_real_escape_string(htmlspecialchars($_POST['name'])); 
    $name = "Test"; 
    $cash = (int)$_POST['cash']; 
    $flags = mysql_real_escape_string(htmlspecialchars($_POST['flags'])); 
    $gender = mysql_real_escape_string(htmlspecialchars($_POST['gender'])); 
    $model = mysql_real_escape_string(htmlspecialchars($_POST['model'])); 
    $faction = mysql_real_escape_string(htmlspecialchars($_POST['faction'])); 
    $names = mysql_real_escape_string(htmlspecialchars($_POST['names'])); 

    mysql_query("UPDATE `test`.`characters` SET `_Name` = '$name', 
    `_Cash` = '$cash', 
    `_Model` = '$model', 
    `_Flags` = '$flags', 
    `_Gender` = '$gender', 
    `_Faction` = '$faction', 
    `_RecognisedNames` = '$names' 
    WHERE `characters`.`_Key` ='$id'") or die(mysql_error()); 
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=webpagetest.php">'; 
}?> 
+1

不要使用'mysql _ *()'函數 - http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-function-in-php - 這不是你的問題情況,但應該指出。此外,包括您用來發布數據的實​​際HTML表單將有所幫助。 – Crontab

+0

您是否檢查變量是否實際填充了預期值?這樣你就可以知道這是你的表單/表單處理還是查詢的問題。 – mboldt

+0

你沒有設置變量$ id ... – superUntitled

回答

0

1)你有沒有在那裏你的SELECT語句子句,所以整個表從數據庫轉移到你的PHP腳本。地址:

$id = mysql_real_escape_string($id); 
$result = mysql_query("select * from characters where _Key = '$id'"); 

以提高性能和帶寬利用率。更好的是,使用mysqli,您可以在其中使用預準備語句。

2)我沒有看到在哪裏設置了$id,當表單返回時,所以這可能也是一個問題。

+1

請不要發佈暴露初學者到SQL注入的代碼片段!向他們展示如何通過將他們引導到最新的解決方案,如[PDO](http://www.php.net/manual/en/intro.pdo.php)或[mysqli _ *](http: //ch2.php.net/manual/en/intro.mysqli.php)。 – CodeZombie

+0

@ ZombieHunter感謝您指出。 –