2011-03-04 40 views
0

我試圖在數據庫中插入特定的值(小刀和毯子),但根本沒有插入到DB /表中。另外,我想在下面的表格中顯示插入的值,但這並不奏效。它依賴於插入它顯示在桌子上。我確定,因爲我通過phpmyAdmin插入了一個值,並將其顯示在表格中。請,我需要修復插入方面。將特定值插入數據庫並將其顯示在表格上?

插入代碼/錯誤處理

<?php 
if (isset($_POST['Collect'])) { 
if(($_POST['Object'])!= "knife" && ($_POST['Object'])!= "blanket") 
{ 
    echo "This isn't among the room objects."; 
}else { 
// this makes sure that all the uses that sign up have their own names 
$sql = "SELECT id FROM objects WHERE object='".mysql_real_escape_string($_POST['Object'])."'"; 
$query = mysql_query($sql) or die(mysql_error()); 
$m_count = mysql_num_rows($query); 

if($m_count >= "1"){ 
    echo 'This object has already been taken.!'; 
    } else{ 
    $sql="INSERT INTO objects (object) 
VALUES 
('$_POST[Object]')"; 

echo "".$_POST['object']." ADDED"; 
} 
} 
} 

?> 

表,還要加上額外的PHP代碼

<p> 
<form method="post"> 
</form> 
Pick Object: <input name="Object" type="text" /> 
<input class="auto-style1" name="Collect" type="submit" value="Collect" /> 
</p> 

<table width="50%" border="2" cellspacing="1" cellpadding="0"> 
     <tr align="center"> 
     <td colspan="3">Player&#39;s Object</td> 
     </tr> 
     <tr align="center"> 
     <td>ID</td> 
     <td>Object</td> 
     </tr> 
     <? 
$result = mysql_query("SELECT * FROM objects") or die(mysql_error()); 
// keeps getting the next row until there are no more to get 
    while($row = mysql_fetch_array($result)) { 
// Print out the contents of each row into a table?> 
     <tr> 
     <td><label for="<?php echo $row['id']; ?>"><?php 
    $name2=$row['id']; 
    echo "$name2"; ?> 
    </label></td> 
     <td><? echo $row['object'] ?></td> 
     </tr> 
     <?php }// while loop ?> 
    </table> 

</body> 

回答

-1

您的SQL語法是錯誤的。您應該更改:

INSERT INTO objects SET id = '', object = '".$_POST['Object']."' 

INSERT INTO objects (id, object) VALUES ('', '".$_POST['Object']."' 

如果你希望你的插件,以同時更換,可能是那裏使用REPLACE而不是插入任何價值。

+1

「SET field = value」語句工作得很好,實際上... – Tsadiq 2011-03-04 15:51:02

+0

謝謝。我修正了這一點,但它仍然不起作用。請檢查錯誤處理如果問題來自 – Kelvin 2011-03-04 15:56:42

0
if(($_POST['Object'])!= knife || ($_POST['Object'])!= blanket) 

THES值刀和毯子是字符串。所以,你可能需要使用他們周圍引號將其定義爲串,或PHP不會理解;)

+0

非常感謝你 – Kelvin 2011-03-04 15:58:59

0

如果對象的主鍵是ID,並將其設置爲自動遞增

$sql = "INSERT INTO objects SET id = '', object = '".$_POST['Object']."'"; 

嘗試

$sql= "INSERT INTO objects(object) VALUES ('".$_POST['Object'].")'; 

,你也許應該將逃跑中有太多

0

您插入查詢,也不正確。 (',')。$ _ POST ['Object']。「')」;

和該代碼

if(($_POST['Object'])!= "knife" || ($_POST['Object'])!= "blanket") 
{ 
    echo "This isn't among the room objects."; 
} 

就一定會執行對象的值是刀或毯,因爲可變可以有一個值。您必須使用

if(($_POST['Object'])!= "knife" && ($_POST['Object'])!= "blanket") 
    { 
     echo "This isn't among the room objects."; 
    } 
+0

它仍然不起作用 – Kelvin 2011-03-04 16:03:26

+0

@Kelvin:更新。 – Gaurav 2011-03-04 16:12:27

相關問題