0

so i'm making a php file that updates multiple rows to mysql but I'm having a problem whenever i submit,and I have no idea if I'm using foreach well. here is my code:

$query = "SELECT id, departments, deptName, headOfOffice FROM aip";
$result = mysqli_query($db,$query);
$count = mysqli_num_rows($result);

while ($row = mysqli_fetch_assoc($result)) {

        echo '<tr>';
        echo '<td><input type="text" name="id[]" value="'.$row['id'].'" readonly></td>';
        echo '<td><input type="text" id ="department_code" name="department_code[]" value="'.$row['departments'].'"></td>';
        echo '<td><input type="text" id="department_name" name="department_name[]" value="'.$row['deptName'].'"></td>';
        echo '<td><input type="text" id="department_head" name="department_head[]" value="'.$row['headOfOffice'].'"></td>';
        echo '</tr>';

}

echo '<tr>';
echo '<td></td>';
echo '<td></td>';
echo '<td><input type="submit" name="update" value="Update">';
echo '</tr>';



if($_SERVER["REQUEST_METHOD"] == "POST"){

$deptid = $_POST['id'];
$code = $_POST['department_code'];
$dname = $_POST['department_name'];
$dhead =$_POST['department_head'];

foreach($_POST['id'] as $count){ \\ i don't know if this is right.

$query2 = "UPDATE aip SET deparments = '".$code[$count].'" WHERE id = "'.$deptid[$count]."'";
$result2 = mysqli_query($db,$query2);
}

 }

the error says "Undefined offset: 2" I'm a newbie here, and this is my first time using arrays. hope someone could help. please!

3
  • It's probably here deparments = '".$code[$count].'" Commented Feb 9, 2017 at 9:10
  • @GurV so what should i do about it? Commented Feb 9, 2017 at 9:14
  • 2
    You should use prepared statements, which make errors like this almost not possible. Also, you might be able to do a single UPDATE instead of looping. Commented Feb 9, 2017 at 9:14

3 Answers 3

3
foreach($_POST['id'] as $count => $id){
    $query2 = "UPDATE aip SET deparments = '".$code[$count]."' WHERE id = '".$deptid[$count]."'";
    $result2 = mysqli_query($db,$query2);
}

P.S. your code is vulnerable to SQL injection

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

2 Comments

i already made the changes, the errors are gone but still it doesn't update.
place updating code at the start of a file otherwise you don't see changes. Try to refresh page to see your changes.
0

There are two ways of using foreach:

foreach($array as $key => $value){
    ...
}

or

foreach($array as $value){
    ...
}

The foreach loop will then iterate through all elements in array, executing the code in brackets for each element once, holding the key name and the value of the element in $key and $value respectively (you can use other than $value and $key as well).

I don't fully understand what exactly you are doing in your code, so if you add an explanation, I will be able to solve your particular case;

Comments

0

i have changed in your script..pls used my script. you do not need to changes any thing just copy and paste my script.

    if($_SERVER["REQUEST_METHOD"] == "POST"){
         $data = $_POST;

        if(count($data['id'])>0){
          foreach($data as $key=>$item){
              $query2 = "UPDATE aip SET deparments = '".$item['department_code'][$key]."', deptName = '".$item['department_name'][$key]."',headOfOffice = '".$item['department_head'][$key]."'  WHERE id = '".$item['id'][$key]."'";
              $result2 = mysqli_query($db,$query2);
        }
   } 
}

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.