注意:未定义的变量:首先在C:\ xampp \ htdocs \ php-databases \ my-profile.php中的第25行,未定义的初始化变量

时间:2018-07-30 10:46:19

标签: php html

对于我要创建的个人资料页面的所有变量,我有一堆未定义的变量。

我认为我之前已经定义了它们,如下所示。我看过类似的文章,但是初始化变量似乎也不起作用(如您所见,我尝试使用$ first)。我是php的新手,因此将不胜感激:)

<?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>

注册表格:

<?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';
    ?>

3 个答案:

答案 0 :(得分:1)

访问数组项时(如$_POST),访问将尝试直接访问元素,而无需先检查其是否存在。

您需要检查它:

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

如果使用的是框架,请尝试使用其请求对象来获取值。

答案 1 :(得分:0)

您不应该直接使用GLOBALS。尝试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']));
}
?>

另一个问题是,使用mysqli_real_escape_string并不是您所认为的那么安全,并且在服务器端错误配置可能会导致一些问题。您应该尝试使用准备好的语句。

答案 2 :(得分:0)

这是问题所在

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

$ first没有价值

在while循环之前为$ first分配一些值

您要在while循环中声明值,

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

如果while循环失败,则$ first ='';不会被调用,

在您的情况下,其中的代码未执行,

所以

更改代码

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

我不知道这将要做什么:

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

但我将其更改为

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

然后检查$ uid是否不为空,并且仅在$ uid不为空时继续

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']);
   }
}

仅在$ first,$ last,$ city和$ 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 } ?>