Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In my application I am getting the following error when trying to get data from MySQL. I'm not sure if it is a misconfiguration of MySQL, if someone could check my code for me and tell me how MySQL should look it will be greatly appreciated.

The error that I am receiving is Could not query: Unknown column 'id' in 'where clause'. This only displays once I select an item from the drop down box.

Here is the code for the main page which has the drop down box on it

<!DOCTYPE html>
<html>
<head>
    <title>Cable Management</title>
    <link href="style.css" rel="stylesheet">
</head>
<body>
<div class="wrapper">
<?php include("Header.php"); ?>
    <div id="main">
         <h1>Cable Management</h1>
         <h2>Bournemouth Office</h2>
         <p>Enter how many cables you want to remove or add from the Bournemouth office</p>
        <script>
            function showcables(str)
            {
                if (str == "")
                {
                    document.getElementById("txtHint").innerHTML="";
                    return;
                }

                if (window.XMLHttpRequest)
                {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp=new XMLHttpRequest();
                }
                else // code for IE6, IE5
                {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }

                xmlhttp.onreadystatechange=function()
                {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                    }
                }

                xmlhttp.open("GET","getcable.php?q="+str,true);
                xmlhttp.send();
            }
       </script>
</head>
<body>

<form>
<INPUT TYPE = "TEXT" NAME='amount' VALUE ="">
<select name="cables" onchange="showcables(this.value)">
<option value="">Select a cable type</option>
<option value="0.25 Metre Orange">0.25 Metre Orange</option>
<option value="0.3 Metre Orange">0.3 Metre Orange</option>
<option value="0.5 Metre Orange">0.5 Metre Orange</option>
<option value="1 Metre Orange">1 Metre Orange</option>
<option value="2 Metre Orange">2 Metre Orange</option>
<option value="3 Metre Orange">3 Metre Orange</option>
<option value="5 Metre Orange">5 Metre Orange</option>
<option value="10 Metre Orange">10 Metre Orange</option>
<option value="2 Metre Black">2 Metre Black</option>
<option value="3 Metre Black">3 Metre Black</option>
<option value="5 Metre Black">5 Metre Black</option>
<option value="10 Metre Black">10 Metre Black</option>
<option value="RJ 11">RJ11</option>
<option value="RJ 11 to BT">RJ11 to BT</option>
</select>
<INPUT TYPE = "button" Name = "Remove" VALUE = "Remove">
<INPUT TYPE = "button" Name = "Add" VALUE = "Add">
</form>
<br>
<div id="txtHint"><b>The amount of cables left will be listed here</b></div>

        </select>
    </div>
    <div class="footer-clear"></div><!-- add this at the end of wrapper -->
</div> <!-- i added this closing tag, it was missing (for wrapper) -->
<?php include("Footer.php"); ?> <!-- move footer to go outside of wrapper -->
</body>

</html> 

Here is the getcable.php page which actually has the MySQL commands in it

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','','cables');
if (!$con)
  {
  die("Could not connect: " . mysqli_connect_error());
  }


$sql="SELECT * FROM bmouthoff WHERE id = '".$q."'";

$result = mysqli_query($con,$sql) or die ("Could not query: " . mysqli_error($con));

echo "<table>";
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Amount'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?> 
share|improve this question
    
check the table again, sure the column isnt actually named ld (LD) ? –  Cups Oct 21 '13 at 11:22
1  
may be id column is not there. –  Jai Oct 21 '13 at 11:28
    
could you show a script of your data table? do you have a column named id? are your id values the strings like '0.25 Metre Orange'? –  Luis Oct 21 '13 at 11:29
    
Sorry, MySQL is not my strong point at all. I am using PHPmyadmin and I have created a database called cables. A table called bmouthoff and a Column called ID. My column wasn't called ID I am not getting this error anymore. Do I not need to add each column as the full name as in the drop down box? How can I get some dummy data to test? Thanks again –  Jono Knight Oct 21 '13 at 11:43
add comment

3 Answers

R u kidding ? value="RJ 11 to BT" This value isn't integer at all. Cannot be converted to int also with the intval funct. Unfortunetely I can not see if there is a null controll for the id ?

$q = isset($_REQUEST["id"]) ? $_REQUEST["id"] : null ;
if($q != null)
{
}

work better . What is the type of ID column ? Is int ? So the escaper of ' ' is useless.

share|improve this answer
    
Sorry I think there may be a problem with the way my application is working then. The idea is that when a certain cable from the drop down box is selected, it connects to MySQL and gets a number of cables that are set for that selected cable type. Make sense? –  Jono Knight Oct 21 '13 at 12:00
add comment

Your table seems not to have a column called id, therefore your query fails.

SELECT * FROM `bmouthoff` WHERE `id` = 'youridexample';

The error message...

Unknown column 'id' in 'where clause'.

...says it all.

Check out the table definition and replace id with the actual column name you want there, or rename the column in your table to id.

share|improve this answer
add comment

In query

SELECT * FROM bmouthoff WHERE id = '".$q."'"

you send value of cables select, wich is not integer.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.