2

I'm inserting into my database id and hours, which is working successfully. However when i insert a new value for an id that has a previous value, the old val isn't replaced by the new, instead they're kept both. I'm trying to update the function with an if/else statement (commented part). But still the same result. The if/else statement must check first if hours (a column in the table) value is empty, if so then perform sql insert, else perform sql update. Any help please?

if (isset($_POST['submit'])) {
    $hours = $_POST['Hours'];
    $selectid = $_POST['SelectID'];
    $sql1 = "INSERT INTO `editedworkhours` (`id`,`H`) VALUES('$selectid','$hours')";

    $getResult = mysql_query($sql1);
    if (mysql_affected_rows() > 0) {

    } else {

    }

    $tempname = $row['Field'];
    $sql2 = "UPDATE editedworkhours SET H ='" . $_GET["hours"] . "' WHERE IDNumber='" . $_GET["selectid"] . "'";
    $result2 = mysqli_query($con, $sql2);
    if ($con->query($sql2) === TRUE) {
    } else {

    }


}
echo $menu;
3
  • 1
    First SELECT, then check if exists or not, then do insert or update Commented Aug 3, 2015 at 6:56
  • @b0s3 i tried this: jsfiddle.net/8jxvy3pe but i'm not sure how to check in the if condition whether the value in cell in column addedwh is null Commented Aug 3, 2015 at 6:58
  • You could do an insert on duplicate key update query Commented Aug 3, 2015 at 7:07

1 Answer 1

2

Try this

<?php
    if(isset($_POST['submit']))
    {
        $addedhours = $_POST['AddedHours'];
        $selectaf = $_POST['SelectAF'];

        $sql1="SELECT * FROM editedworkhours WHERE AFNumber='$selectaf' and AddedWH ='$addedhours'";

        $getResult = mysql_query($sql1);
        $count = count($getResult);
        if(!empty($count) || $count==1)
        {
            $tempname = $row['Field'];
            $sql2 = "UPDATE editedworkhours SET AddedWH ='$addedhours' WHERE AFNumber='$selectaf'";
            $result2 = mysql_query($sql2);

            if (isset($result2))
            {
                //Data inserted
            }
            else
            {
                //Insert Failed
                echo '<script>swal("Error", "Something went wrong error");</script>';
            }
            echo '<script>swal("Success", "Changes have been saved", "success");</script>';
        }
        else
        {
            $sql3 = "INSERT INTO editedworkhours (AFNumber,AddedWH) VALUES('$selectaf','$addedhours')";
            $Result =   mysql_query($sql3);
            if(isset($Result))
            {
                echo 'Success';
            }
            else
            {
                echo 'Failed';
            }
        }
    }
    echo $menu;
Sign up to request clarification or add additional context in comments.

6 Comments

in your code, the if condition is checking if $count is empty, if so then update?
dear i'm getting now a notice for undifined inde for both selectaf and addedhours
how you pass this data?? any form data?? <form ...
is it possible to open a chat conv?
You keep comment it will load automatically. Kindly upload the code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.