0

I have a bunch of undefined variables, for all the variables of a profile page I'm trying to create.

I thought i had defined them previously, as seen below. I looked on similar posts, but initializing the variables does not seem to be working either(I tried with $first as you can see). I'm a newcomer to php, so any help would be greatly appreciated :)

<?php
include_once 'Header.php';
?>


<?php
$uid = (isset($conn, $_POST['user_uid']) ? $_POST['user_uid'] : '');
$result = mysqli_query($conn, "SELECT * FROM users where user_uid='$uid'");  
while($row = mysqli_fetch_array($result))  
    { 
    $first= "";
    $first = $_POST['first'] ?? '';
    $last= mysqli_real_escape_string($conn, $_POST['last']);
    $city= mysqli_real_escape_string($conn, $_POST['city']);
    $country= mysqli_real_escape_string($conn, $_POST['country']);
    }
?>


<?php
include_once 'Footer.php';
?>

<table width="398" border="0" align="center" cellpadding="0">
<tr>
<td height="26" colspan="2">Your Profile Information </td>
<td><div align="right"><a href="index.php">logout</a></div></td>
</tr>
<tr>
<td width="129" rowspan="5"><img src="<?php echo $picture ?>" width="129" 
height="129" alt="no image found"/></td>
<td width="82" valign="top"><div align="left">FirstName:</div></td>
<td width="165" valign="top"><?php echo $first ?></td>
 </tr>
 <tr>
<td valign="top"><div align="left">LastName:</div></td>
<td valign="top"><?php echo $last ?></td>
 </tr>
<tr>
<td valign="top"><div align="left">City:</div></td>
<td valign="top"><?php echo $city ?></td>
</tr>
<tr>
<td valign="top"><div align="left">Country:</div></td>
<td valign="top"><?php echo $country ?></td>
</tr>
 </table>
 <p align="center"><a href="index.php"></a></p>

Signup form:

<?php
include_once 'Header.php';
?>

 <section class="main-container"> 
<div class="main-wrapper">
    <h2>Sign Up</h2>
    <form class="signup-form" action="includes/signup-inc.php" method="POST">
    <div class="mainbox">
    <div class="btncontainer">
    <input id="radbtn1" type="radio" name="type" value="Guide" checked><br>
    <label for="radbtn1"><span class="radio">Client</span></label>
    </div>
    </div>
    <div class="mainbox">
    <div class="btncontainer">
    <input id="radbtn2" type="radio" name="type" value="Trainer"><br>
    <label for="radbtn2"><span class="radio">Trainer</span></label>
    </div>
    </div>
        <input type="text" name="first" placeholder="Firstname">
        <input type="text" name="last" placeholder="Lastname">
        <input type="text" name="email" placeholder="E-mail">
        <input type="text" name="uid" placeholder="Username">
    <input type="password" name="pwd" placeholder="Password">
    <input type="text" name="street" placeholder="Street(not visible)">
    <input type="text" name="postcode" placeholder="Postcode(not visible)">
    <input type="text" name="city" placeholder="City(not visible)">
    <input type="text" name="region" placeholder="Region">
    <input type="text" name="country" placeholder="Country">
    <input type="text" name="phonenumber" placeholder="Phone number(not 
    visible)">

        <button type="submit" name="submit">Sign Up!</button>
   </form>

   </div>
   </section>

   <?php
    include_once 'Footer.php';
    ?>
akemedis
  • 414
  • 2
  • 6
  • 17
  • $first = isset($_POST['first'] ) ? $_POST['first'] : ' '; – Devsi Odedra Jul 30 '18 at 10:48
  • check if $_POST['first'] is set – kkica Jul 30 '18 at 10:49
  • Please put a bit more effort in trying to present your problem in a _reproducible_ way. Right now what you have shown does not even _have_ 25 lines, so if you just quote an error message saying _“in [...]\my-profile.php on line 25”_ that still leaves stuff rather unclear. Please go read [mcve]. – CBroe Jul 30 '18 at 10:50
  • 1
    If your code doesn't go into the `while` loop then `$first` won't be defined later. Check that you actually find a match before assuming everything is OK. – Nigel Ren Jul 30 '18 at 10:50
  • Is this a full code? $first is not called anywhere in your code, show us the code where you call $first first. – Sugumar Venkatesan Jul 30 '18 at 10:52
  • The whole code as shown has several logical issues ... like filling $uid based on `isset($conn, $_POST['user_uid'])`, but then proceeding with a database query in the next line regardless of that variable is now properly filled or even whether a database connection exists in the first place; the only value you are actually dynamically inserting into the query does _not_ get secured against SQL injection, but instead `mysqli_real_escape_string` is used later on, – CBroe Jul 30 '18 at 10:56
  • […] on values that don’t seem to be used in a database context any more; use of a while loop that will either overwrite values if it runs multiple times, or would be rather pointless to begin with if at most one result record is to be expected ... – CBroe Jul 30 '18 at 10:56

3 Answers3

1

When accessing an array item, (as is $_POST), the access will try to directly access the element without first checking if it exists.

You need to check it:

$first = isset($_POST['first'] ) ? $_POST['first'] : ' ';

If you're using a framework, try using its request object to fetch the value.

bear
  • 11,364
  • 26
  • 77
  • 129
0

You shouldn't use GLOBALS directly. Try https://symfony.com/doc/current/components/http_foundation.html

<?php
$request = Request::createFromGlobals();
$uid = $request->get('user_uid', '');

$result = mysqli_query($conn, "SELECT * FROM users where user_uid='$uid'");  

while($row = mysqli_fetch_array($result))  
{ 
    $first = $request->get('first');
    $last = mysqli_real_escape_string($conn, $request->get('last'));
    $city = mysqli_real_escape_string($conn, $request->get('city'));
    $country = mysqli_real_escape_string($conn, $request->get('country']));
}
?>

Another issue, use of mysqli_real_escape_string isn't that secure as you think and wrongly configured on the server side can create few problems. You should try to use prepared statement.

kallosz
  • 521
  • 3
  • 10
0

this is the problem, in

<td width="165" valign="top"><?php echo $first ?></td>

$first don't have value

Assign $first some value before while loop

You are declaring the value inside while loop,

while($row = mysqli_fetch_array($result))  
    { 
    $first= "";
}

If in case while loop fails, $first =''; will not be called,

In your case code inside while is not executed,

So

change your code

$first='';
while($row = mysqli_fetch_array($result)){
    // your code
}

I don't know what this is going to do:

$uid = (isset($conn, $_POST['user_uid']) ? $_POST['user_uid'] : '');

but I will change it to

$uid = isset($conn, $_POST['user_uid']) ? $_POST['user_uid'] : '';

And check whether $uid is not empty, and only if $uid is not empty proceed

if(!empty($uid)){
   $result = mysqli_query($conn, "SELECT * FROM users where user_uid='$uid'");  
   while($row = mysqli_fetch_array($result))  
   { 
       $first = $_POST['first'] ?? '';
       $last= mysqli_real_escape_string($conn, $_POST['last']);
       $city= mysqli_real_escape_string($conn, $_POST['city']);
       $country= mysqli_real_escape_string($conn, $_POST['country']);
   }
}

and print only if there is a value for $first, $last,$city and $country

<?php if(!empty($first)){?>
<tr>
<td width="82" valign="top"><div align="left">FirstName:</div></td>
<td width="165" valign="top"><?php echo $first ?></td>
 </tr>
<?php } if(!empty($last)){?>
 <tr>
<td valign="top"><div align="left">LastName:</div></td>
<td valign="top"><?php echo $last ?></td>
 </tr>
<?php } if(!empty($city)){?>
<tr>
<td valign="top"><div align="left">City:</div></td>
<td valign="top"><?php echo $city ?></td>
</tr>
<?php } if(!empty($country)){?>
<tr>
<td valign="top"><div align="left">Country:</div></td>
<td valign="top"><?php echo $country ?></td>
</tr>
<?php } ?>
Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77