简体   繁体   English

“ where”子句中的未知列“”

[英]Unknown column ' ' in 'where clause

I have been scouring the stack-overflow site and tried most of the similar questions that had been asked/answered, but unfortunately none of the solutions have worked for me so far. 我一直在搜索堆栈溢出站点,并尝试过询问/回答过的大多数类似问题,但不幸的是,到目前为止,没有一种解决方案对我有用。 I am trying to have a list that is being populated by an sql database (which is working) and then once selecting the item, hitting the "generate" button and grabbing the data from the table and posting ONLY the selected data into the table. 我正在尝试获取一个由sql数据库填充的列表(正在运行),然后选择该项目后,单击“生成”按钮,然后从表中获取数据,仅将所选数据发布到表中。 Previously I had an issue where the tables where being populated with ALL the data from the weapons list. 以前,我有一个问题,其中的表中填充了武器列表中的所有数据。 After working on it some I have am now getting the error "Unknown column (whichever weapon name I choose) in where clause 107 (which I am assuming is my line number). Any suggestions? 经过一些努力之后,我现在在where子句107(我假设是我的行号)中收到错误消息“ Unknown列(我选择的武器名称)”。有什么建议吗?

Here is the entirety of the form from populating the list from the database to trying to select the weapon from the list. 这是从数据库中填充列表到尝试从列表中选择武器的完整表格。

<form action="#" method="post">
    <table class="table">
        <thead>
            Martial Weapon Name
        </thead>
        <tr>
            <th>
                <select name="Choosen">
                    <?php 
                    echo'<option>Select Weapon</option>';
                    //Check if at least one row is found
                    if($result->num_rows >0){
                        //Loop through results
                        while($row = $result->fetch_assoc()){
                            //Display weapon info
                            $output = $row['weapon_name'];
                            echo '<option>'.$output.'</option>';
                        }
                    }
                    ?>
                </select>
            </th>
        </tr>
    </table>
    <input class="btn btn-default" type="submit" name="submit" value="Generate">
    <h3>
        Weapon
    </h3>
    <table class="table table-striped">
        <tr>
            <th>
                Weapon Name
            </th>
            <th>
                Weapon Type
            </th>
            <th>
                Damage
            </th>
        </tr>
        <?php
        if (isset($_POST['submit'])) {
            $selected_weapon = $_POST['Choosen'];
            $choose = "SELECT
            weapon_types_martial.id,
            weapon_types_martial.weapon_name,
            weapon_types_martial.weapon_type,
            weapon_types_martial.weapon_damage 
            FROM weapon_types_martial WHERE weapon_types_martial.weapon_name = " . $selected_weapon;
            $result = $mysqli->query($choose) or die($mysqli->error . __LINE__);
            foreach ($result->fetch_assoc() as $item) {
                //Display weapon
                $show = '<tr>';
                $show .= '<td>' . $item['weapon_name'] . '</td>';
                $show .= '<td>' . $item['weapon_type'] . '</td>';
                $show .= '<td>' . $item['weapon_damage'] . '</td>';
                $show .= '</tr>';
                //Echo output
                echo $show;
            }
        }
        ?>
    </table>
</form>

And lastly here is the screenshot of what I am getting 最后是我得到的屏幕截图

条款错误

You need to place quotes in your SQL query. 您需要在SQL查询中放置引号。
Try: 尝试:

$choose = "SELECT
  weapon_types_martial.id,
  weapon_types_martial.weapon_name,
  weapon_types_martial.weapon_type,
  weapon_types_martial.weapon_damage 
  FROM weapon_types_martial WHERE weapon_types_martial.weapon_name = '" . $selected_weapon . "'";

Word of advice, this query is very unsafe. 忠告,此查询非常不安全。 I suggest using a framework or library for database queries. 我建议使用框架或库进行数据库查询。

1)You will not get data in $_POST['choosen'] As you haven't pass value in dropdown(select) 1)您不会在$ _POST ['choosen']中获取数据,因为您没有在下拉列表中传递值(选择)

2)In your database table may be field weapon_name is varchar so you have to pass it into single quote. 2)在您的数据库表中,字段field字段的weapon_name为varchar,因此您必须将其传递给单引号。

Change your code as below: 如下更改代码:

<form action="#" method="post">
    <table class="table">
        <thead>
            Martial Weapon Name
        </thead>
        <tr>
            <th>
                <select name="Choosen">
                    <?php 
                    echo '<option>Select Weapon</option>';
                    //Check if at least one row is found
                    if($result->num_rows >0){
                        //Loop through results
                        while($row = $result->fetch_assoc()){
                            //Display weapon info
                            $output = $row['weapon_name'];
                            echo '<option value="'.$output.'">'.$output.'</option>';  //<--------------change here
                        }
                    }
                    ?>
                </select>
            </th>
        </tr>
    </table>
    <input class="btn btn-default" type="submit" name="submit" value="Generate">
    <h3>
        Weapon
    </h3>
    <table class="table table-striped">
        <tr>
            <th>
                Weapon Name
            </th>
            <th>
                Weapon Type
            </th>
            <th>
                Damage
            </th>
        </tr>
        <?php
        if (isset($_POST['submit'])) {
            $selected_weapon = $_POST['Choosen'];
            $choose = "SELECT
            id,
            weapon_name,
            weapon_type,
            weapon_damage 
            FROM weapon_types_martial WHERE weapon_name = '$selected_weapon'"; //<--------------change here
            $result = $mysqli->query($choose) or die($mysqli->error . __LINE__);
            if ($result->num_rows > 0) {
            while($item = $result->fetch_assoc()) {
                //Display weapon
                $show = '<tr>';
                $show .= '<td>' . $item['weapon_name'] . '</td>';
                $show .= '<td>' . $item['weapon_type'] . '</td>';
                $show .= '<td>' . $item['weapon_damage'] . '</td>';
                $show .= '</tr>';
                //Echo output
                echo $show;
            }
           }
           else
          {
             echo "No data found";
          }
        }
        ?>
    </table>
</form>

You need to give value inside single quotes, 您需要在单引号内给出值,

$choose = "SELECT
  weapon_types_martial.id,
  weapon_types_martial.weapon_name,
  weapon_types_martial.weapon_type,
  weapon_types_martial.weapon_damage 
  FROM weapon_types_martial
  WHERE weapon_types_martial.weapon_name = '" . $selected_weapon ."'";
$choose = "SELECT id, weapon_name, weapon_type, weapon_damage FROM weapon_types_martial WHERE weapon_name = '".$selected_weapon."'";

要么

$choose = "SELECT weapon_types_martial.id, weapon_types_martial.weapon_name, weapon_types_martial.weapon_type, weapon_types_martial.weapon_damage  FROM weapon_types_martial as weapon_types_martial WHERE weapon_types_martial.weapon_name = '" . $selected_weapon ."'";

The options in your select element don't have a value=.. attribute, meaning that nothing is present in your query. select元素中的options没有value=..属性,这意味着查询中不存在任何内容。

So $_POST['Choosen'] will = '' . 所以, $_POST['Choosen']将= ''

Not to mention that you're trying to pass a string, so your query will need to be wrapped in ' : 更不用说您要传递字符串,因此您的查询将需要用'包裹:

$choose = "SELECT id, weapon_name, weapon_type, weapon_damage FROM weapon_types_martial WHERE weapon_name = '".$selected_weapon."'";

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

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