Hi all..
Can anyone help me with this problem?
This code already said successfully update and successfully delete but actually there is no data updated or deleted.
There is no error shows too..
These are the code for update and delete data..

update_user.php

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pqs", $con);
$ref1 = $_REQUEST['ref1'];
$result = mysql_query("SELECT User_tcno, User_name, User_dep, User_username, User_password, User_level FROM userdb WHERE User_id = '$ref1' LIMIT 0,1");
?>
<form action="update_user1.php" method="post" name="form1" id="form1">

  <p>&nbsp;</p>
  
  <?php while($row = mysql_fetch_array($result))
				{

				?>
				
  <table width="545" border="0" align="center" bordercolor="#F0F0F0">
    <tr bordercolor="#F0F0F0" bgcolor="#FFFFFF">
      <th width="196" scope="col"><div align="right" class="style9">User TC No. </div></th>
      <th width="5" scope="col"><div align="center"><strong>:</strong></div></th>
      <th width="330" scope="col"><div align="left">
        <input name="User_tcno" type="text" id="User_tcno" value="<?php echo $row['User_tcno'];?>" size="40" />
      </div></th>
    </tr>			
    <tr bordercolor="#F0F0F0" bgcolor="#FFFFFF">
      <th scope="row"><div align="right" class="style9">User Name </div></th>
      <td><div align="center"><strong>:</strong></div></td>
      <td><input name="User_name" type="text" id="User_name" value="<?php echo $row['User_name'];?>" size="40" /></td>
    </tr>
    <tr bordercolor="#F0F0F0" bgcolor="#FFFFFF">
      <th scope="row"><div align="right" class="style9">Department</div></th>
      <td><div align="center"><strong>:</strong></div></td>
      <td><input name="User_dep" type="text" id="User_dep" value="<?php echo $row['User_dep'];?>" size="40" /></td>
    </tr>
    <tr bordercolor="#F0F0F0" bgcolor="#FFFFFF">
      <th scope="row"><div align="right" class="style9">Access Username </div></th>
      <td><div align="center"><strong>:</strong></div></td>
      <td><input name="User_username" type="text" id="User_username" value="<?php echo $row['User_username'];?>" size="40" /></td>
    </tr>
    <tr bordercolor="#F0F0F0" bgcolor="#FFFFFF">
      <th scope="row"><div align="right" class="style9">Access Password </div></th>
      <td><div align="center"><strong>:</strong></div></td>
      <td><div align="left">
        <input name="User_password" type="text" id="User_password" value="<?php echo $row['User_password'];?>" size="40" />
      </div></td>
    </tr>
    <tr bordercolor="#F0F0F0" bgcolor="#FFFFFF">
      <th height="24" scope="row"><div align="right" class="style9">Access Level </div></th>
      <td><div align="center"><strong>:</strong></div></td>
      <td><div align="left">
        <select name="User_level" id="User_level">
		<option value="<?php echo $row['User_level'];?>"><?php echo $row['User_level'];?></option>
          <option value="-">Select level</option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
      </div></td>
    </tr>
	  <?php }?>
    <tr bordercolor="#F0F0F0" bgcolor="#FFFFFF">
      <th colspan="3" scope="row"><div align="right"></div></th>
    </tr>
    <tr bordercolor="#F0F0F0" bgcolor="#FFFFFF">
      <th height="44" colspan="3" scope="row"><div align="center">
        <input name="submit" type="submit" id="submit" value="Submit" onclick="show_alert()" />
        <input name="Back" type="submit" id="Back" value="Back" />
      </div></th>
    </tr>
  </table>
  
<?php 
mysql_close($con);
?>
  
  <p>&nbsp;</p>
  <div id="footer">
    <p align="left"><strong>Access Level description:</strong><br />
      Level 1   : Admin page<br />
      Level 2	: HRA page<br />
      Level 3	: Guard Page </p>
  </div>
</form>

update_user1.php

<?php

if (isset($_POST['submit'])){
//if (isset($_POST['User_level'])){

$link = mysql_connect("localhost","root","root") or die ("Could not connect to the Database Server!");
$db = mysql_select_db("pqs", $link) or die ("Could not select Database!");
//User_id = $_GET['User_id'];
$result = mysql_query("UPDATE userdb SET User_tcno = '$User_tcno', User_name = '$User_name', User_dep = '$User_dep', User_username = '$User_username', User_password = '$User_password', User_level = '$User_level' WHERE User_id = '$User_id'");
}

if($result){
                                                                                
echo "<script type='text/javascript'>window.location='index.php?page=list_user.php'</script>";
}


mysql_close();
?>

delete_user.php

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pqs", $con);
$User_id = $_GET['User_id'];
$result = mysql_query("DELETE FROM userdb WHERE User_id = '$User_id'");


if($result){
echo "Deleted Successfully";
echo "<script type='text/javascript'>window.location='index.php?page=list_user.php' </script>";
}

else {
echo "ERROR";
}


mysql_close($con);
?>

Please help me with this problem..
Thanks in advance..:)

Dani AI

Generated

Brief diagnosis and concrete fixes (root causes seen in the thread)

  • The update handler never got the primary key or the posted fields, so the UPDATE ran with wrong/empty values. was right to flag the missing posted variables; confirmed adding the id fixed the update.
  • A few index/name mismatches: $_GET['$User_id'] is wrong (the array key must not include a $), the list page passes id while delete handler expects User_id, and the update used the field being changed (User_tcno) in the WHERE clause instead of the primary key — that can easily match multiple rows.
  • The error in update_staff1.php on line 25 is most likely caused by calling mysql_close when the connection variable was never created (connection code runs only inside the POST branch).

Short checklist of fixes to apply

  • Make the update form include the record id as a hidden input (so the handler can read $_POST['User_id']).
  • In the update handler, assign every expected input from $_POST (and cast the id to int) and use the primary key in the WHERE clause.
  • Make the delete link and the delete handler use the same parameter name (either change the link to ?User_id=... or change the handler to read $_GET['id']).
  • Move connection cleanup inside the same block that opens the connection (or check the variable exists before calling mysql_close).
  • Replace mysql_* with mysqli or PDO and use prepared statements to avoid SQL injection and subtle bugs.

Safe example (mysqli prepared update)

$mysqli = new mysqli('localhost','root','root','pqs');
$stmt = $mysqli->prepare(
  "UPDATE userdb SET User_tcno=?, User_name=?, User_dep=?, User_username=?, User_password=?, User_level=? WHERE User_id=?"
);
$stmt->bind_param('ssssssi', $tcno,$name,$dep,$uname,$pwd,$level,$id);

$tcno = $_POST['User_tcno']; $name = $_POST['User_name']; /* ... */ $id = (int) $_POST['User_id'];

$stmt->execute();
if ($stmt->affected_rows > 0) header('Location: index.php?page=list_user.php');
else echo 'No rows updated or error: '.$stmt->error;
$stmt->close(); $mysqli->close();

Quick debug & safety tips

  • During debugging, dump $_POST/$_GET and check the SQL error string ($mysqli->error or mysql_error()).
  • Avoid using GET for destructive actions in production; require POST + confirmation.
  • Never store plaintext passwords; hash them. Use prepared statements (mysqli/PDO) instead of deprecated mysql_* functions.

Recommended Answers

All 15 Replies

Where is $User_id defined in update_user1.php?
Also you need to assign all form's posted variables.
e.g.
$User_tcno = $_POST;
Then only you can use $User_tcno in update query.

Its still same when I define $User_id in update_user1.php.
How about delete data?

post your recent updated code...

update_user1.php

<?php

if (isset($_POST['submit'])){
//if (isset($_POST['User_level'])){

$link = mysql_connect("localhost","root","root") or die ("Could not connect to the Database Server!");
$db = mysql_select_db("pqs", $link) or die ("Could not select Database!");
$User_id = $_GET['$User_id'];
$result = mysql_query("UPDATE userdb SET User_tcno = '$User_tcno', User_name = '$User_name', User_dep = '$User_dep', User_username = '$User_username', User_password = '$User_password', User_level = '$User_level' WHERE User_id = '$User_id'");
}

if($result){
                                                                                
echo "<script type='text/javascript'>window.location='index.php?page=list_user.php'</script>";
}


mysql_close($link);
?>

delete_user.php

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pqs", $con);
$User_id = $_GET['User_id'];
$result = mysql_query("DELETE FROM userdb WHERE User_id = '$User_id'");


if($result){
echo "Deleted Successfully";
echo "<script type='text/javascript'>window.location='index.php?page=list_user.php' </script>";
}

else {
echo "ERROR";
}

mysql_close($con);
?>

You can not use $ after bracker.
$_GET; should be $_GET;
Are you sure you are getting $User_id ??
Also as i said in previous page you need to define all variable before using it in update query as shown below.

<?php

if (isset($_POST['submit'])){
//if (isset($_POST['User_level'])){

$link = mysql_connect("localhost","root","root") or die ("Could not connect to the Database Server!");
$db = mysql_select_db("pqs", $link) or die ("Could not select Database!");
$User_id = $_GET['User_id'];
$User_tcno = $_POST['User_tcno'];
$User_name = $_POST['User_name'];
$User_dep = $_POST['User_dep'];
$User_username = $_POST['User_username'];
$User_password = $_POST['User_password'];
$User_level = $_POST['User_level'];
$result = mysql_query("UPDATE userdb SET User_tcno = '$User_tcno', User_name = '$User_name', User_dep = '$User_dep', User_username = '$User_username', User_password = '$User_password', User_level = '$User_level' WHERE User_id = '$User_id'");
}

if($result){
                                                                                
echo "<script type='text/javascript'>window.location='index.php?page=list_user.php'</script>";
}


mysql_close($link);
?>

User_id is my primary key for table userdb.
Its must be defined to get all the data in row. Am I right?
So I defined it for calling the data in row.
Now I already defined all the column name but the result still same..
I still cannot update the data..
Can you help me to find where is the error?

And another problem had occur..
You said that User_id is not necessary right?
So I change User_id in update sql statement to User_tcno for the condition and successfully update but its update all data in all row.
Not for the certain row..
Can anybody explain me what is going on there?
This is my updated code that I have done..

update_user1.php

<?php

if (isset($_POST['submit'])){
//if (isset($_POST['User_level'])){

$link = mysql_connect("localhost","root","root") or die ("Could not connect to the Database Server!");
$db = mysql_select_db("pqs", $link) or die ("Could not select Database!");
//$User_id = $_POST['User_id'];
$User_tcno = $_POST['User_tcno'];
$User_name = $_POST['User_name'];
$User_dep = $_POST['User_dep'];
$User_username = $_POST['User_username'];
$User_password = $_POST['User_password'];
$User_level = $_POST['User_level'];

$result = mysql_query("UPDATE userdb SET User_tcno = '$User_tcno', User_name = '$User_name', User_dep = '$User_dep', User_username = '$User_username', User_password = '$User_password', User_level = '$User_level' WHERE User_tcno = '$User_tcno'");
}

if($result){
                                                                                
echo "<script type='text/javascript'>window.location='index.php?page=list_user.php'</script>";
}


mysql_close($link);
?>

of-course $User_id is needed.
I said $User_id = $_POST; should be $User_id = $_POST;.
In form you have all fields which will be posted when you click on submit. But there is no field called User_id so you want get it in posted value.
Add below code in form so User_id will be posted after submission.

<input type="hidden" name="User_id" value="<?php echo $ref1; ?>" />

Thanks vibhadevit. It works to the update!
Is it same to the delete function?

I din't find delete link on update_user.php.
If you are deleting through URL like delete_user.php?User_id=<?php echo $id ?> then it will work or post your code where you have delete link.

You didn't posted the data to update the table.

No..I am deleting it in list_user.php form.
So this is my code from the list_user.php.

list_user.php

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pqs", $con);
$result = mysql_query("SELECT * FROM userdb");
?>

<table width="580" border="0" align="center">
  <tr>
    <th height="48" colspan="6" scope="col">List of User </th>
  </tr>
  <tr>
    <th width="166" scope="row">Name</th>
    <td width="94"><div align="center"><strong>Username</strong></div></td>
    <td width="93"><div align="center"><strong>Password</strong></div></td>
    <td width="67"><div align="center"><strong>View</strong></div></td>
    <td width="67"><div align="center"><strong>Update</strong></div></td>
    <td width="67"><div align="center"><strong>Delete</strong></div></td>
  </tr>
  
  <?php while($row = mysql_fetch_array($result))
				{

				?>
  
  <tr>
  <input type="hidden" name="User_id" value="<?php echo $ref1; ?>" />
    <th scope="row"><div align="center"><?php echo $row['User_name']; ?>&nbsp;</div></th>
    <td> <div align="center"><?php echo $row['User_username']; ?>&nbsp;</div></td>
    <td><div align="center"><?php echo $row['User_password']; ?>&nbsp;</div></td>
    <td><div align="center"><a href="view_user.php?ref1=<?php echo $row['User_id'];?>">view</a></div></td>
    <td><div align="center"><a href="update_user.php?ref1=<?php echo $row['User_id'];?>">update</a></div></td>
    <td><div align="center"><a href="delete_user.php?id=<?php echo $row['User_id'];?>">delete</a></div></td>
  </tr>
  <?php }?>
  <tr>
    <th colspan="6" scope="row">&nbsp;</th>
  </tr>
  <tr>
    <th colspan="6" scope="row"><input name="print" type="submit" id="print" value="Print" />
    <input name="export" type="submit" id="export" value="Export" /></th>
  </tr>
</table>

<?php 
mysql_close($con);
?>

And lastly, can u help me find the error in this update form?
The code is same with update_user1.php but get the error in line 25..

This is the code for the update_staff1.php

update_staff1.php

<?php
if (isset($_POST['submit'])){

$link = mysql_connect("localhost","root","root") or die ("Could not connect to the Database Server!");
$db = mysql_select_db("pqs", $link) or die ("Could not select Database!");

$Staff_id = $_POST['Staff_id'];
$Staff_id_no = $_POST['Staff_id_no'];
$Staff_name = $_POST['Staff_name'];
$Staff_purpose = $_POST['Staff_purpose'];
$Staff_time_in = $_POST['Staff_time_in'];
$Staff_Time_out = $_POST['Staff_Time_out'];
$Staff_dept = $_POST['Staff_dept'];
$Staff_jawatan = $_POST['Staff_jawatan'];
$staff_date = $_POST['staff_date'];

$result=mysql_query("UPDATE staffdb SET Staff_id_no = '$Staff_id_no', Staff_name = '$Staff_name', Staff_purpose = '$Staff_purpose', Staff_time_in = '$Staff_time_in', Staff_Time_out = '$Staff_Time_out', Staff_dept = '$Staff_dept', Staff_jawatan = '$Staff_jawatan', staff_date = '$staff_date' WHERE Staff_id = '$Staff_id'");
}

if($result){

echo "<script type='text/javascript'>window.location='index.php?page=list_staff.php'</script>";
}

mysql_close($link);
?>

Please help me..I really can find the solution.. :(

You have passed "id" variable in list_user.php
delete_user.php?id=<?php echo $row;?>

And you are using "User_Id" to get its value in delete_user.php.
So replace below line

$User_id = $_GET['User_id'];

with

$User_id = $_GET['id'];

Even I had faced same problem with the code & have understood this type problem after many attempts...:)

And how about my update_staff1.php?
I still can not find any solution for that problem.
Its still occur the same problem.
Can anyone help me find out where is the error?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.