dan
dan

Reputation: 563

Concatenating a string to an existing string in the database

I'm inserting some data into my database, such as deductedwh and note. And i have an update function that i'm using based on $sql2. The thing is, that deductedwh is a number and it's successfully being added with the previous value in the column. However when it comes to note (text) I'm trying to append the new text value ($note) to the previous existing text in the column (Note). But it's not working, most likely my syntax is wrong. Any guidance please?

 $sql2 = "UPDATE editedworkhours SET DeductedWH = DeductedWH +'$deductedhours' AND Note = Note . '$note' WHERE AFNumber='$selectaf'";
            $result2 = mysql_query($sql2);

            if (isset($result2))
            {

            }
            else
            {

                echo '<script>swal("Error", "Something went wrong error");</script>';
            }

Upvotes: 2

Views: 85

Answers (3)

VMcreator
VMcreator

Reputation: 843

Use mysql concat function:

UPDATE
    editedworkhours
SET
    DeductedWH = DeductedWH + CAST('$deductedhours' AS UNSIGNED),
    Note = CONCAT(Note,'$note')
WHERE
    AFNumber ='$selectaf';  

Upvotes: 1

Vincent
Vincent

Reputation: 66

To update multiple columns in Mysql you have to use comma as separator not AND

SET DeductedWH = DeductedWH +'$deductedhours' , Note = concat(Note, . '$note'.) 

Upvotes: 0

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

You need to use concat() to concatenate your new text with your existing text also you have typo a in set replace and with comma

"UPDATE editedworkhours 
SET DeductedWH = DeductedWH +'$deductedhours' 
,Note = concat(Note, '$note')
WHERE AFNumber='$selectaf'";

Upvotes: 0

Related Questions