0

I am building an Ionic App and I am trying to call a MySQL query from my app

I've managed to successfully do the query for select * from ...; However, I couldn't successfully pass the parameter to use in the WHERE, could you please tell me what I'm doing wrong?

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

Network response: Undefined index: barcode in C:\xampp\htdocs\abido\db2.php on line 15

Thanks in advance!

Edit:

changed hidden input to text and it solved many problems as hidden was passing empty values always for some reason. Also solution below helped.

Checked :Inserting data from front end to mysql db in angularjs and few other topics but couldn't solve my problem, thank you! :)

Community
  • 1
  • 1
Abido
  • 752
  • 6
  • 18

2 Answers2

0

You're passing the GET parameters wrongly, try this

$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;
    });
}
MrFoh
  • 2,693
  • 9
  • 42
  • 77
  • Still getting errors, when I change in php the following line "$barcode=$_GET['barcode'];" to a static number, the error is gone, however, when using GET to read the parameter it says undefined variable – Abido Mar 20 '16 at 02:08
  • Working! THANK YOU! Another problem was with hidden input, for some reason it was passing empty value always, used text input and all was suddenly working! Thanks! – Abido Mar 20 '16 at 02:51
-1

You can do json_encode(thing) instead of changing it by yourself

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46
  • true that would be a better way to write the output section of the code, but it isn't the answer to the problem actually mentioned in the question – ADyson May 02 '18 at 13:13
  • @ADyson my bad trying to help – Camille SAURY May 02 '18 at 13:14
  • The appropriate place for a remark which is intended to help but is not directly the answer is to put it into the comments on the question. Once you have enough reputation (50) you'll be able to add comments to questions. Although, this question is 2 years old (I saw your answer in the "Late Answers" moderation queue), so I don't know if the OP will see or care about comments at this stage. If you wish, you can delete this answer for now, to avoid downvotes. – ADyson May 02 '18 at 13:16