-5
<?php
include 'connection.php';  
 ?>
<form action="update.php" method="post">
<?php 
$grp=$_GET['group_num'];
$query = "SELECT * FROM lectures_table WHERE group_num=$grp";
$result = mysql_query($query);

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

</div>   
 <input type="hidden" name="group_num" value="<?php echo $grp;  ?>" />
  <input type="text" placeholder="First Lecture" name="day[]" value="<?php             
 echo $row['day']; ?>"></td>
    <input type="text" placeholder="First Lecture" name="f[]" value="<?php      

 echo $row['first']; ?>"></td><br>

 </tr>

 <?php 


 } ?>

</tbody>
 </table>         
   <input type="submit" class="btn btn-success" name="edit" value="save">
  &emsp;&emsp;
 </form>
  </div>
    </div>
     </div>
 </div> ?>
 </body>
  </html>

update.php

    <?php
    include 'connection.php';
     $i = 0; 
   while ($i < 5) { 
   // define each variable 
   $scoreaway = $_POST['f'][$i]; 

   // do the update and print out some info just to provide some visual feedback 
   echo $query = "UPDATE lectures_table SET first='".$scoreaway."' WHERE           
   group_num=1"; 
   mysql_query($query) or die ("Error in query: $query");  

   $i++; 
   }

   mysql_close();  

   ?>

The update query just updates the last row. What should I do I need it importantly.

When I submit my form all values take the value of last input ??

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The `UPDATE` query is going to update any and all rows where `group_num=1`, because that's *exactly* what's been written in the query. – David May 14 '16 at 11:47
  • 2
    Use `$_POST['group_num']` instead of `1` in the query. – Barmar May 14 '16 at 11:48
  • There's no need to put the hidden input inside the `while()` loop, since it's the same value for every row. Do it once outside the loop. – Barmar May 14 '16 at 11:49
  • your given group_num=1 in where condition so values changing dynamically but same condition so last value updated – JYoThI May 14 '16 at 11:53

1 Answers1

0

You need to change the form so that each row has a hidden input containing the ID of that row.

while($row=mysql_fetch_array($result)){   ?>
<tr>  
    <td><input type="hidden" name="id[]" value="<?php echo $row['id'];  ?>" /></td>
    <td><input type="text" placeholder="First Lecture" name="day[]" value="<?php echo $row['day']; ?>"></td>
    <td><input type="text" placeholder="First Lecture" name="f[]" value="<?php echo $row['first']; ?>"></td>

</tr>
<?php }

Then use that ID in the UPDATE statement, not group_num.

foreach ($_POST['id'] as $i => $id) {
    $scoreaway = $_POST['f'][$i];
    $day = $_POST['day'][$i];
    $query = "UPDATE lectures_table 
                SET first='$scoreaway', day = '$day' 
                WHERE id = $id";
    mysql_query($query);
}

Also, you should switch from the obsolete mysql extension to mysqli or PDO, and use prepared statements. Your current code is vulnerable to SQL injection. See How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • sir i tried it but nothing change i don't know why the last row give it's alue to other rows – Alex Andrea May 14 '16 at 11:57
  • Because the `UPDATE` statement is using the same `WHERE group_num = $_POST['group_num']`` for each input. Is there an ID in the row that it should use to update just that row? – Barmar May 14 '16 at 12:12
  • i get error message Undefined index: id Invalid argument supplied for foreach() what should i do – Alex Andrea May 14 '16 at 13:59
  • Obviously you need to replace that with the actual name of the column that contains the primary key for each row. – Barmar May 14 '16 at 14:01
  • I don't know your table structure, I guessed it would be named `id`, since that's pretty common. – Barmar May 14 '16 at 14:01