Replace db field string (Resolved)

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Replace db field string (Resolved)

Post 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.
Last edited by ConceptDestiny on Tue Mar 29, 2011 10:06 pm, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Replace db field string

Post by Jackolantern »

What is it doing? Are you getting an error, or is it a logic problem?
The indelible lord of tl;dr
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Replace db field string

Post by ConceptDestiny »

trying to modify a string contained within a database field, i.e. pinstripe pastel yellowbelly, and replace "pastel".
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Replace db field string

Post 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).
The indelible lord of tl;dr
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Replace db field string

Post 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.
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Replace db field string

Post 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 />";
                      }
                }
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Replace db field string

Post by hallsofvallhalla »

it works with this?

Code: Select all

$id[] = $row1['id'];
$morph[] = $row1['morph']."<br />";
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Replace db field string

Post 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.
The indelible lord of tl;dr
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Replace db field string

Post 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
Post Reply

Return to “Advanced Help and Support”