2013-04-28 69 views
-4

我一直在試圖更新我的數據庫,但它更新,它顯示了這樣的MySQL的PHP​​查詢不更新

通知的ID錯誤是成功的:用C player_id:未定義的變量\ WAMP \ WWW \ Potifolio \ update_ac.php第4行

成功:波紋管是我的代碼請幫助:

me.php

<?php 
include_once("connect.php"); 
$query = "SELECT * FROM players"; 
$result = mysql_query($query,$db); 
?> 
<table><tr> 
<td>Name</td> 
<td>Surname</td> 
<td>Positon</td> 
<td>Email</td> 
<td>Passowrd</td> 
<td>Email</td> 
<td>Action</td> 
</tr> 
<?php 
while($row = mysql_fetch_assoc($result)){ 
?> 
<tr> 
<td><?php echo $row['name'];?></td> 
<td><?php echo $row['surname'];?></td> 
<td><?php echo $row['position'];?></td> 
<td><?php echo $row['password'];?></td> 
<td><?php echo $row['username'];?></td> 
<td><?php echo $row['name'];?></td> 
<td ><a href= "update1.php?player_id=<?php echo $row['player_id'];?>">Update</a></td> 
</tr> 
<?php 
} 
?> 

update1.php

<?php 
include_once("connect.php"); 
$player_id = $_GET['player_id']; 
$query = "SELECT * FROM players where player_id ='$player_id'"; 
$result = mysql_query($query,$db); 
$row = mysql_fetch_assoc($result); 

?> 

<form name = "form1"action="update_ac.php" method ="post"> 
<input type = "text" name="name" value="<?php echo $row['name'];?>"> 
<input type = "hidden" name = "player_id" ID = "player_id" value="<?php echo $row['player_id'];?>" > 
<input type = "submit" name="submit" value="submit"> 
</form> 

update_ac.php

<?php 
include_once('connect.php'); 
$name= $_POST['name']; 
$sql="UPDATE players SET name='$name' WHERE player_id = '$player_id'"; 
$result = mysql_query($sql); 
if($result){ 
echo "Success"; 
}else { 
echo "Error"; 
} 
?> 
+4

您正在使用[**的**過時的數據庫API(http://stackoverflow.com/q/12859942/19068),並應使用[現代替代品](http://php.net/manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com/)**,現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin 2013-04-28 08:41:53

+3

您需要幫助才能瞭解錯誤消息嗎?在文件'update_ac.php'的#4行,你正在使用一個名爲'$ player_id'的變量,它從無處跳出。這就是你應該首先解決的問題。 – 2013-04-28 08:46:59

+0

重複的http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – 2013-04-28 08:48:40

回答

1

查詢更改這個

$sql="UPDATE players SET name='$name' WHERE player_id = '$_POST['player_id']'"; 

或類似這樣的

$player_id = $_POST['player_id']; 
$sql="UPDATE players SET name='$name' WHERE player_id = '$player_id'"; 

你是不是assig在你的情況下將$ _POST ['player_id']的真實價值轉換爲變量$ player_id。

所以完整的代碼應該是這樣的

<?php 
include_once('connect.php'); 
if (isset($_POST['submit'])) { 
    $name= $_POST['name']; 
    $player_id = $_POST['player_id']; 
    $sql="UPDATE players SET name='$name' WHERE player_id = '$player_id'"; 
    $result = mysql_query($sql); 
    if($result){ 
     echo "Success"; 
    }else { 
     echo "Error"; 
    } 
} 
?> 
+0

你的答案是足夠的,保存我們在社區需要像你這樣的人,而不是那些剛剛拜訪我們投票給我們的人,感謝下一位 – humphrey 2013-04-28 08:47:13

+1

乾杯@humphrey :)。只要你覺得解決了問題,請選擇答案。 :) – 2013-04-28 08:55:40

+1

我做了我只是等待7分鐘通過 – humphrey 2013-04-28 09:09:30