PHP Prepared Statement insert or update If row exists

荒凉一梦 提交于 2020-01-30 12:30:40

问题


I have been searching for a fix for my code below. I have searched many similar post in stackoverflow but didn't get a work around.

What I'm trying to establish is that, I want to check a query and if row exist I need to use UPDATE statement else INSERT statement. I have established this many time but without the prepared statement. Now that I'm modifying many of my previous codes with the prepared statements. Below is my code.

<?php

include'../db.php';

$exercise_days=$_POST['edays'];
$id=$_POST['id'];

if ($exercise_days!='' && is_numeric($exercise_days)) 
{
    $query=$db->prepare("SELECT * from exercisetable where id=?");
    $query->bind_param('s', $id);
    /* execute query */
    $query->execute();
    /* store result */
    $query->store_result();
    $query->bind_result($rowcheck);
    $query->fetch();
    if ($rowcheck)
    {
        $e_update=$db1->prepare("UPDATE exercisetable SET exercise_days=? WHERE id=?");
        $e_update->bind_param('ss', $exercise_days, $id);
        $e_update->execute();
        if($e_update){
        $message='Exercise Days Updated';
        $success='Success';
        echo json_encode(array('message'=>$message,'success'=>$success));
        }
        $e_update->close();

    } else {
        $e_insert=$db1->prepare("INSERT INTO exercisetable (`id`, `exercise_days`) VALUES (?,?)");
        $e_insert->bind_param('ss', $id, $exercise_days);
        $e_insert->execute();
        if($e_insert){
        $message='Exercise Days Inserted';
        $success='Success';
        echo json_encode(array('message'=>$message,'success'=>$success));
        }
        $e_insert->close();

    }
}

else  
{
    $message='Exercise Days Can Only Be Numbers';
    $success='Error';
    echo json_encode(array('message'=>$message,'success'=>$success));
} 
 ?>

Executing the above code always go into else statement where it goes to INSERT even though row exists. What I want is UPDATE if row Exists and INSERT if else. Any help appreciated.


回答1:


Thanks @RamRaider for the hint and suggestion. I'm posting the solution that worked for me, if in case anyone would require an instant reference.

Few edits that were done to MySql column prior to change in my original PHP code were that I altered my column to UNIQUE.

<?php

include'../db.php';

$exercise_days=$_POST['exercise_days'];
$id=$_POST['id'];

if ($exercise_days!='' && is_numeric($exercise_days))  
{
        $query=$db->prepare("INSERT INTO exercisetable (`id`, `exercise_days`) VALUES (?,?) ON DUPLICATE KEY UPDATE exercise_days =?");
        $query->bind_param('sss', $id, $exercise_days, $exercise_days);
        $query->execute();
        if($query){
        $message='Exercise Days Inserted';
        $success='Success';
        echo json_encode(array('message'=>$message,'success'=>$success));
        }
        $query->close();
}

else
{
    $message='Excercise Days Can Only Be Numbers';
    $success='Error';
    echo json_encode(array('message'=>$message,'success'=>$success));
} 
 ?>


来源:https://stackoverflow.com/questions/57363053/php-prepared-statement-insert-or-update-if-row-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!