Home > front end >  I want to update data in a mysql table where is Column title_modal will be updated if Column store_u
I want to update data in a mysql table where is Column title_modal will be updated if Column store_u

Time:10-18

I have a Table called "widget_cont" and 3 different column "title_modal", "store_url" and "site_url" .

I want to UPDATE column "title_modal" where "store_url" and "site_url" are same values ex: "google.com" = "google.com" However, there are multiple rows with the same type of columns but different values ex: "facebookcom" = "facebook.com".

What I tried yet is:

if(isset( $_POST['title_modal']) ){
   
    $title_modal = mysqli_real_escape_string($conn, $_POST['title_modal']);
   
    if( isset($title_modal) ){
        
        $query_title_modal = "UPDATE widget_cont SET title_modal='$title_modal' WHERE store_url= 'site_url' ";
        echo  $_POST['title_modal'];
    }
   
    if( !mysqli_query($conn, $query_title_modal) ){
        echo "ERROR: " . mysqli_error($conn);
    }
}

Every time the values are updated in the wrong and same row. If I put static URL in the "site_url" column it works. Looks the solution is easy but I have not succeeded yet. Can someone help, please? Thanks.

CodePudding user response:

store_url= 'site_url'

Is wrong. Because of the quote marks it will treat site_url as a literal string, not a column name. In other words it will try to find rows where the store_url column contains the actual text "site_url". I would guess you don't have any rows with such content, and clearly that's not your intent.

If you want to update all rows where those two columns are equal then use

store_url= site_url

without the quote marks.

CodePudding user response:

WHERE store_url= 'site_url' will return items where store_url is the string 'site_url', most likely no items will return. Remove the quotes, replace

$query_title_modal = "UPDATE widget_cont SET title_modal='$title_modal' WHERE store_url= 'site_url' ";

with

$query_title_modal = "UPDATE widget_cont SET title_modal='$title_modal' WHERE store_url=site_url";
  • Related