Jump to content

Dynamic Creating $_POST


AllenHobson

Recommended Posts

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

Link to comment
Share on other sites

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. ==

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.