0

so my newest problem! i have a save button and that save button already has the insert into query and working fine now what i wanna do is when the user clicks on the save button what it does first is, it checks if a record of that user already exists and if there already is a record in regards to that user then instead of inserting it will update the table. and if there isnt a record then it will just insert.

what i have so far:

<?php  

include('connect.php');

if(isset($_POST['action']) && $_POST['action'] === 'saveIteration'){

    $distanta = $_POST['distanta'];
    $durata_calatorie = $_POST['durata_calatorie'];
    $start = $_POST['start'];
    $end = $_POST['end'];

    $add_new_input = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $add_new_input_query = "INSERT INTO ".$DB_TABLE." (distanta , durata , start ,end ) VALUES(? , ? , ? , ?)";
    if ($add_new_input_stmt = $add_new_input ->prepare($add_new_input_query)) {
        $add_new_input_stmt ->bind_param("ssss",$distanta , $durata_calatorie , $start , $end);
        $add_new_input_stmt ->execute();
        $add_new_input_stmt ->close();
    }
    $add_new_input->close();

}

?> 
2
Contributors
1
Reply
28
Views
2 Months
Discussion Span
Last Post by AndrisP
1

Show your table creat SQL code. I do not know what primary or unique keys is set to the table. Example if unique key is person_id

INSERT INTO ".$DB_TABLE." (`person_id`, `distanta` , `durata` , `start` , `end` )
VALUES(? , ? , ? , ? , ?)
ON DUPLICATE KEY UPDATE
SET `distanta` = VALUES(`distanta`)
,`durata` = VALUES(`durata`)
,`start` = VALUES(`start`)
,`end` = VALUES(`end`);

Edited by AndrisP

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.