0

I try to write data from a form I save in an array into my database. Unfortunately it is not working:

        $hardCodedTitles = array(
                    array("Lead inserted by", $_POST["input_lead_insert"]),
                    array("Staffname", $_POST["input_Staffname"]),
                    array("Staff email", $_POST["input_Staffemail"]),
                    array("Workplace", $_POST["input_Workplace"]),
                    array("Gender", $_POST["input_gender"]),
                    array("First Name", $_POST["input_Firstname"]),
                    array("Last Name", $_POST["input_Lastname"]),
                    array("Company", $_POST["input_Company"]),
                    array("Job Position", $_POST["input_Jobposition"]),
                    array("Industry", $_POST["input_Industry"]),
                    array("Mailstop", $_POST["input_Mailstop"]),
                    array("Street", $_POST["input_Street"]),
                    array("Zip Code", $_POST["input_ZIP-Code"]),
                    array("City", $_POST["input_City"]),
                    array("Country", $_POST["input_Country"]),
                    array("Email", $_POST["input_E-Mail"]),
                    array("Phone Number", $_POST["input_PhoneNumber"]), 
                );
                foreach($hardCodedTitles as $title) {
                    if(isset($_POST[$title[1]]) && $_POST[$title[1]] != "") {
                        mysql_query("INSERT INTO lead_data (lead_id, title, value) VALUES ($createdID, '".$title[0]."', '".$title[1]."')");
                    }
                }

Does anyone see the error? It looks like the data is stored correctly in the array when I use print_r()...

6
  • "Does anyone see the error?" - Do you, or are you? and/or what does mysql_error() say? You're assuming success right off the bat without taking possible errors into account. Commented Oct 6, 2014 at 22:44
  • The error is that you're posting god knows what garbage into your database, using PHP functions that have been deprecated for years. Commented Oct 6, 2014 at 22:50
  • where is $createdID defined? Commented Oct 6, 2014 at 22:50
  • 1
    What does "doesn't work" mean? "Doesn't work" is an inadequate description for us to understand the problem. What happened when you tried it? Did you get an error message? Did you get incorrect results? Did you get no results? If the results were incorrect, what made them incorrect? What were you expecting instead? Did you get any correct results? If so, what were they? Don't make us guess. Commented Oct 6, 2014 at 22:51
  • 1
    So $title[1] is the value posted from userland, but you're looking for $_POST[$title[1]]? I can't even follow what this is trying to do... Commented Oct 6, 2014 at 22:59

4 Answers 4

2

This will work:

foreach($hardCodedTitles as $title) {
                    if(isset($title[1]) && $title[1] != "") {
                        mysql_query("INSERT INTO lead_data (lead_id, title, value) VALUES ($createdID, '".$title[0]."', '".$title[1]."')");
                    }
}

Don't forget to sanitize that user input, lest you be left with a hacked or corrupt database!

Sign up to request clarification or add additional context in comments.

Comments

1

it appears that you are dealing with $_POST[$_POST['something']]. Try get away $_POST[] around the keys in first array

Comments

1

You don't need to check if $_POST[$title[1]) is set or is not blank because it is not set and is blank!

Instead just use the $title[1], like so:

(edit: the below code is for your learning/debugging :) )

$_POST["input_lead_insert"] = 1;
$_POST["input_Staffname"] = 2;
$_POST["input_Staffemail"] = 3;
$_POST["input_Workplace"] = 4;
$_POST["input_gender"] = 5;
$_POST["input_Firstname"] = 6;
$_POST["input_Lastname"] = 7;
$_POST["input_Company"] = 8;
$_POST["input_Jobposition"] = 9;
$_POST["input_Industry"] = 10;
$_POST["input_Mailstop"] = 11;
$_POST["input_Street"] = 12;
$_POST["input_ZIP-Code"] = 13;
$_POST["input_City"] = 14;
$_POST["input_Country"] = 15;
$_POST["input_E-Mail"] = 16;
$_POST["input_PhoneNumber"] = 17;

$hardCodedTitles = array(
    array("Lead inserted by", $_POST["input_lead_insert"]),
    array("Staffname", $_POST["input_Staffname"]),
    array("Staff email", $_POST["input_Staffemail"]),
    array("Workplace", $_POST["input_Workplace"]),
    array("Gender", $_POST["input_gender"]),
    array("First Name", $_POST["input_Firstname"]),
    array("Last Name", $_POST["input_Lastname"]),
    array("Company", $_POST["input_Company"]),
    array("Job Position", $_POST["input_Jobposition"]),
    array("Industry", $_POST["input_Industry"]),
    array("Mailstop", $_POST["input_Mailstop"]),
    array("Street", $_POST["input_Street"]),
    array("Zip Code", $_POST["input_ZIP-Code"]),
    array("City", $_POST["input_City"]),
    array("Country", $_POST["input_Country"]),
    array("Email", $_POST["input_E-Mail"]),
    array("Phone Number", $_POST["input_PhoneNumber"]), 
);
foreach($hardCodedTitles as $title) {
    if(isset($title[1]) && $title[1] != "") {
        // will only display the numbers for the purpose of your debugging
        echo $title[1]."<br>";
    }
}

Comments

0

Try check the array $title using print_r within each loop, and see if you get the right array you want.

maybe you can try

foreach($hardCodedTitles AS $title) {
   foreach($title AS $key=>$value ) {
      echo $key ." ". $value ."<br>";
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.