Click here to Skip to main content
13,351,156 members (31,494 online)
Rate this:
 
Please Sign up or sign in to vote.
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 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
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 28-Apr-14 5:29am
Updated 28-Apr-14 5:36am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 30 Apr 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100