Home » Php » php – Unknown column 'empresa' in 'where clause'

php – Unknown column 'empresa' in 'where clause'

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a problem with my Query and I have tried multiple ways to make it works and I can’t wrap it around.

Basically I have 2 script, the first script takes all the column in the companies on employees and display it on a select dropdown menu. I have no problems with it as far I can see.

<?php
        $link = mysqli_connect('localhost', 'root', '', 'provadecodb');
        $resultSet = mysqli_query($link, "SELECT empresa FROM contratistas");
        $color1 = "lightblue";
        $color2 = "teal";
        $color = "$color1";
        ?>
        <select name="empresaselect">
        <option value="blank" style="background:lightblue"></option>
        <?php
        while($rows = mysqli_fetch_assoc($resultSet))
        {
            $color = $color == $color1 ? $color2 : $color1;
            $empresa = $rows['empresa'];
            echo "<option type='text' value='$empresa' style='background:$color'>$empresa</option>";
        }
        mysqli_close($link);

        ?>
        </select> Empresa

My problem start on the second part of the script where it takes the option selected by the user and retrieve the remaining data of that table to display on the page.

<?php
                if(!isset($_POST['empresaselect'])){
                    echo "<b>Información de contratistas:</b>";
                }else{

                    $consulta = $_POST['empresaselect'];

                    $link = mysqli_connect('localhost', 'root', '', 'provadecodb');

                    $sql1 = mysqli_query($link,"SELECT * FROM contratistas WHERE empresa =$consulta") or die($link->error);
                    $sql2 = mysqli_query($link,"SELECT * FROM empleados WHERE empresa =$consulta") or die($link->error);

                    // $row1 = mysqli_fetch_assoc($sql1);
                    //
                    // echo $row1['dni']."<br>";
                    // echo $row1['nombre']."<br>";
                    // echo $row1['telefono']."<br>";
                    // echo $row1['email']."<br>";
                    // echo $row1['tipolic']."<br>";
                    // echo $row1['venclicec']."<br>";
                    // echo $row1['constafip']."<br>";
                    // echo $row1['f931']."<br>";
                    // echo $row1['nomemp']."<br>";
                    // echo $row1['norep']."<br>";
                    // echo $row1['segpers']."<br>";

                }
            ?>

These are the two scripts, and now, to be more specific on the things that I’ve tried so you guys/gals can help me seeying something that maybe I’m missing.

$sql1 = mysqli_query($link,"SELECT * FROM contratistas WHERE empresa =$consulta") or die($link->error);

This drop two errors, depends on what I select from the dropdown menu.

The first ‘company’ it’s called “Diegoland” and if I select this, the error that I’ve got it’s:

Unknown column ‘Diegland’ in ‘where clause’

The second ‘company’ it has more than one word (Super Serv Corp) and shows another error and have problem displaying all the complete name.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Serv Corp’ at line 1

The same happens if I select the third ‘company’ wich have two words (Wig Wonks):

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Wonks’ at line 1

Now, if I change the syntax It changes the error:

$sql1 = mysqli_query($link,"SELECT * FROM contratistas WHERE empresa ='" . $consulta . "'") or die($link->error);

By using '" . $consulta . "' It displays

Unknown column ’empresa’ in ‘where clause’

I am looking forward to work this out, Thanks in advance 🙂

How to&Answers:

My answer is based on the following understanding of what you’d like to do:
You want to list all company names in the select dropdown, Then, when you select one, you want to display the details about that company below the select element on the same page.

I don’t see it in your code, it might be in a part that you didn’t share with us, but first and foremost you need a form to submit.

<form method="post"> should be enough here before you create your select element, it will post to the same page, meaning it will load this page again with the data from any input element inside the form available to you as a POST variable.

Now you can check for $_POST[’empresa’]. However, you have the name of the element as empresas and you’re checking for empresa. You have to make sure they match, otherwise it won’t work.

Last, close your form with </form>. Sorry if this is too basic, just making sure.

One other comment. You seem to have 2 tables with the same empresa field in them. You could use a single query there using a JOIN statement.

$sql = "SELECT * from contratistas c inner join empleados e on c.empresa = e.empresa WHERE c.empresa = '{$_POST['$empresa']}'"; 

Hope this helps.

Answer:

Fixed!

I fixed the problem by deleting the second query

$sql2 = mysqli_query($link,"SELECT * FROM empleados WHERE empresa =$consulta") or die($link->error);

It seems like I have to do a JOIN to select the results of both tables! So, that is my next step on this travel!

Anyway, thanks to everybody for your answers! JP.