Jump to content

Problem editing data


Recommended Posts

Hi,

 

Can anyone tell me where I have gone wrong with this code? It's aim is to display all the usernames with an edit button alongside each to take you to a different page displaying the username in a textbox which can be edited. Up to this point it all works correctly however when I change the username it doesnt update in the database table. No error messages are appearing either. Thanks.

 

edit.php:

<?php # Script 13.8 - edit.php

// Include the configuration file for error management and such.
require_once ('./includes/config.inc.php');

// Set the page title and include the HTML header.
$page_title = 'Edit Users';
include ('./includes/header.html');
?>

<table>
  <tr>
    <td align="center"><p><h1>Edit Users:</h1></p></td>
  </tr>
  <tr>
    <td>
      <table border="1" bgcolor=grey>
      <?php
      require_once ('../mysql_connect.php'); // Connect to the database.
      $order = "SELECT username FROM users";
      $result = mysql_query($order);
      while ($row=mysql_fetch_array($result)){
        echo ("<tr><td>$row[username]</td>");
        echo ("<td><a href=\"edit_form.php?id=$row[username]\">Edit</a></td>");
        echo ("<td><a href=\"delete.php?id=$row[username]\">Delete</a></td></tr>");
      }
      ?>
      </table>
    </td>
   </tr>
</table>

<?php
include ('./includes/footer.html');
?>

 

edit_form.php:

<table border=1>
	  <tr>
	    <td align=center>Form Edit Employees Data</td>
	  </tr>
	  <tr>
	    <td>
	      <table>
	      <?php
	      require_once ('../mysql_connect.php'); // Connect to the database.
	      $id = mysql_real_escape_string($_GET['id']);
	      $order = "SELECT username FROM users
					where username='$id'";

	      $result = mysql_query($order);
	      $row = mysql_fetch_array($result);
	      ?>
	      <form method="post" action="edit_data.php">
	      <input type="hidden" name="id" value="<?php echo $row['username'];?>">
	        <tr>
	          <td>Username</td>
	          <td>
	            <input type="text" name="username"
	        size="20" value="<?php echo $row['username'];?>">
	          </td>
	        </tr>
	        <tr>
	          <td align="right">
	            <input type="submit"
	          name="submit value" value="Edit">
	          </td>
	        </tr>
	      </form>
	      </table>
	    </td>
	  </tr>
</table>

 

edit_data.php:

<?php
//edit_data.php
require_once ('../mysql_connect.php'); // Connect to the database.
$username = mysql_real_escape_string($_POST['username']);
$order = "UPDATE users
          SET username='$username'

          WHERE
          username='$id'";
mysql_query($order);
header("location:edit.php");
?>

Link to comment
https://forums.phpfreaks.com/topic/197620-problem-editing-data/
Share on other sites

Try changing edit_data.php to

<?php
   //edit_data.php
   require_once ('../mysql_connect.php'); // Connect to the database.
   $id = mysql_real_escape_string($_POST['id']); // added this, need to get the id from $_POST 
   $username = mysql_real_escape_string($_POST['username']);
   $order = "UPDATE users
             SET username='$username'

             WHERE
             username='$id'";
   mysql_query($order);
   header("location:edit.php");
   ?>

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.