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