tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I got a table which i use to store html-code. In this code there are url of pictures with capital letters. Now i changed the directories of some of the picture folders (i took out the capitals). Since i use a unix server the pictures in the html-code will be visable and errors occur.
Now i what to write a script that replaces the capitals in the urls in the html codes. Everthing is working correct untill the update query.
<?php
$search= "Media";
$replace= "media";
$query_Recordset_check_content = "SELECT * FROM $tabel_content WHERE content LIKE '%".$search."%'";
$Recordset_check_content = mysql_query($query_Recordset_check_content, $connect) or die(mysql_error());
$row_Recordset_check_content = mysql_fetch_assoc($Recordset_check_content);
$totalRows_Recordset_check_content = mysql_num_rows($Recordset_check_content);
do{
$content = $row_Recordset_check_content['content'];
$content_id = $row_Recordset_check_content['id'];
$content = str_replace($search, $replace, $content);
echo $content;
mysql_query("UPDATE $tabel_content SET content = '".$content."' WHERE id = '".$content_id."'");
// mysql_query("UPDATE $tabel_content SET content = '$z' WHERE id = '$content_id'");
} while($row_Recordset_check_content = mysql_fetch_assoc($Recordset_check_content));
?>
The echo $content shows the html-code and pictures are visable, so the str_replace function worked. But the update query didn't work. What am i doing wrong? Any suggestion?
Run this and see what echo's out of the first $content and the second one. If they are not different MySQL will not update and it will return zero affected rows. Also I threw in an error trapper to see what MySQL is telling you about your query.
<?php
$query_Recordset_check_content = "SELECT * FROM $tabel_content WHERE content LIKE '%".$search."%'";
$Recordset_check_content = mysql_query($query_Recordset_check_content, $connect) or die(mysql_error());
$row_Recordset_check_content = mysql_fetch_assoc($Recordset_check_content);
$totalRows_Recordset_check_content = mysql_num_rows($Recordset_check_content);
while ($row_Record_set_content)
{
$content = $row_Recordset_check_content['content'];
echo $content; // Just for testing
$content_id = $row_Recordset_check_content['id'];
$content = str_replace($search, $replace, $content);
echo $content; // Is this echo different that the first? MySQL will not update if they are not different
if (!mysql_query("UPDATE $tabel_content SET content = '".$content."' WHERE id = '".$content_id."'"))
{
echo mysql_error();
}
}
?>
The html-code had some dubble quotes ("") which caursed the syntax error. I searched php.net for htmlspecialchars function and found a usefull made function:
<?php
$search = "Media";
$replace = "media";
function unhtmlentities ($string) {
$trans_tbl =get_html_translation_table (HTML_ENTITIES );
$trans_tbl =array_flip ($trans_tbl );
return strtr ($string ,$trans_tbl );
}
$query_Recordset_check_content = "SELECT * FROM $tabel_content WHERE content LIKE '%".$search."%'";
$Recordset_check_content = mysql_query($query_Recordset_check_content, $connect) or die(mysql_error());
$row_Recordset_check_content = mysql_fetch_assoc($Recordset_check_content);
$totalRows_Recordset_check_content = mysql_num_rows($Recordset_check_content);
echo $totalRows_Recordset_check_content;
do
{
$content = $row_Recordset_check_content['content'];
echo $content; // Just for testing
$content_id = $row_Recordset_check_content['id'];
$content = str_replace($search, $replace, $content);
$content = unhtmlentities (addslashes (trim ($content)));
echo $content; // Is this echo different that the first? MySQL will not update if they are not different
if (!mysql_query("UPDATE $tabel_content SET content = '".$content."' WHERE id = '".$content_id."'"))
{
echo mysql_error();
}
} while($row_Recordset_check_content = mysql_fetch_assoc($Recordset_check_content));
?>