ionic / angularJS将参数传递给MySQL查询

时间:2016-03-19 15:16:11

标签: mysql angularjs ionic-framework

我正在构建一个Ionic App,我正试图从我的应用程序调用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以及其他一些主题,但无法解决我的问题,谢谢! :)

2 个答案:

答案 0 :(得分:0)

你错误地传递了GET参数,试试这个

$scope.sendinfo = function() {

    var config = {
        params: {
            barcode : $scope.barcode
        }
    };

    $http.get("http://192.168.100.121/abido/db2.php", config).
    then(function (response) {
        $scope.names = response.data.records;
    });
}

答案 1 :(得分:-1)

您可以json_encode(thing)而不是自己更改