【问题标题】:Notice: Undefined variable: first in C:\xampp\htdocs\php-databases\my-profile.php on line 25, undefined initialized variables [duplicate]注意:未定义变量:首先在第 25 行的 C:\xampp\htdocs\php-databases\my-profile.php 中,未定义的初始化变量 [重复]
【发布时间】:2018-07-30 10:46:19
【问题描述】:

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

我以为我之前已经定义了它们,如下所示。我查看了类似的帖子,但初始化变量似乎也不起作用(如您所见,我尝试使用 $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';
    ?>

【问题讨论】:

  • $first = isset($_POST['first'] ) ? $_POST['first'] : ' ';
  • 检查是否设置了 $_POST['first']
  • 请付出更多努力,尝试以可重现的方式呈现您的问题。现在你所显示的甚至没有 25行,所以如果你只是引用一条错误消息说“in [...]\my-profile.php on line 25” 仍然留下一些不清楚的东西。请阅读minimal reproducible example
  • 如果您的代码没有进入while 循环,那么$first 将不会在以后定义。在假设一切正常之前检查您是否确实找到了匹配项。
  • 这是完整的代码吗? $first 在您的代码中没有被调用,请向我们展示您首先调用 $first 的代码。

标签: php html


【解决方案1】:

当访问一个数组项时,(如$_POST),访问将尝试直接访问该元素而不首先检查它是否存在。

你需要检查一下:

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

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

【讨论】:

  • 完成了,得到同样的错误:(
  • 在 OP 使用的现有代码中使用 $_POST['first'] ?? '' 有什么不同?
【解决方案2】:

这是问题,在

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

$first 没有价值

在while循环之前给$first赋值

你在 while 循环中声明值,

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

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

在你的情况下,while里面的代码没有被执行,

所以

更改您的代码

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

【讨论】:

  • 做了一些有趣的事情,我现在没有错误消息,但是,数据库中没有数据显示?它的左边是空白
  • 检查你在 $_POST['user_uid'] 中是否有价值检查我的代码
  • 可能你的 $uid 没有价值
  • 我在数据库中肯定有 $uid 的值,所以我使用了您建议的代码(感谢您的帮助!),并且每次调用它们时都会抛出未定义的变量错误html代码
  • 回显 $uid;在while循环之前
【解决方案3】:

您不应该直接使用 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 并不像您想象的那么安全,并且在服务器端错误配置可能会产生一些问题。您应该尝试使用准备好的语句。

【讨论】:

    猜你喜欢
    • 2014-01-05
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 2015-04-25
    • 1970-01-01
    相关资源
    最近更新 更多