How to update the database using `id`?

advertisements

i want to update my database using id, I have already a database which have their name

Now when i update my database using WHERE college='1' it works successfully but when i update my database using id it's not working please help, and my database id=1 for which i'm working for..

here is my source code:

<?php
$con=mysqli_connect("localhost","root","Bhawanku","members");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM admin");

while($row = mysqli_fetch_array($result))
        {
            echo "(".$row['id'].") ".$row['first_name']." ".$row['last_name'];
        }

mysqli_query($con,"UPDATE admin SET first_name='Rajendra', last_name='Arora'
WHERE id='$id'");

mysqli_close($con);
?>

EDITED

after putting $id it's showing an error undefined variable id.. what's that meaning?


id is a number, not a string. Change it to:

mysqli_query($con,"UPDATE admin SET first_name='Rajendra', last_name='Arora' WHERE id=".$row['id']);

EDIT Your id is only used inside your loop. Try changing it from while to if

$result = mysqli_query($con,"SELECT * FROM admin limit 0, 1");
if ($row = mysqli_fetch_array($result)) {
    mysqli_query($con,"UPDATE admin SET first_name='Rajendra', last_name='Arora' WHERE id=$row['id']");
}

Any other way, using while, will change all user names in the table. If you have more that one row in this table you'll need another approach.