如何让php / ajax从mysql中获取一个整数

时间:2013-10-18 13:23:42

标签: php mysql ajax

该应用程序用于跟踪库存中的电缆数量。

因此,我努力工作的应用程序部分如下。

当用户从下拉框中选择某些内容时,它会查询MySQL数据库并返回一个整数。每个选择只有1个整数。例如,用户从下拉框中选择5米长的橙色电缆,它将显示剩余的电缆数量。哪个存储在mysql数据库中。如果有人可以提供一些帮助,我正在做一个噩梦让代码工作。

这是主页

    <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> 

这是getcable.php文件

    <?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);
?> 

提前致谢

1 个答案:

答案 0 :(得分:0)

你需要把:

echo "<table>";

之前:

while($row = mysqli_fetch_array($result))
PHP中的

摆脱mysqli_select_db($con, "bmouthoff")。这不是数据库的名称,您已经在mysqli_connect()调用中选择了数据库。

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

应该是:

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

并改变:

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

为:

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