0

I've got a form which asks some basic questions about a job, and then asks to person entering to list all those colleagues that were with them at the time. What I want to do is for all colleagues present to have a row entered in to the database - so all the other details remain the same except for their work_id (which is the identifier for the person). My code below seems to submit 1 row to the db with the workID field empty

<?php
require_once('includes/dbconnect.php');

$varWorkID = $_POST['workid'];
$varDate = $_POST['date'];
$varType = $_POST['type'];
$varSuper = $_POST['supervisor'];
$varReference = $_POST['reference'];

//Remove last part of array as extra 1 sent through by form 
$workID = array_pop($varWorkID);

for ($i=0; $i < count($workID); $i++ )
{
    mysqli_query($conn,"INSERT INTO searches (workid,date,type,super,reference) VALUES('".$workID."','".$varDate."','".$varType."','".$varSuper."','".$varReference."')");
}
echo "Completed";

I think I'm fairly close but I just need to get workid to be populated with each of the Work IDs for each of the employees present.

Any assistance greatly appreciated

5
  • This code is extremely vulnerable to SQL Injection. Consider using prepared statements. Commented Apr 30, 2016 at 20:40
  • 1
    Do var_dump($varWorkID); and see what you're getting. Commented Apr 30, 2016 at 20:53
  • array(3) { [0]=> string(5) "AB123" [1]=> string(5) "CD456" [2]=> string(0) "" } Commented Apr 30, 2016 at 21:33
  • 1
    You need change loop definition for ($i=0; $i < count($varWorkID); $i++ ) and swap $workID for $varWorkID[$i] in SQL string Commented Apr 30, 2016 at 22:44
  • Thanks Dima. Jeff credited you below too Commented May 1, 2016 at 19:08

1 Answer 1

1

the problem here is that you're calling $workID = array_pop($varWorkID); before you go into the loop for ($i=0; $i < count($workID); $i++ ), so the PHP interpreter thinks you just want to iterate over the length of that one element. what you actually wanted to do was pop an element off the array while inside the loop.

<?php
require_once('includes/dbconnect.php');

$varWorkID = $_POST['workid'];
$varDate = $_POST['date'];
$varType = $_POST['type'];
$varSuper = $_POST['supervisor'];
$varReference = $_POST['reference'];


// iterate over the count of the whole array $varWorkID = $_POST['workid']
for ($i=0; $i < count($varWorkID); $i++ )
{
    // now pop off the array inside the loop
    //Remove last part of array as extra 1 sent through by form 
    $workID = array_pop($varWorkID);
    mysqli_query($conn,"INSERT INTO searches (workid,date,type,super,reference) VALUES('".$workID."','".$varDate."','".$varType."','".$varSuper."','".$varReference."')");
}
echo "Completed";

now I think @Dima Fitiskin's comment proposes a better approach because you don't really need the expense of calling another function array_pop or throwing another variable on the stack $workID when you've already got an iterated reference to the index $i inside the loop.

<?php
require_once('includes/dbconnect.php');

$varWorkID = $_POST['workid'];
$varDate = $_POST['date'];
$varType = $_POST['type'];
$varSuper = $_POST['supervisor'];
$varReference = $_POST['reference'];


// iterate over the count of the whole array $varWorkID = $_POST['workid']
for ($i=0; $i < count($varWorkID); $i++ )
{
    mysqli_query($conn,"INSERT INTO searches (workid,date,type,super,reference) VALUES('".$varWorkID[$i]."','".$varDate."','".$varType."','".$varSuper."','".$varReference."')");
}
echo "Completed";
Sign up to request clarification or add additional context in comments.

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.