Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello codeproject member, iam beginning starting dynamic web programming using PHP.
in this case i am learning CRUD using PHP MySQL but iam get some problem on editing database. Need some help and suggestion. thank you

this is my php code named as adminedit.php
when i saw in the database, data not changed..

PHP
<?php include "base.php"; 
	 
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Form Edit Data</title>
</head>

<body>
<table border=1>
  <tr>
    <td align=center>Form Edit Data</td>
  </tr>

<tr><td>

<table>
<form method="post" action="admineditprocess.php">
  	  <?php
	  $id = ereg_replace('[^0-9]', '', $_GET['id']);
      $result = mysql_query("SELECT * FROM users where UserID=$id");
	  while($row = mysql_fetch_array($result))
	  {
	  $nama =  $row['Username'];
	  $pass =  $row['Password'];
	  $email = $row['EmailAddress'];
	  $privilege = $row['privilege'];
	  }
      ?>

<input type="hidden" name="id" value="<? echo $id;?>">

<tr>        
<td>Username</td>
<td>
<input type="text" name="username" size="80" value="<?php echo $nama;?>">
</td>
</tr>

<tr>
<td>Password</td>
<td>
<input type="text" name="password" size="80" value="<?php echo $pass;?>">
</td>
</tr>

<tr>
<td>Email Address</td>
<td>
<input type="text" name="email" size="80" value="<?php echo $email;?>">
</td>
</tr>

<tr>
<td>Privilege</td>
<td>
<input type="text" name="privilege" size="80" value="<?php echo $privilege?>">
</td>
</tr>

<tr>
<td align="left">
<input type="submit" name="submit value" value="Edit">
</td>
</tr>

</form>
</table>
</td></tr>
</table>

</body>
</html>


this is admineditprocess.php

PHP
<?php
include "base.php";
$id = $_POST["id"];
$name = $_POST["username"];
$password = $_POST["password"];
$emailaddress = $_POST["email"];
$privilege = $_POST["privilege"];
$order = "UPDATE users 
          SET Username='$name', 
              Password='$password',
              EmailAddress='$emailaddress',
			  privilege='$privilege'
          WHERE 
	      UserID='$id'";
mysql_query($order);
header("location:adminpanel.php");
?>
Posted
Updated 28-Apr-14 4:36am
v2

Did you forget to enclose the $id with single quotes:
$result = mysql_query("SELECT * FROM users where UserID='$id'");

However, mysql_query has been deprecated, should use mysqli, and use prepared-statements-in-php-and-mysqli/[^] to prevent sql injection.
++++++++++++++[round 2]++++++++++++++++++++++++
Do you understand your code? Did you get it from somewhere?
The exit($id); will stop the script and print out the value of $id, it is a way to check if the $id contains any value, apparently it did not, that was the reason why the update sql did not update because the WHERE condition was not met. Where is the source of this $id? it was from adminedit.php, that was the reason I suggest the original solution in solution 1.
 
Share this answer
 
v6
Comments
Gun Gun Febrianza 28-Apr-14 11:32am    
i track the problem code is here dear,

$sql = "UPDATE users SET Username='$name', Password='$password', EmailAddress='$emailaddress', privilege='$privilege' WHERE UserID='$id'";

$result=mysql_query($sql);
Peter Leow 28-Apr-14 11:58am    
Try exit($id); before the update query and see its value.<br>
Gun Gun Febrianza 30-Apr-14 5:40am    
exit($id); <- iam add this still i get nothing changed in my database..
$sql = "UPDATE users SET Username='$name', Password='$password', EmailAddress='$emailaddress', privilege='$privilege' WHERE UserID='$id'";
$result=mysql_query($sql);
Peter Leow 30-Apr-14 6:52am    
see my reply in solution 1.
First, you need to know why it is failing. Gun Gun and Peter both provided code to capture the status in $result. Then, you need to see the value of that result. From there, you can work on a fix. Until then, there is not much that we can do to help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900