【问题标题】:PHP - Populating Display Input Data with MySQL Data from TablePHP - 使用表中的 MySQL 数据填充显示输入数据
【发布时间】:2013-04-25 17:14:36
【问题描述】:

我正在尝试使用我的 MySQL 数据库表interest_type 中的描述填充结果

我的私人功能是

    private function Get_Interests_Types_From_DB()
    {
        $sql = "SELECT 
                    InterestID,
                    InterestDescription
                FROM
                    interest_type";

        $result = mysqli_query($this->Con, $sql);

        while($row = mysqli_fetch_array($result))
        {
            $arrayResult[] = $row;
        }

        return ($arrayResult);
    }

我尝试使用此函数的代码区域位于同一类的公共函数中。表单中的值是数字。我试图将 $interests 变量与表中的 InterestID 联系起来,然后打印 InterestDescription,而不是 $interests 的值。

function ProcessRegistrationForm()
    {   
        $fname = $_POST['firstname'];
        $lname = $_POST['lastname'];
        $email = $_POST['email'];
        $gender = $_POST['gender'];
        $interests = $_POST['interests'];

        if(!isset($_POST['firstname']) || !isset($_POST['lastname']) || !isset($_POST['email']) ||
            ($_POST['firstname']) == '' || ($_POST['lastname']) == '' || ($_POST['email']) == '')
        {
            echo("Please enter your first / last name and email.");
        }   
        else
        {
            echo("<h2>Results</h2>");
            echo("<div id='results'>");
            echo $fname; 
            echo("<br />");
            echo $lname; 
            echo("<br />");
            echo $email;
            echo("<br />");
            echo $gender;
            echo("<br />");

            $interestDescription = $this->Get_Interests_Types_From_DB();

            foreach($interests as $likes)
            {
                if($likes == $interestDescription['InterestID'])
                    echo $$interestDescription['InterestDescription'] . "<br />";
            }
            echo("<p style='font-weight: bold;'>Your data has been saved! We will contact you soon!</p>");
            echo("</div>");
        }

        $myClub = new Club("localhost","A340User","Pass123Word","info_club");

        $date = date("Y/m/d");

        $sql="INSERT INTO member
                    (`FirstName`,`LastName`,`Gender`,`Email`,`MemberSince`)
                VALUES
                    ('$fname','$lname','$gender','$email','$date');";

        $result = mysqli_query($this->Con,$sql);
        /*if($result == true) 
        {
            echo "Successful Insert<br />";
        }
        else
        {
            echo "Error Inserting class" . mysqli_error($this->Con) ." <br />";
        }*/

        for($i = 0; $i < sizeof($interests); $i++)
        {
            $interest = $interests[$i];

            $sql="INSERT INTO member_interests
                        (`Email`,`InterestID`)
                    VALUES
                        ('$email',$interest);";

            $result = mysqli_query($this->Con,$sql);
        }

我收到:Notice: Undefined index: InterestID in C:\xampp-portable\htdocs\A340\Assign5\Assign5_Club_Membership_Class.php on line 287

如何打印 InterestDescription 而不是 $interests 的值?

【问题讨论】:

  • 似乎 $interestDescription['InterestID'] 不存在...用 print_r() 检查 $interestDescription。
  • 我仍然收到Notice: Undefined index: InterestID。我不确定这是为什么。我在同一个类中运行的前一个类函数中使用了相同的信息。我可以向您保证,InterestID 确实存在。为什么会这样?

标签: php mysql forms class function


【解决方案1】:

那里:

while($row = mysqli_fetch_array($result))
{
   $arrayResult[] = $row;
}

现在$arrayResult,实际上是您的$interestDescription,包含如下内容:

$interestDescription[0]["InterestID"] 
$interestDescription[1]["InterestID"]
...

而不是$interestDescription[InterestID"]

更新:

也许你需要这样的东西(假设 $interests 拥有一个 ID 数组)

        $interestDescription = $this->Get_Interests_Types_From_DB();
        foreach($interests as $likes) {
           foreach($interestDescription as $desc) {
              if($likes == $desc['InterestID']) {
                 echo $desc['InterestDescription'] . "<br />";
              }
           }
        }

【讨论】:

  • 这是否意味着我应该使用for循环而不是foreach,所以我可以说$interestDescription[$i]['InterestID']
  • 好的,所以我将代码更改为:$interestDescription = $this-&gt;Get_Interests_Types_From_DB(); for($i = 0; $i &lt; sizeof($interestDescription); $i++) { if($interests = $interestDescription[$i]['InterestID']) echo($interestDescription[$i]['InterestDescription'] . "&lt;br /&gt;"); },它所做的只是使用表中的所有 InterestDescription 数据填充显示,而不仅仅是 $interests 中的值。因此,如果用户选择了值为 2 和 5 的兴趣,我仍然会打印数据库中的所有六个描述
  • 完美。谢谢你。我现在明白了。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多