AllenHobson Posted January 6, 2011 Share Posted January 6, 2011 Hi Everyone I found some code to decrease the writing of $_POST variables especially on big forms, but I cannot seem to get it to work: I am trying to get the $_POST variables from this: <?php $callsign = $_POST['mf_callsign']; $surname = $_POST['mf_surname']; $firstnames = $_POST['mf_firstnames']; $knownas = $_POST['mf_knownas']; $rsaid = $_POST['mf_rsaid']; $birthdate = $_POST['mf_birthdate']; //more code [code] to this (see //Create $_POST Variables section) [code] <?php //Initailise Database first mysql_connect ("localhost", "***", "***") or die ('I cannot connect to the database because: ' .mysql_error()); mysql_select_db ("***"); ========================= //Create $_POST Variables foreach($_POST as $inputKey=>$inputValue): //find all form fields starting with 'mf_' if((strstr($inputKey,'mf_') === true)): $inputKey = mysql_real_escape_string($inputValue); endif; endforeach; ========================= //Then do the Insert into the Table $query="INSERT INTO personal_details (callsign, surname, firstnames, knownas, rsaid, birthdate)Values ('$mf_callsign', '$mf_surname', '$mf_firstnames', '$mf_knownas', '$mf_rsaid', '$mf_birthdate')"; mysql_query($query) or die ('Error Inserting Data into Database'); //If Insert Successful, Goto next Form header("Location: contactdetails.html"); die; ?> The //Create $_POST Variables section is not correct as it is not setting the variables correctly. If I go back to the old way and use that ($callsign = $_POST['mf_callsign'] it inserts no problem. What Am I doing wrong? Regards Allen Quote Link to comment https://forums.phpfreaks.com/topic/223598-dynamic-creating-_post/ Share on other sites More sharing options...
Zurev Posted January 6, 2011 Share Posted January 6, 2011 Off the bat, stop using ===. The triple equals sign means identical in value as well as type. So you're saying if the output of strstr (which is either false, or the part of the string is returned) so in your case would hopefully be a string is identical to true. string mf_ NOT identical to boolean true Try using the equal operator first. == Quote Link to comment https://forums.phpfreaks.com/topic/223598-dynamic-creating-_post/#findComment-1155783 Share on other sites More sharing options...
BlueSkyIS Posted January 6, 2011 Share Posted January 6, 2011 try ${$inputKey} = mysql_real_escape_string($inputValue); Quote Link to comment https://forums.phpfreaks.com/topic/223598-dynamic-creating-_post/#findComment-1155785 Share on other sites More sharing options...
AllenHobson Posted January 6, 2011 Author Share Posted January 6, 2011 Thanks People, I implemented both Suggested changes and it works a Treat... You guys Rock... thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/223598-dynamic-creating-_post/#findComment-1155789 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.