【问题标题】:php mysql undefined indexphp mysql 未定义索引
【发布时间】:2013-08-08 10:53:13
【问题描述】:

我有三个php页面,第一页是在字段中输入用户详细信息第二页是在数据库中插入用户详细信息第三页是显示已发送到数据库的用户详细信息,

一切正常,除了在第三页没有出现用户详细信息并且我收到以下错误消息:

注意:未定义的变量:用户 注意:未定义变量:电子邮件 注意:未定义变量:通过

这是我的代码:

first_page.php

<form action="second_page.php" method="post" >
User Name:  
<input  type="text" name="username" >
User Email
<input  type="text" name="useremail" >
Password:  
<input  type="text" name="password" >
<input type="submit"  name="submit" >
</form>

second_page.php

if (isset($_POST['submit'])) 
{
$user= $_POST['username'];
$email = $_POST['useremail'];
$pass= $_POST['password']; 

mysql_query("INSERT INTO table (username, useremail, password) VALUES  ('$user','$emai','$pass');
header("location: third_page.php");
exit;

third_page.php

$user= $_POST['username'];
$email = $_POST['useremail'];
$pass= $_POST['password']; 

echo ' the user name: '.$user;
echo ' the user email:.'$email;
echo 'the user password:.'$pass;

【问题讨论】:

  • 第二页只有一个 POST 请求,POST 数据只在那里可用。您不能转发或重定向包含 POST 数据的请求。
  • 您只是重定向到third_page.php,而没有发送任何参数值。

标签: php mysql indexing undefined


【解决方案1】:
$user_id = mysql_insert_id();
header("location: third_page.php?id=$user_id");

anf ftech data from datbase

【讨论】:

    【解决方案2】:

    在third_page.php 中,您的$_POST 数据为空(您没有在second_page.php 中发送表单)。

    尝试从third_page.php 的数据库中获取数据!

    还有一点,在second_page.php$username$useremail 中没有定义(你定义了$user$pass)。

    【讨论】:

      【解决方案3】:

      替换以下代码:

      mysql_query("INSERT INTO table (username, useremail, email) VALUES  ('$username','$useremail','$email');
      header("location: third_page.php");
      

      使用以下代码:

      mysql_query("INSERT INTO table (username, useremail, email) VALUES  ('$user','$email','$email');
      header("location: third_page.php?username=$user&useremail=$email&password=$pass");
      

      并且还使用 $_REQUEST 更改 $_POST:

      $user= $_REQUEST['username'];
      $email = $_REQUEST['useremail'];
      $pass= $_REQUEST['password'];
      

      但是通过 URL 发送数据并不安全,因此在 url 中发送 id 并在 third.php 页面上获取数据是一种很好的做法。

      【讨论】:

        【解决方案4】:

        这是我的代码,查询数据库然后打印结果

        <?php
        $con=mysqli_connect("localhost","name","pass","db");
        // Check connection
        if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
        
        $result = mysqli_query($con,"SELECT * your where statement");
        $job = array();
        while($row = mysqli_fetch_array($result)){
            $job[] = $row;
        }
        
        
        
        <div id="content">
                <div class="priority">
                <?php foreach($job as $item): ?>
                    <div class="jobpost">
        
                    Job: <?php echo $item['job'] ?><br />
                    Type: <?php echo $item['type'] ?><br />
                    Comments: <?php echo $item['comments'] ?><br />
                    Timescale: <?php echo $item['timescale'] ?><br />
                    Date: <?php echo $item['date'] ?><br />
                    <a href="<?php echo $item['id'] ?>">Go to this job</a>
        
        
                </div>
                <?php endforeach; ?>
            </div>
        

        【讨论】:

          【解决方案5】:

          您只是在third_page.php 中进行了一些分配,但没有将实际输入发送到该页面。如果您希望能够访问该页面中的变量,您将查询数据库并直接获取结果,或者使用会话。

          方法一:

          second_page.php 中,添加以下内容:

          $_SESSION['username'] = $user;
          $_SESSION['useremail'] = $email;
          $_SESSION['password'] = $pass;
          

          third_page.php 中,添加以下内容:

          $user = $_SESSION['username'];
          $email = $_SESSION['useremail'];
          $pass = $_SESSION['password'];
          

          现在您可以访问third_page.php 中的变量。

          方法二:

          $query = mysql_query("SELECT username, useremail, password FROM table") or die (mysql_error());
          
          while ($row = mysql_fetch_array($query)) {
              $user = $row['username'];
              $email = $row['useremail'];
              $pass = $row['password'];
          }
          

          重要:

          希望这会有所帮助!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-26
            • 1970-01-01
            • 1970-01-01
            • 2015-03-24
            • 2018-01-25
            • 1970-01-01
            相关资源
            最近更新 更多