theveil
theveil

Reputation: 77

PHP Prepared Statement insert or update If row exists

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.

Upvotes: 1

Views: 1028

Answers (1)

theveil
theveil

Reputation: 77

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));
} 
 ?>

Upvotes: 2

Related Questions