-4

I have been trying to update my database but it does it update and it shows a success with an id error like this

Notice: Undefined variable: player_id in C:\wamp\www\Potifolio\update_ac.php on line 4

Success: bellow are my code please help :

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";
}
?>
Antony
  • 14,670
  • 10
  • 42
  • 73
Humphrey
  • 2,473
  • 3
  • 27
  • 38
  • 4
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 28 '13 at 08:41
  • 3
    Do you need help to understand the error message? At line #4 of file `update_ac.php` you are using a variable called `$player_id` that pops up from nowhere. That's what you should fix first. – Álvaro González Apr 28 '13 at 08:46
  • Duplicate of http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – Álvaro González Apr 28 '13 at 08:48
  • @Antony the edit is appreciated – Humphrey Apr 28 '13 at 08:48
  • @ÁlvaroG.Vicario great man – Humphrey Apr 28 '13 at 08:49

1 Answers1

1

Change your query to this

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

or like this

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

you are not assigning the real value of $_POST['player_id'] to the variable $player_id in your case.

So the full code should be like this

<?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";
    }
}
?>
chandresh_cool
  • 11,600
  • 3
  • 27
  • 44