Flutter Php Mysql Database Tutorial : CRUD Operations

FLUTTER => PHP => MYSQL

PHP code for db_Config.php file

<?php
$db_name = "DB_NAME";
$db_user = "DBUSER_NAME";
$db_pass = "DB_PASS";
$db_server = "localhost";
$con = mysqli_connect($db_server,$db_user,$db_pass,$db_name);
if(!$con){
	echo  "connection error";
}
else{
	//echo  "connection succesful ";
}
?>

Php code for Insert Data flutter_register.php

<?php
require_once "db_config.php";
$name=$_POST["names"];
$pass=$_POST["password"];
$query="insert into flutterSample (name,pass) values ('$name','$pass')";
$result = mysqli_query($con,$query);
if($result){
	echo "done";
}
else{
	echo "error";
}
?>	

Flutter Code for Insert Data

  void SaveData(String name, String pass) async{
  var url = 'https://learningeek.com/flutter_register.php'; 

    var data = {"names":name,"password":pass};
    var res = await http.post(url,body: data);
  }

PHP code for FetchData

 <?php
require_once('db_config.php');
 $sql = "SELECT * FROM flutterSample";
 $r = mysqli_query($con,$sql);
 $result = array();
 while ($row = mysqli_fetch_array($r)) {
  array_push($result,array("id"=>$row['0'],"name"=>$row['1'],"pass"=>$row['2']));
}
echo json_encode(array("result"=>$result));

mysqli_close($con);
?>

Flutter code for data Fetching

  Future getData() async{
    var url = "https://learningeek.com/flutter_getData.php";
    http.Response res = await http.get(url);

    var data = jsonDecode(res.body);
    dataModel = new List();

    for(var word in data['result']){
      String id = word['id'];
      String name = word['name'];
      String pass = word['pass'];

      dataModel.add(new Model(id,name,pass));
    }
    print('datalenght'+"qwertyu");
    return dataModel;
  }

Php code for update data flutter_update.php

<?php  
require_once "db_config.php";

$rolls=$_POST["_id"];
$names=$_POST["nm"];
$passw=$_POST["pss"];

$query="UPDATE flutterSample SET name='$names',pass='$passw' WHERE id='$rolls'";
$result=mysqli_query($con,$query);
if($result)
{
echo "done";
}
else{
echo "error";
}
?>
?>

Flutter code for Update Data

  void updateData(String id,String name,String pass)async{
    var url = 'https://learningeek.com/flutter_update.php';
    var data = {"_id":id,"nm":name,"pss":pass};
    var res = await http.post(url,body: data);
  }

PHP code for deletion data flutter_delete.php

<?php  
require_once "db_config.php";
$id=$_POST["id"];
$query="DELETE FROM flutterSample  WHERE id='$id'";
$result=mysqli_query($con,$query);
if($result)
{
	echo "done";
}
else{
echo "error";
}
?>

FLUTTER code for data deletion

  void deleteData(String _id)async{
    var url = 'https://learningeek.com/flutter_delete.php';
    var data = {"id":_id};
    var res = await http.post(url,body: data);
  }

3 thoughts on “Flutter Php Mysql Database Tutorial : CRUD Operations”

  1. Hi,
    thank you for sharing. Is this script safe to connect to the database? What if someone calls the php script? Is this script for publication?
    Can you help me?

    Reply
  2. Great tutorial. I was able to follow it and it works. But can you please post the code for the “Refresh” button? It’s missing.

    Thanks in advance.

    Reply

Leave a Comment