简体   繁体   English

显示MySQL数据 <select>失败

[英]Displaying MySQL data in <select> failing

I am simply trying to display an html <select> drop down that displays options from MySQL database rows. 我只是试图显示一个HTML <select>下拉列表,该下拉列表显示MySQL数据库行中的选项。 As far as I can tell this should be working. 据我所知这应该工作。 However, it is not displaying anything in the drop down. 但是,它在下拉菜单中未显示任何内容。 the campaigns table definitely has data in the campaignid and name columns. campaigns表中的campaignidname列中肯定有数据。 Not sure why it isn't display anything. 不知道为什么它什么都不显示。 Please help. 请帮忙。

the form that should display name 应显示name的表格

<?php
//include db connect
  include ("db_con.php");



   //campaign change form function
  function changeCampaign() {
    //set variables
    $query = mysqli_query($con,"SELECT * FROM campaigns");
    //echo form
    echo '<table border="1">
          <form action="functions/campaign_change.php" name="campaignChange" method="post">
            <tr>
              <td><select name="campaignList">';
                    //loop campaigns
                    while ($row = mysqli_fetch_array($query)) {
                      echo "<option value='" . $row['campaignid'] . "'>" . $row['name'] . "</option>";
                    }
        echo '</select></td>
            </tr>
            <tr>
              <td><input type="submit" value="Load" /></td>
            </tr>
          </form>
        </table>';
}
?>

And then I am simply calling the changeCampaign(); 然后我只是调用changeCampaign(); function, which IS displaying the form with <select> just no content in it. 函数,即显示带有<select>的表单,但其中没有任何内容。

The following code was added to show a successful script adding to the same table 添加了以下代码,以显示成功添加到同一表中的脚本

here is the form to create a campaign 这是创建campaign的形式

//campaign creation form function
  function createCampaign() {
    echo '<table border="1">
          <form action="functions/campaign_create.php" name="campaignCreate" method="post">
            <tr>
              <td><input type="text" name="name" placeholder="Enter Campaign Name" required /></td>
            </tr>
            <tr>
              <td><center><input type="submit" name="Create" /></center></td>
            </tr>
          </form>
          </table>';
}

campaign_create.php campaign_create.php

<?php
//include db connect
  include ("db_con.php");

//start session
  session_start();

//set variable names
  $username = $_SESSION['username'];
  $campaignname = addslashes($_POST['name']);

//validate campaign name length
  if ((strlen($campaignname) < 6) || (strlen($campaignname) > 55)) {
    echo 'Campaign name must be between 6 and 55 characters - Please go back and try again.';
    exit;
  }

//create new campaign
  $query = mysqli_query($con, "INSERT INTO campaigns (creator, name) VALUES ('".$username."', '".$campaignname."')");
  if ($query) {
    echo 'Campaign created successfully!';
    header( "refresh:2;url=../index.php" );
  } else {
    echo 'There was an unknown error in creating the campaign - Please go back and try again.';
  }

?>

PS - Yes, campaignid is a row in the table, and does have data in it. PS-是的, campaignid是表中的一行,并且确实有数据。

You need to echo a close select tag. 您需要回显关闭选择标签。 Just add this line after your loop and you'll be fine 只需在循环后添加此行,就可以了

echo '</select>';

Also try changing the first line of your loop to this: 另外,尝试将循环的第一行更改为此:

while ($row = mysqli_fetch_array($con, $query)) {

First, your <select> HTML tag is never closed with a </select> . 首先,您的<select> HTML标记永远不会用</select>关闭。

Second, the $con variable is not usable from within the changeCampaign function scope. 其次,在changeCampaign函数范围内, $con变量不可用。 Add global $con; 添加global $con; to the beginning of the function to fix it. 到该函数的开头进行修复。

Additionally, I prefer to jump in and out of PHP blocks with this kind of stuff. 另外,我更喜欢用这种东西跳入PHP块。 Consider this code: 考虑以下代码:

<?php
//include db connect
include ("db_con.php");

//campaign change form function
    function changeCampaign() {
    global $con; // we need the connection data in here.
    //set variables
    $query = mysqli_query($con,"SELECT * FROM campaigns");
?>
<table border="1">
    <form action="functions/campaign_change.php" name="campaignChange" method="post">
        <tr>
            <td><select name="campaignList">';
<?php
                    //loop that is NOT displaying the name
                    while ($row = mysqli_fetch_array($query)) {
                        echo "<option value='" . $row['campaignid'] . "'>" . $row['name'] . "</option>";
                    }
?>
            </select></td>
        </tr>
        <tr>
            <td><input type="submit" value="Load" /></td>
        </tr>
        </form>
    </table>';
<?php
}
?>

In this, you have HTML code that is outputted just as if it had been echo ed, but it is much nicer. 在这种情况下,您将输出HTML代码,就像被echo ,但是它要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM