How to add data to table that is existing

955 views Asked by At
if (file_exists(TEMP.'/'."$check_dir".'/'."$endofname$test".'.jpeg')) 
        {
                echo "File : $endofname$test was made <br />";
                move($new_file_destination);

                $img_name = 'http:localhost/single4thenight/media/com_jomclassifieds/items/backpage/'."$check_dir".'/'."$endofname$test".'_1.jpeg, ';
                $img_title = "UPDATE iutca_jomcl_adverts SET images='$img_name' WHERE title='$title'";
                if ($conn->query($img_title) === TRUE) 
                    {
                        echo "Record updated successfully";
                    } else {
                            echo "Error updating record: " . mysqli_error($conn);
                                }       
        }

I need to add data to a table that is existing but I want to add data to the table not erase it with the updating data

3

There are 3 answers

1
davemyron On BEST ANSWER

If you're trying to append (concatenate) a new value to the end of an existing row's value you will still need to use UPDATE.

It would look something like this:

UPDATE iutca_jomcl_adverts SET images = CONCAT_WS('|', images, '$img_name') WHERE title='$title'

(CONCAT_WS is "concatenate with separator")

However, that won't handle the case where the row doesn't already exist. In that case, you would need to do an INSERT and use MySQL's ON DUPLICATE KEY UPDATE images = CONCAT(images,'|$img_name') (if title is a unique key).

2
Andrea Martinelli On

Try to use the INSERT INTO SQL if you want to add a new row.

INSERT INTO table (column1, column2, ..., columnN) VALUES ('value_column1',...,'value_columnN')

If you want to add data to an existing row (not table), you should use UPDATE SQL setting only field you want to edit/add.

1
Justin Workman On

You are updating rows using the UPDATE syntax. That only tells MySQL that you want to update data in that database NOT create a new row.

You will have to use the INSERT INTO syntax outlined in the MySQL documentation.