djs1 Posted September 30, 2009 Share Posted September 30, 2009 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 Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted September 30, 2009 Share Posted September 30, 2009 Yes, that is very possible to do. You just need to manipulate your information before you insert it into the database. You can do all of this code right above your mysql insert statement. Quote Link to comment Share on other sites More sharing options...
djs1 Posted September 30, 2009 Author Share Posted September 30, 2009 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! Quote Link to comment Share on other sites More sharing options...
cags Posted October 1, 2009 Share Posted October 1, 2009 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')"); ?> Quote Link to comment Share on other sites More sharing options...
djs1 Posted October 1, 2009 Author Share Posted October 1, 2009 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.