Jump to content

String Replace on Insert?


djs1

Recommended Posts

Hello, I'm pretty new to PHP, and I've successfully performed a string replacement when calling information FROM the DB... however, I'm curious if it's possible to perform a string replacement on a piece of information as it's being entered INTO the DB?

 

example:

I have a form with a text field called "filename", in which all of the words typed into it will be separated by underscores. So I'd like to have a hidden form field which says 'take the value of "filename" and perform a string replacement on it which strips out the underscores'.

 

This way I'll have an underscored version AND a non-underscored version in the database.

 

Is this possible?

 

Thanks in advance!

-d

 

Link to comment
Share on other sites

Ok, thank you! ... you just got me one step closer!

 

By moving my code above the mysql insert statement, the string replacement is at least successfully working... However, the string replaced result I'm getting is for the previous record.  In other words, each time i enter data into the form, the database updates with the previous form's info.  the result is always one behind.

 

any suggestions?

 

here's my code:

Above mysql insert statement:

<?php $description = str_replace("_"," ",$_POST['filename']); ?>

 

Hidden Form Field:   

<input type="hidden" name="description" id="description" value="<?php echo $description; ?>">

 

thanks again!

 

Link to comment
Share on other sites

You're whole process doesn't make sense (to me). Look at it in a work-flow sort of way.

 

  • User requests page.
  • Page is output (at this point <input="hidden" name="decription" ... will have a value of "" because $description isn't set yet)
  • User fills in form
  • User clicks submit
  • Code replaces spaces with underscores
  • Code inserts into database
  • Page is output (at this point <input="hidden" name="description" ... will have a value of the previous form)

 

As you can see the value included in the hidden  input is at no point relevant to what is input to the form... Or am I missing something? If you want both an underscored version and a non underscored version in the database, surely you should just be doing...

 

<?php
$filename = $_POST['filename'];
$under_filename = str_replace("_", " ", $filename);

mysql_query("INSERT INTO `table` (`filename`, `under_filename`) VALUES ('$filename', '$under_filename')");
?>

 

Link to comment
Share on other sites

No, you were correct, the flow of my process didn't make sense... and I appreciate you laying it out for me so that I could see the error for myself. 

 

What I didn't realize is that I could insert a value ( ie. $under_filename ) into the database without actually having a form field for it.  That's why I was trying to force a hidden value. 

 

I did what you recommended and it's working properly now.  Thanks for the help!

-d

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.