1

I have a weird problem with array_combine() function. In fact, I've got two arrays of the same length ($skills and $skills_level), the first array contains checked checkboxes from a html form, and second one contains associated options. I'd like to iterate them simultaneously and insert their elements in a DB table:

foreach(array_combine($skills, $skills_level) as $skill => $skill_level) {
 $insert_skills = "INSERT INTO worker_skills(worker_username, skill, level) VALUES('$worker_username', '$skill', '$skill_level')";
 $query1 = mysqli_query($conn, $insert_skills);

It all goes well apart from one particular value, which is the first checkbox of the form. I'm pretty sure it goes into the $skills array (I tested it), but for some reason it's not being inserted into table row, neither it is its associated value from array $skills_level. I tried different methods (getting array keys separately, using a for loop, etc.) but the problem persists. Can anyone help me with that?

Joey
  • 15
  • 4
  • What do you get with `print_r(array_combine($skills, $skills_level))`? – asprin Aug 02 '18 at 05:26
  • Can you post var_export([$skills,$skills_level]);? – vuliad Aug 02 '18 at 05:28
  • It seems, your skills array contains the first value more than once... Try to check this first... – Elementary Aug 02 '18 at 05:29
  • 1) Can you give us an example of skills,skills_level ? print_r is enough. 2) Its better to combine the insert statements into one statement , comma separated and execute it once out side the foreach . – Ashraf Aug 02 '18 at 05:31
  • @asprin it always prints the correct values – Joey Aug 02 '18 at 05:38
  • @vuliad array ( 0 => array ( 0 => 'Originality\'', 1 => 'Problem solving', ), 1 => array ( 0 => '3', 4 => '3', ), ), it seems to be correct – Joey Aug 02 '18 at 05:46
  • @Elementary I already tested it, it prints out only once – Joey Aug 02 '18 at 05:47
  • @Ashraf Array ( [Originality] => 3 [Problem solving] => 1 ) this is what print_r shows with two skills and their levels (the first one is the skill that isn't accepted by the query). How can I execute it once outside the foreach? – Joey Aug 02 '18 at 05:52
  • Try to debug the sql error : `if(! mysqli_query(....)) echo mysqli_error($con);` . This might give you the db error (duplicate entry etc .. ) . – Ashraf Aug 02 '18 at 05:57
  • For your second question : Inside the loop `$insterts[]= 'INSERT INTO ......';` and then after the loop `mysqli_query($conn,implode(";",$insterts));` – Ashraf Aug 02 '18 at 06:00
  • @Ashraf mysql_error() expects parameter 1 to be resource, object given – Joey Aug 02 '18 at 06:02
  • @Joey its `mysqli_` not `mysql` – Ashraf Aug 02 '18 at 06:03
  • @Ashraf apparently it doesn't recognize mysqli_error as a function, only mysql_error – Joey Aug 02 '18 at 06:10
  • See here : https://stackoverflow.com/a/17053501/1140136 – Ashraf Aug 02 '18 at 06:14

2 Answers2

2

You probably have two skills that have the same name. Since there can't be two equal array keys you will end up with an shortened array.

$skills = [ 'a', 'b', 'a' ];
$levels = [ 1, 2, 3 ];
var_dump(array_combine($skills, $levels));


array(2) {
  'a' =>
  int(3)
  'b' =>
  int(2)
}
Karol Samborski
  • 2,595
  • 8
  • 18
1

The problem is that when you include the first data item 0 => 'Originality\'', there is a quote in the text. As you build the string up with quote delimeters, this breaks the format. You end up with...

INSERT INTO worker_skills(worker_username, skill, level)
    VALUES('username', 'Originality'', 'level')";

This can also be a problem with other text as well. You should switch to using prepared statements and insert the data something like...

$insert = $conn->prepare("INSERT INTO worker_skills(worker_username, skill, level) 
                            VALUES(?, ?, ?)");
foreach(array_combine($skills, $skills_level) as $skill => $skill_level) {
    $insert->bind_param("sss", $worker_username, $skill, $skill_level);
    $insert->execute();
}

(I've not tested this, but let me know if your still having problems).

Nigel Ren
  • 53,991
  • 11
  • 38
  • 52