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()...

Toby
  • 37
  • 1
  • 7
  • *"Does anyone see the error?"* - [**Do you, or are you?**](http://php.net/manual/en/function.error-reporting.php) and/or what does `mysql_error()` say? You're assuming success *right off the bat* without taking possible errors into account. – Funk Forty Niner Oct 06 '14 at 22:44
  • The error is that you're posting [god knows what garbage](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) into your database, using PHP functions that have been [deprecated for years](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – miken32 Oct 06 '14 at 22:50
  • where is `$createdID` defined? – Jonathan Kuhn Oct 06 '14 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. – Andy Lester Oct 06 '14 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... – miken32 Oct 06 '14 at 22:59
  • You should check/display mysql error after you execute query. – Kamil Oct 06 '14 at 23:14

4 Answers4

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!

MattWithoos
  • 341
  • 1
  • 15
1

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

zuups
  • 1,140
  • 1
  • 11
  • 17
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>";
    }
}
MattWithoos
  • 341
  • 1
  • 15
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>";
   }
}
Li Kia Chiu
  • 213
  • 1
  • 6