Jump to content

UPDATING data through a php form


Recommended Posts

I am new to programming and i wanted to see how i can update data that is already in my database through an edit button, but i want it to edit only that member_id (primary key)

 

my database is composed of the following

 

member_id (auto increment)

firstname

lastname

login

password

usertype

 

$sql = mysql_query("SELECT * FROM newUsers2 ORDER BY member_id ASC");

$id = 'member_id';
$fname = 'firstname';
$lname = 'lastname';
$login = 'login';


echo '<ul class="list">' ; 
echo  '<li class="heading">';
echo '<span class="id">ID </span>' ;
echo '<span class="firstname">FIRST NAME </span>' ;
echo '<span class="lastname">LAST NAME </span>' ;
echo '<span class="login">LOGIN </span>' ;
echo '<span class="userType">ADMIN RIGHTS</span>';
echo  '<span class="edit">EDIT</span>'; 
echo '</li>' ;  



while ($rows = mysql_fetch_assoc($sql)){
if($rows['userType'] == 1) {
$userTypeValue = "YES";
} else {
$userTypeValue = "NO";
}
echo '<li class="entries">' ;
echo '<span class="id">' .$rows[$id]. '</span>' ;
echo '<span class="firstname">' .$rows[$fname]. '</span>' ;
echo '<span class="lastname">' .$rows[$lname]. '</span>' ;
echo '<span class= "login">' .$rows[$login]. '</span>';
echo  '<span class="userType">' .$userTypeValue. '</span>' ;
echo  '<span class="edit"> <a href="edituser.php">EDIT</a> </span>'; 
echo '</li>' ;
}	  
echo '</ul>' ;  
?>   

 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/240922-updating-data-through-a-php-form/
Share on other sites

In the Edit link you need to pass the member id as a parameter of the anchor tag. Such as:

echo  "<span class=\"edit\"> <a href=\"edituser.php?id={$rows[$id]}\">EDIT</a> </span>"; 

 

Then on the edituser.php page you can access that ID value using $_GET['id']. On that page you can use that value to 1) run a query to get the current values to populate the form and 2) populate a hidden field in that form with the ID. Then when the user POSTs that page take the ID and the other form field values (escape the values) and run an update query. However, if this page does not require authentication anyone can edit any record by trying different IDs in the URL

how would you structure a update query i know for inserting i used this, but how would i structure a Update Query?

 

//Create INSERT query
$qry = "INSERT INTO newUsers2(firstname, lastname, login, passwd, userType) VALUES('$fname','$lname','$login','".md5($_POST['password'])."', '$userType')";
$result = @mysql_query($qry);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.