0

So I have a data table that allows people to see certain information, although when I try to call $data (username) it states it is undefined Undefined variable: data on line 80

I have tried to change the name data as well as names in the information such as name and the table name but I have gotten no success

<?php
ob_start();
include('auth.php');
include('admin-auth.php');
include('header.php');
 if(isset($_GET['uid']) && $_GET['uid'] != ''){
    $query = "SELECT * FROM players WHERE uid='".$_GET['uid']."'";
 $res = $con->query($query);
 if($res->num_rows != 0){
 $data = $res->fetch_assoc();
 }else{
  header("Location: all-users-game.php");
 } 
 }
 if(isset($_POST['uid'])){
  $name = $_REQUEST['name'];
  $pid = $_REQUEST['pid'];
  $cash = $_REQUEST['cash'];
  $bankacc = $_REQUEST['bankacc'];
  $coplevel = $_REQUEST['coplevel'];
  $mediclevel = $_REQUEST['mediclevel'];
  $query = "UPDATE players SET name = '".$name."', cash = '".$cash."', bankacc = '".$bankacc."', coplevel = '".$coplevel."', mediclevel = '".$mediclevel."', pid = '".$pid."' WHERE uid = '".$_REQUEST['uid']."'";
  $result = $con->query($query);
  if($result){
   echo "<script>alert('Data Updated.')</script>";
   header('location:all-users-game.php');
  }
 }
 
 ?>
<style>
.profile_details textarea {
    width: 100%;
    height: 50px;
    border: 1px solid #fff;
    background: none;
    padding: 0 20px;
    font-size: 18px;
    font-weight: 300;
    color: #fff;
    border-radius: 10px;
    font-family: 'Roboto', sans-serif;
}
.update-edit button { 

background: rgba(88,134,23,1);
background: -moz-linear-gradient(left, rgba(88,134,23,1) 0%, rgba(139,190,72,1) 53%, rgba(88,134,23,1) 100%);
background: -webkit-gradient(left top, right top, color-stop(0%, rgba(88,134,23,1)), color-stop(53%, rgba(139,190,72,1)), color-stop(100%, rgba(88,134,23,1)));
background: -webkit-linear-gradient(left, rgba(88,134,23,1) 0%, rgba(139,190,72,1) 53%, rgba(88,134,23,1) 100%);
background: -o-linear-gradient(left, rgba(88,134,23,1) 0%, rgba(139,190,72,1) 53%, rgba(88,134,23,1) 100%);
background: -ms-linear-gradient(left, rgba(88,134,23,1) 0%, rgba(139,190,72,1) 53%, rgba(88,134,23,1) 100%);
background: linear-gradient(to right, rgba(88,134,23,1) 0%, rgba(139,190,72,1) 53%, rgba(88,134,23,1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#588617', endColorstr='#588617', GradientType=1 );

padding: 7px 40px;
    border: 0px;
    font-size: 20px;
    text-align: center;
    color: #fff;
    margin: 0 10px;
    border-radius: 10px;
    text-transform: uppercase;
    display: inline-block;
    font-family: 'Audiowide', cursive;
}
</style>
<div class="title">
     <h1>Update User</h1>
  
    </div>
    <div class="profile_details">
    <div class="profile_info">
     <h1>Update User Detail</h1>
  <form method="post" >
        <div class="row">

            <div class="col-md-12">
            <div class="pro_row">
                 <label>Username</label>
                    <input type="text" name="username" placeholder="Username" readonly value="<?php echo $data['name']; ?>">
    <input type="hidden" name="uid"
    value="<?php echo $data['uid']; ?>">
                </div>
            </div>

I understand there is some stuff in there I do not need, this is also not the entire page.

  • 1
    Your script is at risk of [SQL Injection Attack](//stackoverflow.com/questions/60174) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](//stackoverflow.com/questions/5741187) Use [prepared parameterized statements](https://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – John Conde Mar 15 '18 at 00:17
  • _"is undefined, but it is"_ - how do you know?? There are several possibilities where `$data` can be undefined.... You have 3 conditions to be met before defining `$data`. One of those is not true, obviously. – Jeff Mar 15 '18 at 00:18
  • Well, your `$data` variable is set inside of an `if` conditional. Are you sure that conditional was true? – lurker Mar 15 '18 at 00:18
  • 2
    You also will have an issue with your redirect – John Conde Mar 15 '18 at 00:19

0 Answers0