8 Replies - 190 Views - Last Post: 04 May 2019 - 08:41 AM Rate Topic: -----

#1 shiftadelete   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 04-November 16

Only last row can be updated.

Posted 04 May 2019 - 07:32 AM

I am really new to PHP. I am trying to learn it by doing projects. So please forgive me if I did something really wrong. The problem is I have several rows. But I can only update the last row.
<?php 
              $query=mysqli_query($con,"SELECT * FROM teacher");
              while ($row=mysqli_fetch_object($query)) {
                ?>
                   <tr>
                     
                     <td style='width:150px; border:2px solid grey;' ><input type="text" name="id" value="<?php echo $row->id; ?>"></td>
                     <td style='width:150px; border:2px solid grey;' ><input type="text" name="name" value="<?php echo $row->name; ?>"></td>
                     <td style='width:150px; border:2px solid grey;' ><input type="text" name="surname" value="<?php echo $row->surname; ?>"></td>
                     <td style='width:150px; border:2px solid grey;' ><input type="text" name="dept_name" value="<?php echo $row->dept_name; ?>"></td>
                     <td style='width:150px; border:2px solid grey;'><button type="submit" id="btnUpdate">Update</button></td>
                   </tr>

                <?php
              }
             ?>
          </table>
           
             
        </form>
        <?php 

          if($_GET){

             $id=$_GET['id'];
             $name=$_GET['name'];
             $surname=$_GET['surname'];
             $dept_name=$_GET['dept_name']; 


             $query="UPDATE teacher SET name='$name', surname='$surname', dept_name='$dept_name' Where id='$id'";
             $data=mysqli_query($con,$query);
             if($data){
               echo "data updated";
             header("Location:updateteacher.php");
              ob_end_flush();
             }
         }


         ?>


Is This A Good Question/Topic? 0
  • +

Replies To: Only last row can be updated.

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2835
  • View blog
  • Posts: 11,125
  • Joined: 03-December 12

Re: Only last row can be updated.

Posted 04 May 2019 - 07:40 AM

Prepared statements first of all. Why are you using get when it should be post? Iím wondering how that doesnít throw you into a loop to begin with.
Was This Post Helpful? 0
  • +
  • -

#3 shiftadelete   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 04-November 16

Re: Only last row can be updated.

Posted 04 May 2019 - 07:48 AM

astonecipher
Now used the query inside for loop. Again does not work.
if($_POST){

             $id=$_POST['id'];
             $name=$_POST['name'];
             $surname=$_POST['surname'];
             $dept_name=$_POST['dept_name']; 

             $count = count($_POST['id']);
             if($count>0){
               for($i=0;$i<$count;$i++){
             $query="UPDATE teacher SET name='$name', surname='$surname', dept_name='$dept_name' Where id='$id'";
             $data=mysqli_query($con,$query);
             if($data){
               echo "data updated";
             header("Location:updateteacher.php");
              ob_end_flush();
             }
           }
         }
        }

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2835
  • View blog
  • Posts: 11,125
  • Joined: 03-December 12

Re: Only last row can be updated.

Posted 04 May 2019 - 07:51 AM

Does the form send it as post?
Was This Post Helpful? 0
  • +
  • -

#5 shiftadelete   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 04-November 16

Re: Only last row can be updated.

Posted 04 May 2019 - 07:52 AM

astonecipher Yes.
<form action="" method="post">

This post has been edited by shiftadelete: 04 May 2019 - 07:52 AM

Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2835
  • View blog
  • Posts: 11,125
  • Joined: 03-December 12

Re: Only last row can be updated.

Posted 04 May 2019 - 07:53 AM

Wait a minute... you are looping thru giving all the input elements the same name. So of course you can only send one in, the last one that was added by the loop. Are you trying to send an array?
Was This Post Helpful? 0
  • +
  • -

#7 shiftadelete   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 04-November 16

Re: Only last row can be updated.

Posted 04 May 2019 - 08:07 AM

astonecipher Well, I did not understand what you mean by " trying to send an array" But this is my interface https://imgur.com/8tsySsa
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2835
  • View blog
  • Posts: 11,125
  • Joined: 03-December 12

Re: Only last row can be updated.

Posted 04 May 2019 - 08:25 AM

You have several issues. The biggest is not using prepared statements. Next you think you are getting more values than you actually are. Lastly, you are only using the first values sent in, even if you were to be getting what you think.



if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
}


If you want the button to work for each line, you need the form to be for each line, not all of them.
Was This Post Helpful? 0
  • +
  • -

#9 shiftadelete   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 04-November 16

Re: Only last row can be updated.

Posted 04 May 2019 - 08:41 AM

astonecipher I wrote form tags inside while loop, now it works! And update buttons for each line does not cause any issues. Thanks for your time sir.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1