【发布时间】:2016-07-06 07:59:17
【问题描述】:
我正在构建一个 Ionic 应用程序并尝试从我的应用程序调用 MySQL 查询
我已经成功完成了 select * from ... 的查询;但是,我无法成功传递要在 WHERE 中使用的参数,您能告诉我我做错了什么吗?
Controller code in app.js:
exampleApp.controller('productScan', function($scope, $http) {
$scope.sendinfo=function(){
$http.get("http://192.168.100.121/abido/db2.php",{barcode : $scope.barcode})
.then(function (response) {$scope.names = response.data.records;});
}
});
index.html
<ion-content ng-controller="productScan">
<form ng-submit="sendinfo()" method="get">
<input name="barcode" type="hidden" value="5053990101832">
<button type="submit" class="button">select pringles</button>
</form>
<div ng-app="gAssist" ng-controller="productScan">
<table>
<tr ng-repeat="x in names">
<td>{{ x.ID }}</td>
<td>{{ x.Name }}</td>
<td>{{ x.Description }}</td>
<td>{{ x.Barcode }}</td>
</tr>
</table>
</div>
</ion-content>
db2.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gadb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$barcode=$_GET['barcode'];
$result = $conn->query("SELECT * FROM products where barcode=$barcode");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"ID":"' . $rs["id"] . '",';
$outp .= '"Name":"' . $rs["name"] . '",';
$outp .= '"Description":"' . $rs["description"] . '",';
$outp .= '"Barcode":"'. $rs["barcode"] . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo($outp);
?>
网络响应:未定义索引:第 15 行 C:\xampp\htdocs\abido\db2.php 中的条形码
提前致谢!
编辑:
将隐藏输入更改为文本,它解决了许多问题,因为隐藏总是出于某种原因传递空值。下面的解决方案也有帮助。
检查了:Inserting data from front end to mysql db in angularjs 和其他一些主题,但无法解决我的问题,谢谢! :)
【问题讨论】:
标签: mysql angularjs ionic-framework