user3806613
user3806613

Reputation: 510

replace string in php?

This question is a bit basic and have been covered many times but I'm not sure why my code doesn't do anything. it doesn't update string at all.

this is my code:

$fineImage = "users_fav/".$_GET['id']."/$newname";


$icon = "<img src='images/icon.png' height='70' width='70'  />";

$sql = "UPDATE $lchat SET user_message = replace(user_message, '$icon', '$fineImage')";
$query = mysqli_query($db_conx, $sql);

the problem is that if I change the '$icon', '$fineImage' to something like 'david', 'mark'. it works fine and it will replace the david with mark...!

so why doesn't it work the way i do it?

Upvotes: 0

Views: 73

Answers (2)

O. Jones
O. Jones

Reputation: 108841

It's likely that your call to MySQL's REPLACE(input, before, after) is failing to find before in its input, so is returning input unmodified.

Why could this be? Several reasons:

  1. user_message doesn't contain what you think it contains. For example, are the < and > tags entitized (that is, coded with &lt; and the like)?
  2. you're replacing a full <img..> tag with your $fine_image. Is $fineimage also an <img ...> tag?
  3. your before parameter contains embedded single quote characters. That could conspire to make your SQL string invalid.

Upvotes: 2

Yash Sodha
Yash Sodha

Reputation: 703

Try this:-

$sql = "UPDATE {$lchat} SET `user_message` = replace(`user_message`, '{$icon}', '{$fineImage}')";

Make sure you have $lchat, $icon, $fineImage defined. :)

Upvotes: 0

Related Questions