开发者

how to edit data the data stored in database and which form it should be retrived back

HI everyone

I have a html application form which have username,password,age etc .NOw i am able to store these values into database without any problem.Now i want to add a edit button if user want to edit his profile..

1.First tell me where i should put this Edit Button.I think i should place it into user'home page where he is redirected after confirming username and password...

Now i want to know how he can do edit opearation in existing data which is stored in database. I know very well how to ret开发者_如何学运维rive data from database.Now tell me in which form this data should be retrived from database..Should i retrive it in that application form agina(how) or i should retrive it in a file(but then how to send back).

I have no idea what to do and please tell me how this data will be updated(i think at the place of insert command i should use update command)..Plz tell me in deatil.I will be thankfull to you


By my opinion the edit ubtton must be in the user profile. As for the update - create primary key ofr one of the columns and update by it, something like

$sql = 'UPDATE table SET username='.$user_name.', password ='.$pass.' WHERE id = '.$user_id

Dont forget to make server side validation of the data too.

Here is simple solution:

<?php
if(isset($_GET['user_id'))
{
  $sql = 'SELECT id, username, password from users_table WHERE id='.$users_id;

  if ($result = $mysqli_object->query($sql)) {
        $row = $result->fetch_assoc() 
  }
  echo '<form action="" method="post">';
  echo '<input type="text" name="username" value="'.$row['username'].'"';
  echo '<input type="password" name="password" value="'.$row['password'].'"';
  echo '<input type="hidden" name="id" value="'.$row['id'].'"';
  echo '<input type="submit" value="save" />';
}elseif(isset($_POST['id'])){
  $user_name  = $_POST['username'];
  $pass  = $_POST['password'];
  $user_id  = $_POST['id'];

  $sql = 'UPDATE table SET username='.$user_name.', password ='.$pass.' WHERE id = '.$user_id
  if($result = $mysqli_object->query($sql))
    echo 'Profile updated';
  else
    echo $mysqli_object->error;
}else{
  echo 'You are not supposed to be on this page!';
}  

?>

I haven`t tested it so it could have any syntax mistakes. You can read a little bit more for the mysqli (MySQL Improved Extension) in the PHP documentation.

If you are trying to develop an application(not just playing with PHP) you should better try some of the available free frameworks, they will do most of the work instead of you.

Good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜