Page 1 of 1

Replace db field string (Resolved)

Posted: Thu Mar 24, 2011 8:41 pm
by ConceptDestiny
Hi guys,

Just wondering if any of you could help me out with this bit of code. Trying to replace a portion of a string contained within a db field.

Code: Select all

       $table = ($_POST['table']);
                $morphold = ($_POST['morphold']);
                $morphnew = ($_POST['morphnew']);               
                $query1 = "SELECT * FROM $table WHERE morph like '%$morphold%'";           
                $result1 = mysql_query($query1) or die(mysql_error());
                $countresults=mysql_num_rows($result1);
                if ($countresults == 0)
                {echo "No results, try again.<br /><br /><a href='morphreplace.php'><button>Back</button></a>";}
                else
                {
                  echo "Total results: $countresults<br /><br />";
                    while ($row1 = mysql_fetch_array($result1))
                    {
                               
                      $id[] = $row1['id'];
                      $morph[] = $row1['morph'];                 
                    }
                   
                      for ($i=0;$i<$countresults;$i++)
                      {
                          $find = $morphold;
                          $replace = $morphnew;
                          $newmorph = preg_replace("/$find/", "$replace", $morph[$i]);
                          $updateplayers="UPDATE reptiles SET morph='$newmorph' WHERE id='$id[$i]'";
                          mysql_query($updateplayers) or die("Could not update new morph data.");               
                          echo "$newmorph<br /><br />";
                      }
                }
resolved.

Re: Replace db field string

Posted: Thu Mar 24, 2011 11:13 pm
by Jackolantern
What is it doing? Are you getting an error, or is it a logic problem?

Re: Replace db field string

Posted: Sat Mar 26, 2011 11:17 pm
by ConceptDestiny
trying to modify a string contained within a database field, i.e. pinstripe pastel yellowbelly, and replace "pastel".

Re: Replace db field string

Posted: Sun Mar 27, 2011 12:10 am
by Jackolantern
Sorry, I mean, what is wrong with the code? It always helps debugging efforts to know if you are getting an error message (and what error message if so), or if it is just not doing what you need it to (and what it is doing if it is not what you want).

Re: Replace db field string

Posted: Sun Mar 27, 2011 2:10 pm
by ConceptDestiny

Code: Select all

                          $find=($morphold);
                          $replace = ($morphnew);
                          $newmorph = str_replace($find, $replace, $morph[$i]);

                          $updateplayers="UPDATE reptiles SET morph='$newmorph' WHERE id='$id[$i]'";
                          mysql_query($updateplayers) or die("Could not update new morph data.");               

                          echo "$newmorph<br /><br />";
The above portion of the code doesn't appear to be functioning correctly. It's getting $morphold and $morphnew, however I think it may be having trouble with the $morph[$i]. So $morphold isn't being replaced with $morphnew. The $newmorph var is still outputting the original db value without replacing the relevant part of the string as specified in $morphold.

The apparent logic I was using was:
retrieve db field, modify it using the str_replace, then update the field again with the modified string. It's probably some small detail I'm missing... argh.

Re: Replace db field string

Posted: Sun Mar 27, 2011 7:45 pm
by ConceptDestiny
Found the problem. Made a few tweaks, managed to rectify the code so it works.

The following is the corrected, and updated code:

Code: Select all

      $table = ($_POST['table']);
                $morphold = ($_POST['morphold']);
                $morphnew = ($_POST['morphnew']);                
                $query1 = "SELECT * FROM $table WHERE morph like '%$morphold%'";            
                $result1 = mysql_query($query1) or die(mysql_error());
                $countresults=mysql_num_rows($result1);
                if ($countresults == 0)
                {echo "No results, try again.<br /><br /><a href='morphreplace.php'><button>Back</button></a>";}
                else
                {
                  echo "Total results: $countresults<br /><br />";
                    while ($row1 = mysql_fetch_array($result1))
                    {
                                
                      $id[] = $row1['id'];
                      $morph[] = $row1['morph'];                  
                    }
                    
                      for ($i=0;$i<$countresults;$i++)
                      {
                          $find = $morphold;
                          $replace = $morphnew; 
                          $newmorph = preg_replace("/$find/", "$replace", $morph[$i]);
                          $updateplayers="UPDATE reptiles SET morph='$newmorph' WHERE id='$id[$i]'";
                          mysql_query($updateplayers) or die("Could not update new morph data.");               
                          echo "$newmorph<br /><br />";
                      }
                }

Re: Replace db field string

Posted: Mon Mar 28, 2011 4:16 pm
by hallsofvallhalla
it works with this?

Code: Select all

$id[] = $row1['id'];
$morph[] = $row1['morph']."<br />";

Re: Replace db field string

Posted: Tue Mar 29, 2011 3:51 am
by Jackolantern
Doesn't that code simply default to storing it in the 0 slot of the array? I am not completely sure though, as I always explicitly give the index.

Re: Replace db field string

Posted: Tue Mar 29, 2011 10:01 pm
by ConceptDestiny
hallsofvallhalla wrote:it works with this?

Code: Select all

$id[] = $row1['id'];
$morph[] = $row1['morph']."<br />";
It does, but it adds "<br>" into the morph string. :oops:

But yeah, the final version of the code works perfectly, albeit very rudimentary.
Jackolantern wrote:Doesn't that code simply default to storing it in the 0 slot of the array? I am not completely sure though, as I always explicitly give the index.
Not sure what you mean, tell me more? :D