How to get php/ajax to get an integer from mysql


Problem :

The application is made to keep a track of how many cables are in stock.

So the part of my application that I am struggling to get working is the following.

When a user selects something from the dropdown box, it queries the MySQL database and returns an integer. There is only 1 integer per selection. For example a user selects a 5 metre orange cable from the drop down box it will display the amount of cables left. Which is stored in the mysql database. I am having a nightmare getting the code to work if someone could please provide some assistance.

Here is the main page

    <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 of that type 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 file

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

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

mysqli_select_db($con,"bmouthoff");
$sql="SELECT * FROM bmouthoff WHERE id = '".$q."'";

$result = mysqli_query($con,$sql);


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

mysqli_close($con);
?> 

Thanks in advance



Solution :

You need to put:

echo "<table>";

before:

while($row = mysqli_fetch_array($result))

in the PHP.

Get rid of mysqli_select_db($con, "bmouthoff"). That's not the name of the database, and you already selected the database in the mysqli_connect() call.

die("Could not connect: " . mysqli_error($con));

should be:

die("Could not connect: " . mysqli_connect_error());

And change:

$result = mysqli_query($con,$sql);

to:

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

Mysql Tutorials

Mysql Howto..

Python scraper how to add mysql insert

How to store the specific (polls eg.) data in a MySQL database?

how to list mysql query result in particular sequence?

PHP - How to check URLS for 404/Timeout?

How to use checkboxes to enter true (1) or false (0) into mySQL AND show as checked in php / html form?

C++ MFC Dialog - How to display items from a MySQL database in a list control?

How to integrate database into software

How can I add mysql abilities to tagmate? [closed]

How to see local traffic sent by a program to a local mySQL database?

How to get a field value and all related elements by using that field value in the same query?

How to store permanent set max_allowed_packet mysql variable [duplicate]

How to edit datatable's rows (JAVA + Primefaces + JSF + MYSQL)

How to search min value from four other different fields in mysql query?

How to match Value from string in Mysql PHP

how to create a trigger for specific database in MySql?

How to get all values in a colum listed in MySql - php

Hide / show list in PHP

How can I import GTFS files into MySQL?

Logic Issue - how many / which small boxes in a big box - PHP/MySQL

how to insert java arraylist into mysql table

How to get unix timestamp interval between two datetime values most efficiently in MySQL?

how to update multple rows in mysql with php [closed]

MySQL - Relational tables - How to create multiple relationships?

How to implement a search engine using python mysql?

How to interface a java application with a local database in Mac os x

How to perform MySQL 'SUM' but to '0' decimal places?

How to select the last row in mysql

How to Display Current Username In Form Field then Input from the Form into Another Table - “Forum Posts”

Django: How to set the correct the location for MySQL database

How to write store procedure to check Grant in MySql?