如何使用ion-toggle更改db中的值

时间:2019-12-14 22:39:10

标签: php angular ionic3

您好,我想在数据库中连续更改一个值。该值为0或1。当ion-toggle关闭时为0,当ion-toggle设置为on时为1。

这是我到目前为止所拥有的

html

   <ion-item>
    <ion-label *ngIf="item.statusSet">
       On
    </ion-label>
    <ion-label *ngIf="!item.statusSet">
        Off
    </ion-label>
    <ion-toggle color="primary" (ionChange)="changeStatusSet(item)" [(ngModel)]="item.statusSet"></ion-toggle>
</ion-item>

.ts

  changeStatusSet(item:any) 
{
    this.userData.changeStatus(item.id, item.statusSet)
    .map(res => res.json())
    .subscribe(data => {

        if (data.success) {
            if(item.statusSet)
            {
                this.validStatus = 'Its ON';
            }
            else
            {
                this.validStatus = 'Its Off';
            }
            swal({
                icon: "success",
                text: this.validStatus,
                buttons: {
                    visible: false
                }
            });
        }
    });  

}

php函数changeStatus(item.id,item.statusSet)

function changeStatus()
{

$request = \Slim\Slim::getInstance()->request();
$response['success'] = true; // 1 true if not errors OK NOTHING

$id = $request->post('id');
**$status =  $request->post('status') ? 1 : 0;**

$db = getDB();
$sql = "UPDATE table SET status = :status WHERE id = :id";
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->bindParam("status", $status);
$stmt->execute();
$db = null;

echo json_encode($response);

}

我突出显示了状态变量,原因是我不确定它是否正确。作为回应,我获得了成功

谢谢

0 个答案:

没有答案