Delete row in mysql doesn't work


#1

Hi,

I try to delete a MySQL row from my TS file but it doesn’t work…

Here is my TS method :

onDeletePanier(id){
  
  const confirm = this.alertCtrl.create({
    title: 'Suppresion de l\'article',
    message: 'Êtes-vous sûr de vouloir supprimer cet article du panier ?',
    buttons: [
      {
        text: 'Annuler',
        handler: () => {
          console.log('Annulation suppression');
        }
      },
      {
        text: 'Supprimer',
        handler: () => {
          console.log(id);
          this.authService.postData(id, "deletePanier").then((result) =>{
            this.responseData = result;
            console.log(this.responseData);
            this.navCtrl.setRoot(PanierPage);
            
          }, (err) =>{
              // Connection failed
          })
        }
      }
    ]
  });
  confirm.present();
}

AND my PHP API method :

function deletePanier() {
    $request = \Slim\Slim::getInstance()->request();
    
    $id	= $request->getBody();
    
	/*echo '{"id offre à supprimer": '. $id . '}';
	exit();*/
    
    try {
        $db = getDB();

        $sql = "DELETE FROM panier WHERE id= :id";
        $stmt = $db->prepare($sql);
        $stmt->bindValue(':id', $id, PDO::PARAM_INT);
        $stmt->execute();
        
        echo '{"success":{"Suppression offre panier":'. $id .'}}';
               
        $db = null;
        
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

I have no errors but the row is not deleted… :face_with_raised_eyebrow:
Somebody can help me please ???

Thanks a lot


#2

Hi,

Have you checked that the API call is executing the “delete” code?


#3

Thank you for reply TheTourist.
Yes I had… And the id is correctly passed to the API.
I can see API response in the console.log…


#4

In fact, I try to get a storage value in the constructor of my TS file.

   this.storage.get('userid').then((val) => {
      this.userData.id = val;
      console.log("id",this.userData.id);
      console.log("val",val);
    });

    this.getOffres();
  }

I want to use this value in my methods getOffres but it is empty :

 getOffres(){
   console.log("userId",this.userData.id);
  this.authService.postData(this.userData, "mesOffres").then((result) =>{
    this.responseData = result;

#5

You can’t do anything with Storage until its ready() Promise resolves.


#6

Thank you for your help rapropos. I’m really new with IONIC 3.
What do you mean ? Could you explain me with my code, please ?


#7

It means this.getOffers() should wait storage.get finishes to execute:

 this.storage.get('userid').then((val) => {
      this.userData.id = val;
      console.log("id",this.userData.id);
      console.log("val",val);
     this.getOffres();
    });
  }

Or you can turn this.userData into a Observable and subscribe it. Then, everytime it’s values changes, you can do something (in this case, execute this.getOffres()).