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! :)

2 Answers 2

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;
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

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

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

3 Comments

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 my bad trying to help
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.