Jump to content

Updating data in a database


taz321

Recommended Posts

Hi

 

Im having abit of trouble updating data in the database.

 

I have displayed a list of people who have the attributes, EmpID, Name, firstname, surname ect and have placed a link around the name attribute which goes to the update form.

The name which i click on should display all its contents in the update form but it doesnt.

 

The code is shown below for this -

 

<a href="EditUserForm.php?employeeID=<?=$row['employeeID']?>"><?php echo $row['firstname'];?></div></td>

 

i have below the form from where im going to manually update the existing data below.

 

<?php
session_start();

require "connect.php";
$Username = $_SESSION['employeeID'];
$query = "select * from employee where employeeID = '".$_SESSION['employeeID']."'";
$result = mysql_query($query, $connection) or die ("Unable to perform query<br>$query");
$row= mysql_fetch_array($result);
?>
<body>
<h1 align="center">Edit person Details </h1>

<form action="EditUser.php" method="get">

<p>employeeID:<br/>
<input name="employeeID" type="text" readonly="true" value=" <?=$row['employeeID']?>" >
</p>

<p>title:<br/>
<input name="title" type="text" value="<?=$row['title']?>">
</p>

<p>firstname:<br/>
<input name="firstname" type="text" value="<?=$row['firstname']?>">
</p>

<p>surname:<br/>
<input name="surname" type="text" value="<?=$row['surname']?>">
</p>

<p>username:<br/>
<input name="username" type="text" value="<?=$row['username']?>">
</p>

<p>Password: <br/>
<input name="password" type="password" value="<?=$row['password']?>">
</p>
<input name="Save" type="submit" value="Update">
</form>

</body>
</html>

 

AND this is the PHP code (EditUser.php) which the above calls in order for the updating to work

 

<?php
require "connect.php";
$employeeID = $_SESSION['employeeID'];
$title = $_SESSION['title'];
$firstname = $_GET['firstname'];
$surname = $_GET['surname'];
$username = $_GET['username'];
$password = $_GET['password'];

$query = "update employee set employeeID='".$employeeID."',title= '".$title."', firstname = '".$firstname."', surname = '".$surname."', username = '".$username."',    password = '".$password."'";
$result = mysql_query($query, $connection) or die ("Unable to perform query<br>$query");
header ("Location: EditUserForm.php");
exit(); 

?>

 

When i look at the form, it doesnt appear to show me any data to edit, im abit confused.

 

Any help would be grateful.

 

 

MOD EDIT: code tags added

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/83065-updating-data-in-a-database/
Share on other sites

I have just noticed that in this bit of the code it says $_SESSION instead of $_GET as you have rightfully pointed out.

 

<?php

require "connect.php";

$employeeID = $_GET['employeeID'];

$title = $_GET['title'];

$firstname = $_GET['firstname'];

$surname = $_GET['surname'];

$username = $_GET['username'];

$password = $_GET['password'];

 

i have changed but but nothing seems to be working yet ?

 

Thanks just read those links you gave me and have got a better understanding now.

 

The code below for the form where it gets the data from the database, is it correct ?

 

<?php

require "connect.php";

$employeeID = $_GET['employeeID'];

$title = $_GET['title'];

$firstname = $_GET['firstname'];

$surname = $_GET['surname'];

$username = $_GET['username'];

$password = $_GET['password'];

 

$query = "update employee set employeeID='".$employeeID."',title= '".$title."', firstname = '".$firstname."', surname = '".$surname."', username = '".$username."',    password = '".$password."'";

$result = mysql_query($query, $connection) or die ("Unable to perform query<br>$query");

header ("Location: EditUserForm.php");

exit();

 

?>

 

 

 

Sorry im not sure if this will help, but i have displayed the contents from the database in another sheet and below is a sample of the code, there is a link around the "name" attribute and i was hoping that once this is clicked the user would go to the form and have displayed the details of that particular person ready for me to edit.

 

<td width="130"><div align="center" class="style7">

<a href="EditUserForm.php?employeeID=<?=$row['employeeID']?>"><?php echo $row['firstname'];?></div></td>

 

 

    <td width="130"><div align="center" class="style7"><?php echo $row['surname'];?></div></td>

    <td width="130"><div align="center" class="style7"><?php echo $row['username'];?></div></td>

    <td width="130"><div align="center" class="style7"><?php echo $row['password'];?></div></td>

    <td width="80"> </td>

You shoudn't be updating the employeeID. You should be using that to identify the record to be updated (otherwise all records in the table will be set to those values)

 

<?php
$query = "update employee SET
    title= '$title', 
    firstname = '$firstname', 
    surname = '$surname', 
    username = '$username',    
    password = '$password'
    WHERE employeeID = '$employeeID' ";
?>  

no you didn't understood what $_GET means

look try something like this  :

require "connect.php";
$Username = $_SESSION['employeeID'];
$query = mysql_query("select * from employee where employeeID = '$Username') or die("".mysql_error());
if( mysql_num_rows($query)) {
   $row = mysql_fetch_array($query);
      }

 

i hope it helps

$_GET gets variables from the URL like: www.example.com/test.php?employee=4

 

echo $_GET['employee'] --> outputs 4

 

If you want to get the information from the database, you can use a select statement, then iterate through the row to get what you're looking for.

 

Hope this helps.

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.