我一直在尝试更新我的数据库,但它会更新并显示成功,ID错误就像这样
注意:未定义的变量:第4行的C:\ wamp \ www \ Potifolio \ update_ac.php中的player_id
成功:下面是我的代码请帮助:
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";
}
?>
答案 0 :(得分: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'";
在你的情况下,你没有将$ _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";
}
}
?>