Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating the database using php

Tags:

sql

php

mysql

I'm inserting addedworkhours into my database, but the problem is that if i insert several times for the same id (afnumber), the old and new values are kept. The new val doesn't replace the old one. I'm trying to update it (the commented section) however not at all successful. Always the same result. I'm trying to get it to work using an if/else condition, to check whether a value in a column is empty, then insert. If not update, any help in the if condition statement?

The way I'm getting the updates output:

   if(isset($_POST['submit'])){

    if(AddedWH IS NULL) THEN

        $addedhours = $_POST['AddedHours'];
        $selectaf = $_POST['SelectAF'];
              $sql1="INSERT INTO `editedworkhours` (`AFNumber`,`AddedWH`) VALUES('$selectaf','$addedhours')";

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

             } 
             else{

             }

    else


                $tempname = $row['Field'];
                $sql2 = "UPDATE editedworkhours SET AddedWH ='".$_GET["addedhours"]."' WHERE AFNumber='".$_GET["selectaf"]."'";
                $result2 = mysqli_query($con,$sql2);
                if ($con->query($sql2) === TRUE) {
                } else {
                    echo "Error: " . $sql2 . "<br>" . $con->error;
                    echo '<script>swal("Error", "Something went wrong '.$con->error.'", "error");</script>';
                }
     echo '<script>swal("Success", "Changes have been saved", "success");</script>';  


} END IF;
echo $menu;
like image 695
dan Avatar asked Jul 22 '15 12:07

dan


2 Answers

One elegant way of doing this is defining afnumber as the table's primary key:

ALTER TABLE `editedworkhours` 
ADD CONSTRAINT `editedworkhours_pk` PRIMARY KEY (`afnumber`);

EDIT:
As Gordon noted in the comments, if you already have a primary key, you could add a unique constraint:

ALTER TABLE `editedworkhours` 
ADD CONSTRAINT `editedworkhours_uc` UNIQUE (`afnumber`);    

Once you have the constraint in place, you can utilize MySQL's insert's on duplicate key update clause:

INSERT INTO `editedworkhours`
(`afnumber`, `addedwh`)
VALUES ($selectaf, $addedwh)
ON DUPLICATE KEY UPDATE `addedwh` = VALUES(`addedwh`)
like image 153
Mureinik Avatar answered Oct 05 '22 02:10

Mureinik


Well, what I would do is make two statements that run - 1 INSERT and 1 UPDATE. The INSERT however would check that the same afnumber does not already EXIST. Here would be a sample script:

  if(isset($_POST['submit'])) {

   $addedhours = $_POST['AddedHours'];
   $selectaf = $_POST['SelectAF'];

$sql1="INSERT INTO `editedworkhours` (`AFNumber`,`AddedWH`) 
select '$selectaf','$addedhours'
where not exists (select afnumber from editedworkhours where afnumber = '$selectaf')";

$getResult =mysql_query($sql1);

 if(mysql_affected_rows($getResult) == 0) {

 $sql2 = "UPDATE editedworkhours SET AddedWH ='".$_POST["addedhours"]."' WHERE AFNumber='".$_POST["selectaf"]."'";

 $result2 = mysql_query($sql2);
 }

}

The above does 2 things:

a) Inserts if the record does not exist b) Updates if the insert results in 0 records

I also noted that in your "update" section you have $_GET superglobals whereas the insert has $_POST so I changed them both to $_POST. Not sure if that was bugging anything...

like image 34
Walker Farrow Avatar answered Oct 05 '22 01:10

Walker Farrow